1.開發環境搭建以及建立Maven Web項目css
參看以前的博文[確保maven web項目不報錯]:http://www.cnblogs.com/cainiaomahua/p/6306476.htmlhtml
2.SSM整合java
此次整合有2個配置文件,分別是spring-mybatis.xml,包含spring和mybatis的配置文件,還有個是spring-mvc的配置文件,此外有2個資源文件:jdbc.propertis和log4j.properties。mysql
完整目錄結構以下:web
使用框架的版本:spring
Spring 4.0.2 RELEASEsql
Spring MVC 4.0.2 RELEASE數據庫
MyBatis 3.2.6apache
2.1 Maven引入須要的JAR包json
在pom.xml中引入jar包(此處引入jar包,能夠複製代碼,也可使用視圖的方式,參考此連接:http://www.360doc.com/content/14/0517/18/9560593_378558295.shtml)
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.light.web</groupId> 8 <artifactId>oneBirdPro</artifactId> 9 <version>0.0.1-SNAPSHOT</version> 10 <packaging>war</packaging> 11 12 <name>oneBirdPro Maven Webapp</name> 13 <!-- FIXME change it to the project's website --> 14 <url>http://www.example.com</url> 15 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 <maven.compiler.source>1.7</maven.compiler.source> 19 <maven.compiler.target>1.7</maven.compiler.target> 20 21 <!-- spring版本號 --> 22 <spring.version>4.0.2.RELEASE</spring.version> 23 <!-- mybatis版本號 --> 24 <mybatis.version>3.2.6</mybatis.version> 25 <!-- log4j日誌文件管理包版本 --> 26 <slf4j.version>1.7.7</slf4j.version> 27 <log4j.version>1.2.17</log4j.version> 28 </properties> 29 30 <dependencies> 31 <dependency> 32 <groupId>junit</groupId> 33 <artifactId>junit</artifactId> 34 <version>4.11</version> 35 <scope>test</scope> 36 </dependency> 37 38 <!-- spring核心包 --> 39 <dependency> 40 <groupId>org.springframework</groupId> 41 <artifactId>spring-core</artifactId> 42 <version>${spring.version}</version> 43 </dependency> 44 45 <dependency> 46 <groupId>org.springframework</groupId> 47 <artifactId>spring-web</artifactId> 48 <version>${spring.version}</version> 49 </dependency> 50 <dependency> 51 <groupId>org.springframework</groupId> 52 <artifactId>spring-oxm</artifactId> 53 <version>${spring.version}</version> 54 </dependency> 55 <dependency> 56 <groupId>org.springframework</groupId> 57 <artifactId>spring-tx</artifactId> 58 <version>${spring.version}</version> 59 </dependency> 60 61 <dependency> 62 <groupId>org.springframework</groupId> 63 <artifactId>spring-jdbc</artifactId> 64 <version>${spring.version}</version> 65 </dependency> 66 67 <dependency> 68 <groupId>org.springframework</groupId> 69 <artifactId>spring-webmvc</artifactId> 70 <version>${spring.version}</version> 71 </dependency> 72 <dependency> 73 <groupId>org.springframework</groupId> 74 <artifactId>spring-aop</artifactId> 75 <version>${spring.version}</version> 76 </dependency> 77 78 <dependency> 79 <groupId>org.springframework</groupId> 80 <artifactId>spring-context-support</artifactId> 81 <version>${spring.version}</version> 82 </dependency> 83 84 <dependency> 85 <groupId>org.springframework</groupId> 86 <artifactId>spring-test</artifactId> 87 <version>${spring.version}</version> 88 </dependency> 89 <!-- mybatis核心包 --> 90 <dependency> 91 <groupId>org.mybatis</groupId> 92 <artifactId>mybatis</artifactId> 93 <version>${mybatis.version}</version> 94 </dependency> 95 <!-- mybatis/spring包 --> 96 <dependency> 97 <groupId>org.mybatis</groupId> 98 <artifactId>mybatis-spring</artifactId> 99 <version>1.2.2</version> 100 </dependency> 101 <!-- 導入java ee jar 包 --> 102 <dependency> 103 <groupId>javax</groupId> 104 <artifactId>javaee-api</artifactId> 105 <version>7.0</version> 106 </dependency> 107 <!-- 導入Mysql數據庫連接jar包 --> 108 <!-- <dependency> 109 <groupId>mysql</groupId> 110 <artifactId>mysql-connector-java</artifactId> 111 <version>5.1.30</version> 112 </dependency> --> 113 <!-- 導入Oracle數據庫連接jar包 --> 114 <dependency> 115 <groupId>com.oracle</groupId> 116 <artifactId>ojdbc14</artifactId> 117 <version>10.2.0.2.0</version> 118 </dependency> 119 <dependency> 120 <groupId>c3p0</groupId> 121 <artifactId>c3p0</artifactId> 122 <version>0.9.1.1</version> 123 </dependency> 124 <!-- 導入dbcp的jar包,用來在applicationContext.xml中配置數據庫 --> 125 <dependency> 126 <groupId>commons-dbcp</groupId> 127 <artifactId>commons-dbcp</artifactId> 128 <version>1.2.2</version> 129 </dependency> 130 <!-- JSTL標籤類 --> 131 <dependency> 132 <groupId>jstl</groupId> 133 <artifactId>jstl</artifactId> 134 <version>1.2</version> 135 </dependency> 136 <!-- 日誌文件管理包 --> 137 <!-- log start --> 138 <dependency> 139 <groupId>log4j</groupId> 140 <artifactId>log4j</artifactId> 141 <version>${log4j.version}</version> 142 </dependency> 143 144 145 <!-- 格式化對象,方便輸出日誌 --> 146 <dependency> 147 <groupId>com.alibaba</groupId> 148 <artifactId>fastjson</artifactId> 149 <version>1.1.41</version> 150 </dependency> 151 152 <dependency> 153 <groupId>org.slf4j</groupId> 154 <artifactId>slf4j-api</artifactId> 155 <version>${slf4j.version}</version> 156 </dependency> 157 158 <dependency> 159 <groupId>org.slf4j</groupId> 160 <artifactId>slf4j-log4j12</artifactId> 161 <version>${slf4j.version}</version> 162 </dependency> 163 <!-- log end --> 164 <!-- 映入JSON --> 165 <dependency> 166 <groupId>org.codehaus.jackson</groupId> 167 <artifactId>jackson-mapper-asl</artifactId> 168 <version>1.9.13</version> 169 </dependency> 170 <!-- 上傳組件包 --> 171 <dependency> 172 <groupId>commons-fileupload</groupId> 173 <artifactId>commons-fileupload</artifactId> 174 <version>1.3.1</version> 175 </dependency> 176 <dependency> 177 <groupId>commons-io</groupId> 178 <artifactId>commons-io</artifactId> 179 <version>2.4</version> 180 </dependency> 181 <dependency> 182 <groupId>commons-codec</groupId> 183 <artifactId>commons-codec</artifactId> 184 <version>1.9</version> 185 </dependency> 186 <dependency> 187 <groupId>org.hamcrest</groupId> 188 <artifactId>hamcrest-core</artifactId> 189 <version>1.3.RC2</version> 190 </dependency> 191 192 <dependency> 193 <groupId>org.hamcrest</groupId> 194 <artifactId>hamcrest-library</artifactId> 195 <version>1.3.RC2</version> 196 </dependency> 197 <dependency> 198 <groupId>commons-logging</groupId> 199 <artifactId>commons-logging</artifactId> 200 <version>1.1.3</version> 201 </dependency> 202 </dependencies> 203 204 <build> 205 <finalName>oneBirdPro</finalName> 206 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> 207 <plugins> 208 <plugin> 209 <artifactId>maven-clean-plugin</artifactId> 210 <version>3.0.0</version> 211 </plugin> 212 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> 213 <plugin> 214 <artifactId>maven-resources-plugin</artifactId> 215 <version>3.0.2</version> 216 </plugin> 217 <plugin> 218 <artifactId>maven-compiler-plugin</artifactId> 219 <version>3.7.0</version> 220 </plugin> 221 <plugin> 222 <artifactId>maven-surefire-plugin</artifactId> 223 <version>2.20.1</version> 224 </plugin> 225 <plugin> 226 <artifactId>maven-war-plugin</artifactId> 227 <version>3.2.0</version> 228 </plugin> 229 <plugin> 230 <artifactId>maven-install-plugin</artifactId> 231 <version>2.5.2</version> 232 </plugin> 233 <plugin> 234 <artifactId>maven-deploy-plugin</artifactId> 235 <version>2.8.2</version> 236 </plugin> 237 </plugins> 238 </pluginManagement> 239 </build> 240 </project>
2.2 整合SpringMVC 配置文件中不要出現中文空格
配置裏面的註釋也很詳細,主要是自動掃描控制器,視圖模式,註解的啓動這三個。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.1.xsd 11 http://www.springframework.org/schema/mvc 12 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 13 <!-- 自動掃描該包,使SpringMVC認爲包下用了@controller註解的類是控制器 --> 14 <context:component-scan base-package="com.bird.business.controller" /> 15 <context:component-scan base-package="com.bird.business.service" /> 16 <context:component-scan base-package="com.bird.business.dao" /> 17 <!--避免IE執行AJAX時,返回JSON出現下載文件 --> 18 <bean id="mappingJacksonHttpMessageConverter" 19 class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 20 <property name="supportedMediaTypes"> 21 <list> 22 <value>text/html;charset=UTF-8</value> 23 </list> 24 </property> 25 </bean> 26 <!-- 啓動SpringMVC的註解功能,完成請求和註解POJO的映射 --> 27 <bean 28 class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 29 <property name="messageConverters"> 30 <list> 31 <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON轉換器 --> 32 </list> 33 </property> 34 </bean> 35 <!-- 定義跳轉的文件的先後綴 ,視圖模式配置--> 36 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 37 <!-- 這裏的配置個人理解是自動給後面action的方法return的字符串加上前綴和後綴,變成一個 可用的url地址 --> 38 <property name="prefix" value="/WEB-INF/jsp/" /> 39 <property name="suffix" value=".jsp" /> 40 </bean> 41 42 <!-- 配置文件上傳,若是沒有使用文件上傳能夠不用配置,固然若是不配,那麼配置文件中也沒必要引入上傳組件包 --> 43 <bean id="multipartResolver" 44 class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 45 <!-- 默認編碼 --> 46 <property name="defaultEncoding" value="utf-8" /> 47 <!-- 文件大小最大值 --> 48 <property name="maxUploadSize" value="10485760000" /> 49 <!-- 內存中的最大值 --> 50 <property name="maxInMemorySize" value="40960" /> 51 </bean> 52 53 </beans>
2.2.2 配置web.xml文件
配置的spring-mvc的Servlet就是爲了完成SpringMVC+MAVEN的整合。
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <web-app 4 version="3.0" 5 xmlns="http://java.sun.com/xml/ns/javaee" 6 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 7 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 8 metadata-complete="true"> 9 <display-name>Archetype Created Web Application</display-name> 10 11 <!-- Spring和mybatis的配置文件 --> 12 <context-param> 13 <param-name>contextConfigLocation</param-name> 14 <param-value>classpath:spring-mybatis.xml</param-value> 15 </context-param> 16 17 <!-- 編碼過濾器 --> 18 <filter> 19 <filter-name>encodingFilter</filter-name> 20 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 21 <async-supported>true</async-supported> 22 <init-param> 23 <param-name>encoding</param-name> 24 <param-value>UTF-8</param-value> 25 </init-param> 26 </filter> 27 <filter-mapping> 28 <filter-name>encodingFilter</filter-name> 29 <url-pattern>/*</url-pattern> 30 </filter-mapping> 31 32 <!-- Spring監聽器 --> 33 <listener> 34 <listener-class> 35 org.springframework.web.context.ContextLoaderListener 36 </listener-class> 37 </listener> 38 <!-- 防止Spring內存溢出監聽器 --> 39 <listener> 40 <listener-class> 41 org.springframework.web.util.IntrospectorCleanupListener 42 </listener-class> 43 </listener> 44 45 <!-- Spring MVC servlet --> 46 <servlet> 47 <servlet-name>SpringMVC</servlet-name> 48 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 49 <!-- 50 配置SpringMVC 須要配置的文件 51 spring-dao.xml,spring-service.xml,spring-web.xml 52 Mybites -> spring -> springMvc 53 --> 54 <init-param> 55 <param-name>contextConfigLocation</param-name> 56 <param-value>classpath:spring-mvc.xml</param-value> 57 </init-param> 58 <load-on-startup>1</load-on-startup> 59 <async-supported>true</async-supported> 60 </servlet> 61 <servlet-mapping> 62 <servlet-name>SpringMVC</servlet-name> 63 <!-- 此處能夠能夠配置成*.do,對應struts的後綴習慣 --> 64 <url-pattern>/</url-pattern> 65 </servlet-mapping> 66 <welcome-file-list> 67 <welcome-file>/index.jsp</welcome-file> 68 </welcome-file-list> 69 </web-app>
爲了方便調試,通常都會使用日誌來輸出信息,Log4j是Apache的一個開放源代碼項目,經過使用Log4j,咱們能夠控制日誌信息輸送的目的地是控制檯、文件、GUI組件,甚至是套接口服務器、NT的事件記錄器、UNIX Syslog守護進程等;咱們也能夠控制每一條日誌的輸出格式;經過定義每一條日誌信息的級別,咱們可以更加細緻地控制日誌的生成過程。
Log4j的配置很簡單,並且也是通用的,下面給出一個基本的配置,換到其餘項目中也無需作多大的調整。
log4j.properties
1 #定義LOG輸出級別 2 log4j.rootLogger=INFO,Console,File 3 #定義日誌輸出目的地爲控制檯 4 log4j.appender.Console=org.apache.log4j.ConsoleAppender 5 log4j.appender.Console.Target=System.out 6 #能夠靈活地指定日誌輸出格式,下面一行是指定具體的格式 7 log4j.appender.Console.layout=org.apache.log4j.PatternLayout 8 log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n 9 10 #文件大小達到指定尺寸的時候產生一個新的文件 11 log4j.appender.File=org.apache.log4j.RollingFileAppender 12 #指定輸出目錄 13 log4j.appender.File.File=logs/ssm.log 14 #定義文件最大大小 15 log4j.appender.File.MaxFileSize=10MB 16 #輸出全部日誌,若是換成DEBUG表示輸出DEBUG以上級別日誌 17 log4j.appender.File.Threshold=ALL 18 log4j.appender.File.layout=org.apache.log4j.PatternLayout 19 log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
2.3.一、創建JDBC屬性文件
jdbc.properties(文件編碼修改成utf-8) 根據本身數據庫配置url username password
1 #鏈接數據庫 2 jdbc.driver=oracle.jdbc.driver.OracleDriver 3 jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl 4 jdbc.username=orcl 5 jdbc.password=orcl 6 #定義初始鏈接數 7 initialSize=0 8 #定義最大鏈接數 9 maxActive=20 10 #定義最大空閒 11 maxIdle=20 12 #定義最小空閒 13 minIdle=1 14 #定義最長等待時間 15 maxWait=60000
這個文件就是用來完成spring和mybatis的整合的。主要的就是自動掃描,自動注入,配置數據庫。註釋也很詳細,你們看看就明白了。
spring-mybatis.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd"> 9 10 <!--配置整合mybatis過程 1.配置數據庫相關參數--> 11 <context:property-placeholder location="classpath:jdbc.properties"/> 12 13 <!--2.數據庫鏈接池--> 14 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 15 <!--配置鏈接池屬性--> 16 <property name="driverClass" value="${jdbc.driver}" /> 17 18 <!-- 基本屬性 url、user、password --> 19 <property name="jdbcUrl" value="${jdbc.url}" /> 20 <property name="user" value="${jdbc.username}" /> 21 <property name="password" value="${jdbc.password}" /> 22 23 <!--c3p0私有屬性--> 24 <property name="maxPoolSize" value="30"/> 25 <property name="minPoolSize" value="10"/> 26 <!--關閉鏈接後不自動commit--> 27 <property name="autoCommitOnClose" value="false"/> 28 29 <!--獲取鏈接超時時間--> 30 <property name="checkoutTimeout" value="1000"/> 31 <!--當獲取鏈接失敗重試次數--> 32 <property name="acquireRetryAttempts" value="2"/> 33 </bean> 34 35 <!--約定大於配置--> 36 <!--3.配置SqlSessionFactory對象--> 37 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 38 39 <!--往下才是mybatis和spring真正整合的配置--> 40 <!--注入數據庫鏈接池--> 41 <property name="dataSource" ref="dataSource"/> 42 <!-- 43 配置mybatis全局配置文件:mybatis-config.xml 44 指定Mybatis的配置文件位置。若是指定了該屬性, 45 那麼會以該配置文件的內容做爲配置信息構建對應的SqlSessionFactoryBuilder, 46 可是後續屬性指定的內容會覆蓋該配置文件裏面指定的對應內容 47 --> 48 <property name="configLocation" value="classpath:mybatis-config.xml"/> 49 <!-- 50 掃描entity包,使用別名,多個用;隔開 51 通常對應實體類所在的包,這個時候會自動取對應包中不包括包名的簡單類名做爲包括包名的別名。 52 多個package之間能夠用逗號或者分號等來進行分隔。(value的值必定要是包的全名) 53 --> 54 <property name="typeAliasesPackage" value="com.bird.business.domain"/> 55 <!-- 56 掃描sql配置文件:mapper須要的xml文件 57 Mapper文件存放的位置,當Mapper文件跟對應的Mapper接口處於同一位置的時候能夠不用指定該屬性的值 58 --> 59 <property name="mapperLocations" value="classpath:com/bird/business/mapper/*Mapper.xml"/> 60 <!-- 61 數組類型,用來指定別名的。 62 指定了這個屬性後,Mybatis會把這個類型的短名稱做爲這個類型的別名, 63 前提是該類上沒有標註@Alias註解,不然將使用該註解對應的值做爲此種類型的別名。 64 (value的值必定要是類的徹底限定名) 65 --> 66 <!-- <property name="typeAliases"> 67 <array> 68 <value>com.tiantian.mybatis.model.Blog</value> 69 <value>com.tiantian.mybatis.model.Comment</value> 70 </array> 71 </property> --> 72 </bean> 73 74 <!--4:配置掃描Dao接口包,動態實現DAO接口,注入到spring容器--> 75 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 76 <!--注入SqlSessionFactory--> 77 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 78 <!-- 給出須要掃描的Dao接口--> 79 <property name="basePackage" value="com.bird.business.dao"/> 80 </bean> 81 82 </beans>
2.3.一、創建mybatis-config.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--配置全局屬性--> <!-- <settings> --> <!--使用jdbc的getGeneratekeys獲取自增主鍵值--> <!-- <setting name="useGeneratedKeys" value="true"/> --> <!--使用列別名替換列名 默認值爲true select name as title(實體中的屬性名是title) form table; 開啓後mybatis會自動幫咱們把表中name的值賦到對應實體的title屬性中 --> <!-- <setting name="useColumnLabel" value="true"/> --> <!--開啓駝峯命名轉換Table:create_time到 Entity(createTime)--> <!-- <setting name="mapUnderscoreToCamelCase" value="true"/> --> <!-- </settings> --> </configuration>
2.4 運行測試
建立實體類、MyBatis映射文件以及DAO接口
SysUser類
1 package com.bird.business.domain; 2 3 public class SysUser { 4 5 //---------------------主鍵ID---------------------// 6 /** 7 * 主鍵ID 8 */ 9 private String id = null; 10 11 public String getId() { 12 return id; 13 } 14 15 public void setId(String id) { 16 this.id = id; 17 } 18 //-----------------用戶賬號---------------------// 19 /** 20 * 用戶賬號 21 */ 22 private String username = null; 23 24 /** 25 * 設置屬性用戶賬號的值 26 * 27 * @param username 28 * 用戶賬號 29 */ 30 public void setUsername(String username) { 31 this.username = username; 32 } 33 34 /** 35 * 取得屬性用戶賬號的值 36 * 37 * @return 用戶賬號 38 */ 39 public String getUsername() { 40 return this.username; 41 } 42 //-----------------用戶姓名---------------------// 43 /** 44 * 用戶姓名 45 */ 46 private String trueName = null; 47 48 /** 49 * 設置屬性用戶姓名的值 50 * 51 * @param trueName 52 * 用戶姓名 53 */ 54 public void setTrueName(String trueName) { 55 this.trueName = trueName; 56 } 57 58 /** 59 * 取得屬性用戶姓名的值 60 * 61 * @return 用戶姓名 62 */ 63 public String getTrueName() { 64 return this.trueName; 65 } 66 67 /** 68 * 刪除標識 69 */ 70 private String deleteFlag=null; 71 72 public String getDeleteFlag() { 73 return deleteFlag; 74 } 75 76 public void setDeleteFlag(String deleteFlag) { 77 this.deleteFlag = deleteFlag; 78 } 79 80 }
DAO接口類
1 package com.bird.business.dao; 2 3 import com.bird.business.domain.SysUser; 4 5 public interface SysUserDao { 6 7 public SysUser findSysUserById(String username); 8 }
實現增刪改查的xml配置文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.bird.business.dao.SysUserDao"> 6 <!-- 定義緩存 通常是一級緩存,若是用同一個sqlsession 那麼相同查詢直接會從緩存中查找 7 <cache size="1024" flushInterval="60000" eviction="LRU" readOnly="false"></cache> 8 --> 9 10 <!-- 按照用戶名和密碼查找 --> 11 <select id="findSysUserById" resultType="SysUser" parameterType="String"> 12 select 13 t.id as id, 14 t.username as username, 15 t.true_name as trueName, 16 t.delete_flag as deleteFlag 17 from sys_user t where t.username = #{username} 18 </select> 19 20 </mapper>
創建Service接口和實現類
1 package com.bird.business.service; 2 3 import com.bird.business.domain.SysUser; 4 5 public interface ISysUserService { 6 public SysUser getSysUserById(String username); 7 }
1 package com.bird.business.service.impl; 2 3 import javax.annotation.Resource; 4 5 import org.springframework.stereotype.Service; 6 7 import com.bird.business.dao.SysUserDao; 8 import com.bird.business.domain.SysUser; 9 import com.bird.business.service.ISysUserService; 10 11 @Service("iSysUserService") 12 public class SysUserServiceImpl implements ISysUserService { 13 14 @Resource 15 private SysUserDao sysUserDao; 16 17 @Override 18 public SysUser getSysUserById(String username) { 19 return this.sysUserDao.findSysUserById(username); 20 } 21 22 }
創建UserController類
1 package com.bird.business.controller; 2 3 import javax.annotation.Resource; 4 import javax.servlet.http.HttpServletRequest; 5 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 10 import com.bird.business.domain.SysUser; 11 import com.bird.business.service.ISysUserService; 12 13 @Controller 14 @RequestMapping("/student") 15 public class SysUserController { 16 @Resource 17 private ISysUserService iSysUserService; 18 19 @RequestMapping("/studentView") 20 public String toIndex(HttpServletRequest request, Model model){ 21 String username = request.getParameter("username"); 22 SysUser user = this.iSysUserService.getSysUserById(username); 23 model.addAttribute("user", user); 24 return "studentView"; 25 } 26 }
新建jsp頁面
studentView.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'index.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 根據用戶帳號查詢用戶信息. <br> 25 ${user.username}---${user.trueName}26 </body> 27 </html>
部署項目
輸入地址:http://ip:端口/oneBirdPro/student/studentView?username=admin