1.开发环境搭建以及创建Maven Web项目

     参看之前的博文[确保maven web项目不报错]:http://www.cnblogs.com/cainiaomahua/p/6306476.html

2.SSM整合

     这次整合有2个配置文件,分别是spring-mybatis.xml,包含spring和mybatis的配置文件,还有个是spring-mvc的配置文件,此外有2个资源文件:jdbc.propertis和log4j.properties。

     完整目录结构如下:

     

     使用框架的版本:

          Spring 4.0.2 RELEASE

          Spring MVC 4.0.2 RELEASE

          MyBatis 3.2.6

    2.1  Maven引入需要的JAR包

           在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  配置文件中不要出现中文空格

   2.2.1   配置spring-mvc.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: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>

   2.2.3   Log4j的配置

       为了方便调试,一般都会使用日志来输出信息,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 Spring与MyBatis的整合

     2.3.1、建立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

    2.3.2、建立spring-mybatis.xml配置文件

    这个文件就是用来完成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.1、建立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

    

 

posted on 2017-01-19 16:00  菜鸟麻花  阅读(46162)  评论(3编辑  收藏  举报