spring+springmvc+mybatis+maven搭建的框架项目

[置顶] Spring+Mybatis+SpringMVC+Maven+MySql搭建实例

标签: SpringMybatisspring mvcMaven
32504人阅读 评论(40) 收藏 举报
本文章已收录于:
分类:
Mybatis(12)
作者同类文章 X
    SpringMVC(8)
    作者同类文章 X
      Spring(24)
      作者同类文章 X

        目录(?)[+]

        1. 一准备工作
        2. 二工程创建
        3. 三spingmybatis配置
        4. 四单元测试
        5. 五转换web工程
        6. 六配置SpringMVC

                林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka

                摘要:本文主要讲了如何使用Maven来搭建spring+Mybatis+SpringMVC+MySQL的搭建实例,文章写得很详细,有代码有图片,最后也带有运行的效果。

        本文工程免费下载

        一、准备工作

        1. 首先创建一个表:

        [sql] view plain copy
        print ?
        1. CREATE TABLE `t_user` (  
        2.   `USER_ID` int(11) NOT NULL AUTO_INCREMENT,  
        3.   `USER_NAME` char(30) NOT NULL,  
        4.   `USER_PASSWORD` char(10) NOT NULL,  
        5.   `USER_EMAIL` char(30) NOT NULL,  
        6.   PRIMARY KEY (`USER_ID`),  
        7.   KEY `IDX_NAME` (`USER_NAME`)  
        8. ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8  
        CREATE TABLE `t_user` (
          `USER_ID` int(11) NOT NULL AUTO_INCREMENT,
          `USER_NAME` char(30) NOT NULL,
          `USER_PASSWORD` char(10) NOT NULL,
          `USER_EMAIL` char(30) NOT NULL,
          PRIMARY KEY (`USER_ID`),
          KEY `IDX_NAME` (`USER_NAME`)
        ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8

        随便插入一些数据:

        [sql] view plain copy
        print ?
        1. INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (1, '林炳文''1234567@''[email protected]');  
        2. INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (2, 'evan''123''[email protected]');  
        3. INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (3, 'kaka''cadg''[email protected]');  
        4. INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (4, 'simle''cscs''[email protected]');  
        5. INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (5, 'arthur''csas''[email protected]');  
        6. INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (6, '小德''yuh78''[email protected]');  
        7. INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (7, '小小''cvff''[email protected]');  
        8. INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (8, '林林之家''gvv''[email protected]');  
        9. INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (9, '林炳文Evankaka''dfsc''[email protected]');  
        10. INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (10, 'apple''uih6''[email protected]');  
        INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (1, '林炳文', '1234567@', '[email protected]');
        INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (2, 'evan', '123', '[email protected]');
        INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (3, 'kaka', 'cadg', '[email protected]');
        INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (4, 'simle', 'cscs', '[email protected]');
        INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (5, 'arthur', 'csas', '[email protected]');
        INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (6, '小德', 'yuh78', '[email protected]');
        INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (7, '小小', 'cvff', '[email protected]');
        INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (8, '林林之家', 'gvv', '[email protected]');
        INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (9, '林炳文Evankaka', 'dfsc', '[email protected]');
        INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (10, 'apple', 'uih6', '[email protected]');
        

        二、工程创建

        1、Maven工程创建

        (1)新建


        (2)选择快速框架

        (3)输出项目名,包,记得选war(表示web项目,以后可以spingMVC连起来用)


        (4)创建好之后 

        目录如下:


        (5)检查下

        这三个地方JDK的版本一定要一样!!!!




        三、sping+mybatis配置

        1、整个工程目录如下:


        2、POM文件

        [html] view plain copy
        print ?
        1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
        3.     <modelVersion>4.0.0modelVersion>  
        4.     <groupId>com.lingroupId>  
        5.     <artifactId>ssm_projectartifactId>  
        6.     <version>0.0.1-SNAPSHOTversion>  
        7.     <packaging>warpackaging>  
        8.     <properties>  
        9.           
        10.         <spring.version>3.2.8.RELEASEspring.version>  
        11.           
        12.         <slf4j.version>1.6.6slf4j.version>  
        13.         <log4j.version>1.2.12log4j.version>  
        14.           
        15.         <junit.version>4.10junit.version>  
        16.           
        17.         <mybatis.version>3.2.1mybatis.version>  
        18.     properties>  
        19.   
        20.     <dependencies>  
        21.           
        22.         <dependency>  
        23.             <groupId>org.springframeworkgroupId>  
        24.             <artifactId>spring-coreartifactId>  
        25.             <version>${spring.version}version>  
        26.         dependency>  
        27.         <dependency>  
        28.             <groupId>org.springframeworkgroupId>  
        29.             <artifactId>spring-webmvcartifactId>  
        30.             <version>${spring.version}version>  
        31.         dependency>  
        32.         <dependency>  
        33.             <groupId>org.springframeworkgroupId>  
        34.             <artifactId>spring-contextartifactId>  
        35.             <version>${spring.version}version>  
        36.         dependency>  
        37.         <dependency>  
        38.             <groupId>org.springframeworkgroupId>  
        39.             <artifactId>spring-context-supportartifactId>  
        40.             <version>${spring.version}version>  
        41.         dependency>  
        42.         <dependency>  
        43.             <groupId>org.springframeworkgroupId>  
        44.             <artifactId>spring-aopartifactId>  
        45.             <version>${spring.version}version>  
        46.         dependency>  
        47.         <dependency>  
        48.             <groupId>org.springframeworkgroupId>  
        49.             <artifactId>spring-aspectsartifactId>  
        50.             <version>${spring.version}version>  
        51.         dependency>  
        52.         <dependency>  
        53.             <groupId>org.springframeworkgroupId>  
        54.             <artifactId>spring-txartifactId>  
        55.             <version>${spring.version}version>  
        56.         dependency>  
        57.         <dependency>  
        58.             <groupId>org.springframeworkgroupId>  
        59.             <artifactId>spring-jdbcartifactId>  
        60.             <version>${spring.version}version>  
        61.         dependency>  
        62.         <dependency>  
        63.             <groupId>org.springframeworkgroupId>  
        64.             <artifactId>spring-webartifactId>  
        65.             <version>${spring.version}version>  
        66.         dependency>  
        67.   
        68.           
        69.         <dependency>  
        70.             <groupId>junitgroupId>  
        71.             <artifactId>junitartifactId>  
        72.             <version>${junit.version}version>  
        73.             <scope>testscope>  
        74.         dependency>  
        75.   
        76.           
        77.           
        78.         <dependency>  
        79.             <groupId>log4jgroupId>  
        80.             <artifactId>log4jartifactId>  
        81.             <version>${log4j.version}version>  
        82.         dependency>  
        83.         <dependency>  
        84.             <groupId>org.slf4jgroupId>  
        85.             <artifactId>slf4j-apiartifactId>  
        86.             <version>${slf4j.version}version>  
        87.         dependency>  
        88.         <dependency>  
        89.             <groupId>org.slf4jgroupId>  
        90.             <artifactId>slf4j-log4j12artifactId>  
        91.             <version>${slf4j.version}version>  
        92.         dependency>  
        93.           
        94.   
        95.           
        96.         <dependency>  
        97.             <groupId>org.springframeworkgroupId>  
        98.             <artifactId>spring-testartifactId>  
        99.             <version>${spring.version}version>  
        100.             <scope>testscope>  
        101.         dependency>  
        102.   
        103.           
        104.         <dependency>  
        105.             <groupId>org.mybatisgroupId>  
        106.             <artifactId>mybatisartifactId>  
        107.             <version>${mybatis.version}version>  
        108.         dependency>  
        109.   
        110.           
        111.         <dependency>  
        112.             <groupId>org.mybatisgroupId>  
        113.             <artifactId>mybatis-springartifactId>  
        114.             <version>1.2.0version>  
        115.         dependency>  
        116.   
        117.           
        118.         <dependency>  
        119.             <groupId>mysqlgroupId>  
        120.             <artifactId>mysql-connector-javaartifactId>  
        121.             <version>5.1.29version>  
        122.         dependency>  
        123.     dependencies>  
        124.   
        125. project>  
        
        	4.0.0
        	com.lin
        	ssm_project
        	0.0.1-SNAPSHOT
        	war
        	
        		
        		3.2.8.RELEASE
        		
        		1.6.6
        		1.2.12
        		
        		4.10
        		
        		3.2.1
        	
        
        	
        		
        		
        			org.springframework
        			spring-core
        			${spring.version}
        		
        		
        			org.springframework
        			spring-webmvc
        			${spring.version}
        		
        		
        			org.springframework
        			spring-context
        			${spring.version}
        		
        		
        			org.springframework
        			spring-context-support
        			${spring.version}
        		
        		
        			org.springframework
        			spring-aop
        			${spring.version}
        		
        		
        			org.springframework
        			spring-aspects
        			${spring.version}
        		
        		
        			org.springframework
        			spring-tx
        			${spring.version}
        		
        		
        			org.springframework
        			spring-jdbc
        			${spring.version}
        		
        		
        			org.springframework
        			spring-web
        			${spring.version}
        		
        
        		
        		
        			junit
        			junit
        			${junit.version}
        			test
        		
        
        		
        		
        		
        			log4j
        			log4j
        			${log4j.version}
        		
        		
        			org.slf4j
        			slf4j-api
        			${slf4j.version}
        		
        		
        			org.slf4j
        			slf4j-log4j12
        			${slf4j.version}
        		
        		
        
        		
        		
        			org.springframework
        			spring-test
        			${spring.version}
        			test
        		
        
        		
        		
        			org.mybatis
        			mybatis
        			${mybatis.version}
        		
        
        		
        		
        			org.mybatis
        			mybatis-spring
        			1.2.0
        		
        
        		
        		
        			mysql
        			mysql-connector-java
        			5.1.29
        		
        	
        
        
        3、java代码-------src/main/java

        目录如下:


        (1)User.Java

        对应数据库中表的字段,放在src/main/java下的包com.lin.domain

        [java] view plain copy
        print ?
        1. package com.lin.domain;  
        2.   
        3. /** 
        4.  * User映射类 
        5.  *  
        6.  * @author linbingwen 
        7.  * @time 2015.5.15 
        8.  */  
        9. public class User {  
        10.     private Integer userId;  
        11.     private String userName;  
        12.     private String userPassword;  
        13.     private String userEmail;  
        14.   
        15.     public Integer getUserId() {  
        16.         return userId;  
        17.     }  
        18.   
        19.     public void setUserId(Integer userId) {  
        20.         this.userId = userId;  
        21.     }  
        22.   
        23.     public String getUserName() {  
        24.         return userName;  
        25.     }  
        26.   
        27.     public void setUserName(String userName) {  
        28.         this.userName = userName;  
        29.     }  
        30.   
        31.     public String getUserPassword() {  
        32.         return userPassword;  
        33.     }  
        34.   
        35.     public void setUserPassword(String userPassword) {  
        36.         this.userPassword = userPassword;  
        37.     }  
        38.   
        39.     public String getUserEmail() {  
        40.         return userEmail;  
        41.     }  
        42.   
        43.     public void setUserEmail(String userEmail) {  
        44.         this.userEmail = userEmail;  
        45.     }  
        46.   
        47.     @Override  
        48.     public String toString() {  
        49.         return "User [userId=" + userId + ", userName=" + userName  
        50.                 + ", userPassword=" + userPassword + ", userEmail=" + userEmail  
        51.                 + "]";  
        52.     }  
        53.       
        54. }  
        package com.lin.domain;
        
        /**
         * User映射类
         * 
         * @author linbingwen
         * @time 2015.5.15
         */
        public class User {
        	private Integer userId;
        	private String userName;
        	private String userPassword;
        	private String userEmail;
        
        	public Integer getUserId() {
        		return userId;
        	}
        
        	public void setUserId(Integer userId) {
        		this.userId = userId;
        	}
        
        	public String getUserName() {
        		return userName;
        	}
        
        	public void setUserName(String userName) {
        		this.userName = userName;
        	}
        
        	public String getUserPassword() {
        		return userPassword;
        	}
        
        	public void setUserPassword(String userPassword) {
        		this.userPassword = userPassword;
        	}
        
        	public String getUserEmail() {
        		return userEmail;
        	}
        
        	public void setUserEmail(String userEmail) {
        		this.userEmail = userEmail;
        	}
        
        	@Override
        	public String toString() {
        		return "User [userId=" + userId + ", userName=" + userName
        				+ ", userPassword=" + userPassword + ", userEmail=" + userEmail
        				+ "]";
        	}
        	
        }
        

        (2)UserDao.java

        Dao接口类,用来对应mapper文件。放在src/main/java下的包com.lin.dao,内容如下:

        [java] view plain copy
        print ?
        1. package com.lin.dao;  
        2.   
        3.   
        4. import com.lin.domain.User;  
        5.   
        6. /** 
        7.  * 功能概要:User的DAO类 
        8.  *  
        9.  * @author linbingwen 
        10.  * @since 2015年9月28日 
        11.  */  
        12. public interface UserDao {  
        13.     /** 
        14.      *  
        15.      * @author linbingwen 
        16.      * @since 2015年9月28日 
        17.      * @param userId 
        18.      * @return 
        19.      */  
        20.     public User selectUserById(Integer userId);  
        21.   
        22. }  
        package com.lin.dao;
        
        
        import com.lin.domain.User;
        
        /**
         * 功能概要:User的DAO类
         * 
         * @author linbingwen
         * @since 2015年9月28日
         */
        public interface UserDao {
        	/**
        	 * 
        	 * @author linbingwen
        	 * @since 2015年9月28日
        	 * @param userId
        	 * @return
        	 */
        	public User selectUserById(Integer userId);
        
        }
        

        (2)UserService.java和UserServiceImpl.java

        service接口类和实现类,放在src/main/java下的包com.lin.service,内容如下:

        UserService.java

        [java] view plain copy
        print ?
        1. package com.lin.service;  
        2.   
        3. import org.springframework.stereotype.Service;  
        4.   
        5. import com.lin.domain.User;  
        6.   
        7. /** 
        8.  * 功能概要:UserService接口类 
        9.  *  
        10.  * @author linbingwen 
        11.  * @since  2015年9月28日  
        12.  */  
        13. public interface UserService {  
        14.     User selectUserById(Integer userId);  
        15.   
        16. }  
        package com.lin.service;
        
        import org.springframework.stereotype.Service;
        
        import com.lin.domain.User;
        
        /**
         * 功能概要:UserService接口类
         * 
         * @author linbingwen
         * @since  2015年9月28日 
         */
        public interface UserService {
        	User selectUserById(Integer userId);
        
        }
        

        UserServiceImpl.java

        [java] view plain copy
        print ?
        1. package com.lin.service;  
        2.   
        3. import org.springframework.beans.factory.annotation.Autowired;  
        4. import org.springframework.stereotype.Service;  
        5.   
        6. import com.lin.dao.UserDao;  
        7. import com.lin.domain.User;  
        8.   
        9. /** 
        10.  * 功能概要:UserService实现类 
        11.  *  
        12.  * @author linbingwen 
        13.  * @since  2015年9月28日  
        14.  */  
        15. @Service  
        16. public class UserServiceImpl implements UserService{  
        17.     @Autowired  
        18.     private UserDao userDao;  
        19.   
        20.     public User selectUserById(Integer userId) {  
        21.         return userDao.selectUserById(userId);  
        22.           
        23.     }  
        24.   
        25. }  
        package com.lin.service;
        
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Service;
        
        import com.lin.dao.UserDao;
        import com.lin.domain.User;
        
        /**
         * 功能概要:UserService实现类
         * 
         * @author linbingwen
         * @since  2015年9月28日 
         */
        @Service
        public class UserServiceImpl implements UserService{
        	@Autowired
        	private UserDao userDao;
        
        	public User selectUserById(Integer userId) {
        		return userDao.selectUserById(userId);
        		
        	}
        
        }
        

        (4)mapper文件

        用来和dao文件对应,放在src/main/java下的com.lin.mapper包下

        [html] view plain copy
        print ?
        1. xml version="1.0" encoding="UTF-8"?>    
        2. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
        3. <mapper namespace="com.lin.dao.UserDao">  
        4.   
        5.     <resultMap id="BaseResultMap" type="com.lin.domain.User">  
        6.         <id column="USER_ID" property="userId" jdbcType="INTEGER" />  
        7.         <result column="USER_NAME" property="userName" jdbcType="CHAR" />  
        8.         <result column="USER_PASSWORD" property="userPassword" jdbcType="CHAR" />  
        9.         <result column="USER_EMAIL" property="userEmail" jdbcType="CHAR" />  
        10.     resultMap>  
        11.       
        12.     <select id="selectUserById" parameterType="int" resultMap="BaseResultMap">  
        13.         SELECT * FROM t_user WHERE USER_ID = #{userId}  
        14.     select>  
        15. mapper>  
          
        
        
        
        	
        		
        		
        		
        		
        	
        	
        	
        

        4、资源配置-------src/main/resources

        目录如下:


        (1)mybatis配置文件

        这里没有什么内容,因为都被放到application.xml中去了,放在src/main/resources下的mybatis文件夹下

        mybatis-config.xml内容如下:

        [html] view plain copy
        print ?
        1. xml version="1.0" encoding="UTF-8"?>    
        2. "http://mybatis.org/dtd/mybatis-3-config.dtd">    
        3. <configuration>      
        4. configuration>  
          
          
            
        

        (2)数据源配置jdbc.properties

        放在src/main/resources下的propertiesy文件夹下

        [html] view plain copy
        print ?
        1. jdbc_driverClassName=com.mysql.jdbc.Driver  
        2. jdbc_url=jdbc:mysql://localhost:3306/learning  
        3. jdbc_username=root  
        4. jdbc_password=christmas258@  
        jdbc_driverClassName=com.mysql.jdbc.Driver
        jdbc_url=jdbc:mysql://localhost:3306/learning
        jdbc_username=root
        jdbc_password=christmas258@
        (3)Spring配置

        这是最重要的:application.xml内容如下

        [html] view plain copy
        print ?
        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" xmlns:context="http://www.springframework.org/schema/context"  
        4.     xmlns:aop="http://www.springframework.org/schema/aop"  
        5.     xsi:schemaLocation="    
        6.            http://www.springframework.org/schema/beans    
        7.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
        8.            http://www.springframework.org/schema/aop    
        9.            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
        10.            http://www.springframework.org/schema/context    
        11.            http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
        12.        
        13.          
        14.      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        15.         <property name="locations">  
        16.             <list>  
        17.                <value>classpath:properties/*.propertiesvalue>  
        18.                   
        19.             list>  
        20.         property>  
        21.     bean>  
        22.       
        23.       
        24.   
        25.       
        26.     <bean id="dataSource"  
        27.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        28.           
        29.           
        30.         <property name="driverClassName">  
        31.             <value>${jdbc_driverClassName}value>  
        32.         property>  
        33.         <property name="url">  
        34.             <value>${jdbc_url}value>  
        35.         property>  
        36.         <property name="username">  
        37.             <value>${jdbc_username}value>  
        38.         property>  
        39.         <property name="password">  
        40.             <value>${jdbc_password}value>  
        41.         property>  
        42.     bean>  
        43.   
        44.       
        45.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        46.         <property name="basePackage"  
        47.             value="com.lin.dao" />  
        48.     bean>  
        49.   
        50.       
        51.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        52.         <property name="dataSource" ref="dataSource" />  
        53.         <property name="mapperLocations" value="classpath*:com/lin/mapper/**/*.xml"/>    
        54.         <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />  
        55.           
        56.     <context:component-scan base-package="com.lin.service" />  
        57.   
        58. beans>  
        
        
             
               
             
                
                    
                       classpath:properties/*.properties
                        
                    
                
            
            
            
        
        	
        	
        		
        		
        	   
        		
        			${jdbc_driverClassName}
        		
        		
        			${jdbc_url}
        		
        		
        			${jdbc_username}
        		
        		
        			${jdbc_password}
        		
        	
        
        	
        	
        		
        	
        
            
        	
        		
                  
        		
        		
        	
        
        	
        	
        
        
        

        (4)日志打印log4j.properties

        就放在src/main/resources

        [html] view plain copy
        print ?
        1. log4j.rootLogger=DEBUG,Console,Stdout  
        2.   
        3. #Console  
        4. log4j.appender.Console=org.apache.log4j.ConsoleAppender  
        5. log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
        6. log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n  
        7.   
        8. log4j.logger.java.sql.ResultSet=INFO  
        9. log4j.logger.org.apache=INFO  
        10. log4j.logger.java.sql.Connection=DEBUG  
        11. log4j.logger.java.sql.Statement=DEBUG  
        12. log4j.logger.java.sql.PreparedStatement=DEBUG   
        13.   
        14. log4j.appender.Stdout = org.apache.log4j.DailyRollingFileAppender    
        15. log4j.appender.Stdout.File = E://logs/log.log    
        16. log4j.appender.Stdout.Append = true    
        17. log4j.appender.Stdout.Threshold = DEBUG     
        18. log4j.appender.Stdout.layout = org.apache.log4j.PatternLayout    
        19. log4j.appender.Stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n    
        log4j.rootLogger=DEBUG,Console,Stdout
        
        #Console
        log4j.appender.Console=org.apache.log4j.ConsoleAppender
        log4j.appender.Console.layout=org.apache.log4j.PatternLayout
        log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
        
        log4j.logger.java.sql.ResultSet=INFO
        log4j.logger.org.apache=INFO
        log4j.logger.java.sql.Connection=DEBUG
        log4j.logger.java.sql.Statement=DEBUG
        log4j.logger.java.sql.PreparedStatement=DEBUG 
        
        log4j.appender.Stdout = org.apache.log4j.DailyRollingFileAppender  
        log4j.appender.Stdout.File = E://logs/log.log  
        log4j.appender.Stdout.Append = true  
        log4j.appender.Stdout.Threshold = DEBUG   
        log4j.appender.Stdout.layout = org.apache.log4j.PatternLayout  
        log4j.appender.Stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n  

        四、单元测试

        上面的配置完好,接下来就是测验成功

        整个目录 如下:


        (1)测试基类

        [java] view plain copy
        print ?
        1. package com.lin.baseTest;  
        2.   
        3. import org.junit.runner.RunWith;  
        4. import org.slf4j.Logger;  
        5. import org.slf4j.LoggerFactory;  
        6. import org.springframework.test.context.ContextConfiguration;  
        7. import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;  
        8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
        9.   
        10. /** 
        11.  * 功能概要: 
        12.  *  
        13.  * @author linbingwen 
        14.  * @since  2015年9月28日  
        15.  */  
        16. //指定bean注入的配置文件  
        17. @ContextConfiguration(locations = { "classpath:application.xml" })  
        18. //使用标准的JUnit @RunWith注释来告诉JUnit使用Spring TestRunner  
        19. @RunWith(SpringJUnit4ClassRunner.class)  
        20. public abstract class SpringTestCase extends AbstractJUnit4SpringContextTests{  
        21.     protected Logger logger = LoggerFactory.getLogger(getClass());  
        22. }  
        package com.lin.baseTest;
        
        import org.junit.runner.RunWith;
        import org.slf4j.Logger;
        import org.slf4j.LoggerFactory;
        import org.springframework.test.context.ContextConfiguration;
        import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
        import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
        
        /**
         * 功能概要:
         * 
         * @author linbingwen
         * @since  2015年9月28日 
         */
        //指定bean注入的配置文件
        @ContextConfiguration(locations = { "classpath:application.xml" })
        //使用标准的JUnit @RunWith注释来告诉JUnit使用Spring TestRunner
        @RunWith(SpringJUnit4ClassRunner.class)
        public abstract class SpringTestCase extends AbstractJUnit4SpringContextTests{
        	protected Logger logger = LoggerFactory.getLogger(getClass());
        }
        

        (2)测试类

        [java] view plain copy
        print ?
        1. package com.lin.service;  
        2.   
        3. import org.apache.log4j.Logger;  
        4. import org.junit.Test;  
        5. import org.springframework.beans.factory.annotation.Autowired;  
        6.   
        7. import com.lin.baseTest.SpringTestCase;  
        8. import com.lin.domain.User;  
        9.   
        10. /** 
        11.  * 功能概要:UserService单元测试 
        12.  *  
        13.  * @author linbingwen 
        14.  * @since  2015年9月28日  
        15.  */  
        16. public class UserServiceTest extends SpringTestCase {  
        17.     @Autowired  
        18.     private UserService userService;  
        19.     Logger logger = Logger.getLogger(UserServiceTest.class);  
        20.       
        21.     @Test  
        22.     public void selectUserByIdTest(){  
        23.         User user = userService.selectUserById(10);  
        24.         logger.debug("查找结果" + user);  
        25.     }  
        26.       
        27.   
        28. }  
        package com.lin.service;
        
        import org.apache.log4j.Logger;
        import org.junit.Test;
        import org.springframework.beans.factory.annotation.Autowired;
        
        import com.lin.baseTest.SpringTestCase;
        import com.lin.domain.User;
        
        /**
         * 功能概要:UserService单元测试
         * 
         * @author linbingwen
         * @since  2015年9月28日 
         */
        public class UserServiceTest extends SpringTestCase	{
        	@Autowired
        	private UserService userService;
        	Logger logger = Logger.getLogger(UserServiceTest.class);
        	
        	@Test
        	public void selectUserByIdTest(){
        		User user = userService.selectUserById(10);
                logger.debug("查找结果" + user);
        	}
        	
        
        }
        
        选中selectUserByIdTest,然后右键如下运行


        输出结果:


        重要打印的结果


        这里

        [plain] view plain copy
        print ?
        1. 2015-09-28 15:20:15,129 [main] DEBUG [com.lin.dao.UserDao.selectUserById] - ==>  Preparing: SELECT * FROM t_user WHERE USER_ID = ?   
        2. 2015-09-28 15:20:15,160 [main] DEBUG [com.lin.dao.UserDao.selectUserById] - ==> Parameters: 10(Integer)  
        3. 2015-09-28 15:20:15,160 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6b64bff9]  
        4. 2015-09-28 15:20:15,160 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource  
        5. 2015-09-28 15:20:15,160 [main] DEBUG [com.lin.service.UserServiceTest] - 查找结果User [userId=10, userName=apple, userPassword=uih6, [email protected]]  
        2015-09-28 15:20:15,129 [main] DEBUG [com.lin.dao.UserDao.selectUserById] - ==>  Preparing: SELECT * FROM t_user WHERE USER_ID = ? 
        2015-09-28 15:20:15,160 [main] DEBUG [com.lin.dao.UserDao.selectUserById] - ==> Parameters: 10(Integer)
        2015-09-28 15:20:15,160 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6b64bff9]
        2015-09-28 15:20:15,160 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
        2015-09-28 15:20:15,160 [main] DEBUG [com.lin.service.UserServiceTest] - 查找结果User [userId=10, userName=apple, userPassword=uih6, [email protected]]

        数据库:


        程序成功运行,并且结果正确!

        到这里配置好spring+mybatis+mysql!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        =====================================================
        这里开始添加spirngMVC,并转换maven工程为web项目

        五、转换web工程

        接上面的工程接着说,此时要加spingMVC之前,工程得再稍微转换下
        此时webapp下的结果还没有显示出来,因为此时我们还没有配置此的项目为web项目

        再次进去Properties配置,如下图所示:如果没有显示如下,那就先把dynamic web module的√给去掉,然后重新再打开 这个
        进入,然后输入如下内容

        确定OK之后,webapp下面就会自动生成一些文件,如下





        六、配置SpringMVC

        (1)修改POM文件,增加了一此内容,新的POM文件如下:
        [html] view plain copy
        print ?
        1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
        3.     <modelVersion>4.0.0modelVersion>  
        4.     <groupId>com.lingroupId>  
        5.     <artifactId>ssm_projectartifactId>  
        6.     <version>0.0.1-SNAPSHOTversion>  
        7.     <packaging>warpackaging>  
        8.     <properties>  
        9.           
        10.         <spring.version>3.2.8.RELEASEspring.version>  
        11.           
        12.         <slf4j.version>1.6.6slf4j.version>  
        13.         <log4j.version>1.2.12log4j.version>  
        14.           
        15.         <junit.version>4.10junit.version>  
        16.           
        17.         <mybatis.version>3.2.1mybatis.version>  
        18.     properties>  
        19.   
        20.     <dependencies>  
        21.           
        22.         <dependency>  
        23.             <groupId>org.springframeworkgroupId>  
        24.             <artifactId>spring-coreartifactId>  
        25.             <version>${spring.version}version>  
        26.         dependency>  
        27.         <dependency>  
        28.             <groupId>org.springframeworkgroupId>  
        29.             <artifactId>spring-webmvcartifactId>  
        30.             <version>${spring.version}version>  
        31.         dependency>  
        32.         <dependency>  
        33.             <groupId>org.springframeworkgroupId>  
        34.             <artifactId>spring-contextartifactId>  
        35.             <version>${spring.version}version>  
        36.         dependency>  
        37.         <dependency>  
        38.             <groupId>org.springframeworkgroupId>  
        39.             <artifactId>spring-context-supportartifactId>  
        40.             <version>${spring.version}version>  
        41.         dependency>  
        42.         <dependency>  
        43.             <groupId>org.springframeworkgroupId>  
        44.             <artifactId>spring-aopartifactId>  
        45.             <version>${spring.version}version>  
        46.         dependency>  
        47.         <dependency>  
        48.             <groupId>org.springframeworkgroupId>  
        49.             <artifactId>spring-aspectsartifactId>  
        50.             <version>${spring.version}version>  
        51.         dependency>  
        52.         <dependency>  
        53.             <groupId>org.springframeworkgroupId>  
        54.             <artifactId>spring-txartifactId>  
        55.             <version>${spring.version}version>  
        56.         dependency>  
        57.         <dependency>  
        58.             <groupId>org.springframeworkgroupId>  
        59.             <artifactId>spring-jdbcartifactId>  
        60.             <version>${spring.version}version>  
        61.         dependency>  
        62.         <dependency>  
        63.             <groupId>org.springframeworkgroupId>  
        64.             <artifactId>spring-webartifactId>  
        65.             <version>${spring.version}version>  
        66.         dependency>  
        67.   
        68.           
        69.         <dependency>  
        70.             <groupId>junitgroupId>  
        71.             <artifactId>junitartifactId>  
        72.             <version>${junit.version}version>  
        73.             <scope>testscope>  
        74.         dependency>  
        75.   
        76.           
        77.           
        78.         <dependency>  
        79.             <groupId>log4jgroupId>  
        80.             <artifactId>log4jartifactId>  
        81.             <version>${log4j.version}version>  
        82.         dependency>  
        83.         <dependency>  
        84.             <groupId>org.slf4jgroupId>  
        85.             <artifactId>slf4j-apiartifactId>  
        86.             <version>${slf4j.version}version>  
        87.         dependency>  
        88.         <dependency>  
        89.             <groupId>org.slf4jgroupId>  
        90.             <artifactId>slf4j-log4j12artifactId>  
        91.             <version>${slf4j.version}version>  
        92.         dependency>  
        93.           
        94.   
        95.           
        96.         <dependency>  
        97.             <groupId>org.springframeworkgroupId>  
        98.             <artifactId>spring-testartifactId>  
        99.             <version>${spring.version}version>  
        100.             <scope>testscope>  
        101.         dependency>  
        102.   
        103.           
        104.         <dependency>  
        105.             <groupId>org.mybatisgroupId>  
        106.             <artifactId>mybatisartifactId>  
        107.             <version>${mybatis.version}version>  
        108.         dependency>  
        109.   
        110.           
        111.         <dependency>  
        112.             <groupId>org.mybatisgroupId>  
        113.             <artifactId>mybatis-springartifactId>  
        114.             <version>1.2.0version>  
        115.         dependency>  
        116.   
        117.           
        118.         <dependency>  
        119.             <groupId>mysqlgroupId>  
        120.             <artifactId>mysql-connector-javaartifactId>  
        121.             <version>5.1.29version>  
        122.         dependency>  
        123.           
        124.               
        125.         <dependency>  
        126.             <groupId>javaxgroupId>  
        127.             <artifactId>javaee-apiartifactId>  
        128.             <version>6.0version>  
        129.             <scope>providedscope>  
        130.         dependency>  
        131.   
        132.           
        133.         <dependency>  
        134.             <groupId>javaxgroupId>  
        135.             <artifactId>javaee-web-apiartifactId>  
        136.             <version>6.0version>  
        137.             <scope>providedscope>  
        138.         dependency>  
        139.     dependencies>  
        140.   
        141. project>  
        
        	4.0.0
        	com.lin
        	ssm_project
        	0.0.1-SNAPSHOT
        	war
        	
        		
        		3.2.8.RELEASE
        		
        		1.6.6
        		1.2.12
        		
        		4.10
        		
        		3.2.1
        	
        
        	
        		
        		
        			org.springframework
        			spring-core
        			${spring.version}
        		
        		
        			org.springframework
        			spring-webmvc
        			${spring.version}
        		
        		
        			org.springframework
        			spring-context
        			${spring.version}
        		
        		
        			org.springframework
        			spring-context-support
        			${spring.version}
        		
        		
        			org.springframework
        			spring-aop
        			${spring.version}
        		
        		
        			org.springframework
        			spring-aspects
        			${spring.version}
        		
        		
        			org.springframework
        			spring-tx
        			${spring.version}
        		
        		
        			org.springframework
        			spring-jdbc
        			${spring.version}
        		
        		
        			org.springframework
        			spring-web
        			${spring.version}
        		
        
        		
        		
        			junit
        			junit
        			${junit.version}
        			test
        		
        
        		
        		
        		
        			log4j
        			log4j
        			${log4j.version}
        		
        		
        			org.slf4j
        			slf4j-api
        			${slf4j.version}
        		
        		
        			org.slf4j
        			slf4j-log4j12
        			${slf4j.version}
        		
        		
        
        		
        		
        			org.springframework
        			spring-test
        			${spring.version}
        			test
        		
        
        		
        		
        			org.mybatis
        			mybatis
        			${mybatis.version}
        		
        
        		
        		
        			org.mybatis
        			mybatis-spring
        			1.2.0
        		
        
        		
        		
        			mysql
        			mysql-connector-java
        			5.1.29
        		
        		
                    
        		
        			javax
        			javaee-api
        			6.0
        			provided
        		
        
        		
        		
        			javax
        			javaee-web-api
        			6.0
        			provided
        		
        	
        
        
        其实也就增加了下面两个
        [html] view plain copy
        print ?
        1.           
        2. <dependency>  
        3.     <groupId>javaxgroupId>  
        4.     <artifactId>javaee-apiartifactId>  
        5.     <version>6.0version>  
        6.     <scope>providedscope>  
        7. dependency>  
        8.   
        9.   
        10. <dependency>  
        11.     <groupId>javaxgroupId>  
        12.     <artifactId>javaee-web-apiartifactId>  
        13.     <version>6.0version>  
        14.     <scope>providedscope>  
        15. dependency>  
                  
        		
        			javax
        			javaee-api
        			6.0
        			provided
        		
        
        		
        		
        			javax
        			javaee-web-api
        			6.0
        			provided
        		

        (2) 在src/main/resource中添加springmvc文件夹,然后添加文件spring-mvc.xml,内容如下:
        [html] view plain copy
        print ?
        1. xml version="1.0" encoding="UTF-8"?>  
        2. <beans xmlns="http://www.springframework.org/schema/beans"  
        3.   xmlns:p="http://www.springframework.org/schema/p"  
        4.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        5.   xmlns:context="http://www.springframework.org/schema/context"  
        6.   xmlns:mvc="http://www.springframework.org/schema/mvc"  
        7.   xsi:schemaLocation="  
        8.     http://www.springframework.org/schema/beans  
        9.     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
        10.     http://www.springframework.org/schema/context  
        11.     http://www.springframework.org/schema/context/spring-context-3.2.xsd  
        12.     http://www.springframework.org/schema/mvc  
        13.     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">  
        14.        
        15.       
        16.    <context:component-scan base-package="com.lin.controller"/>    
        17.        
        18.      
        19.      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
        20.       p:prefix="/WEB-INF/view/" p:suffix=".jsp"/>  
        21. beans>  
        
        
             
            
             
             
           
             
        
        (3)配置web,xml
        目录如下 :

        这里是关键,web.xml是用来起动sping、spingMVC
        放在src/main/webapp/WEB-INF下。内容如下:
        [html] view plain copy
        print ?
        1. xml version="1.0" encoding="UTF-8"?>  
        2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
        4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
        5.     id="WebApp_ID" version="2.5">  
        6.     <display-name>Archetype Created Web Applicationdisplay-name>  
        7.      
        8.     <welcome-file-list>  
        9.         <welcome-file>index.jspwelcome-file>  
        10.     welcome-file-list>  
        11.    
        12.       
        13.     <context-param>  
        14.         <param-name>contextConfigLocationparam-name>  
        15.         <param-value>classpath:application.xmlparam-value>  
        16.     context-param>  
        17.       
        18.     <context-param>  
        19.         <param-name>webAppRootKeyparam-name>  
        20.         <param-value>springmvc.rootparam-value>  
        21.     context-param>  
        22.    
        23.    
        24.       
        25.     <filter>  
        26.         <filter-name>SpringEncodingFilterfilter-name>  
        27.         <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>  
        28.         <init-param>  
        29.             <param-name>encodingparam-name>  
        30.             <param-value>UTF-8param-value>  
        31.         init-param>  
        32.         <init-param>  
        33.             <param-name>forceEncodingparam-name>  
        34.             <param-value>trueparam-value>  
        35.         init-param>  
        36.     filter>  
        37.     <filter-mapping>  
        38.         <filter-name>SpringEncodingFilterfilter-name>  
        39.         <url-pattern>/*url-pattern>  
        40.     filter-mapping>  
        41.    
        42.       
        43.     <context-param>  
        44.           
        45.         <param-name>log4jConfigLocationparam-name>  
        46.         <param-value>classpath:log4j.propertiesparam-value>  
        47.     context-param>  
        48.     <context-param>  
        49.           
        50.         <param-name>log4jRefreshIntervalparam-name>  
        51.         <param-value>6000param-value>  
        52.     context-param>  
        53.     <listener>  
        54.         <listener-class>org.springframework.web.util.Log4jConfigListenerlistener-class>  
        55.     listener>  
        56.    
        57.     <listener>  
        58.         <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>  
        59.     listener>  
        60.    
        61.       
        62.     <servlet>  
        63.         <servlet-name>dispatcherServletservlet-name>  
        64.         <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>  
        65.         <init-param>  
        66.             <param-name>contextConfigLocationparam-name>  
        67.               
        68.             <param-value>classpath:springmvc/spring-mvc.xmlparam-value>  
        69.         init-param>  
        70.         <load-on-startup>1load-on-startup>  
        71.     servlet>  
        72.       
        73.     <servlet-mapping>  
        74.         <servlet-name>dispatcherServletservlet-name>  
        75.         <url-pattern>/url-pattern>  
        76.     servlet-mapping>  
        77.    
        78.       
        79.     <error-page>  
        80.           
        81.         <error-code>404error-code>  
        82.         <location>/WEB-INF/errorpage/404.jsplocation>  
        83.     error-page>  
        84.     <error-page>  
        85.           
        86.         <error-code>405error-code>  
        87.         <location>/WEB-INF/errorpage/405.jsplocation>  
        88.     error-page>  
        89.     <error-page>  
        90.           
        91.         <error-code>500error-code>  
        92.         <location>/WEB-INF/errorpage/500.jsplocation>  
        93.     error-page>  
        94. web-app>  
        
        
            Archetype Created Web Application
           
        	
        		index.jsp
        	
         
            
            
                contextConfigLocation
                classpath:application.xml
            
            
            
                webAppRootKey
                springmvc.root
            
         
         
            
            
                SpringEncodingFilter
                org.springframework.web.filter.CharacterEncodingFilter
                
                    encoding
                    UTF-8
                
                
                    forceEncoding
                    true
                
            
            
                SpringEncodingFilter
                /*
            
         
            
            
                
                log4jConfigLocation
                classpath:log4j.properties
            
            
                
                log4jRefreshInterval
                6000
            
            
                org.springframework.web.util.Log4jConfigListener
            
         
            
                org.springframework.web.context.ContextLoaderListener
            
         
            
        	
        		dispatcherServlet
        		org.springframework.web.servlet.DispatcherServlet
        		
        			contextConfigLocation
        			
        			classpath:springmvc/spring-mvc.xml
        		
        		1
        	
        	
        	
        		dispatcherServlet
        		/
        	
         
            
            
                
                404
                /WEB-INF/errorpage/404.jsp
            
            
                
                405
                /WEB-INF/errorpage/405.jsp
            
            
                
                500
                /WEB-INF/errorpage/500.jsp
            
        

        (4)添加index.jsp
        在src/main/webapp/WEB-INF下新建一个文件夹view,添加一个index.jsp,内容如下:
        [html] view plain copy
        print ?
        1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
        2. <html>  
        3. <body>  
        4. <h2>Hello World!h2>  
        5.  ${user.userId}<br>  
        6.  ${user.userName}<br>  
        7.  ${user.userPassword}<br>  
        8.  ${user.userEmail}<br>  
        9. body>  
        10. html>  
        <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
        
        
        

        Hello World!

        ${user.userId}
        ${user.userName}
        ${user.userPassword}
        ${user.userEmail}

        (5)最后就是controller的书写了

        在src/main/java下新建一个包com.lin.controller.然后新建一个类UserController.java,其内容如下:

        [java] view plain copy
        print ?
        1. package com.lin.controller;  
        2.   
        3. import javax.annotation.Resource;  
        4.   
        5. import org.springframework.stereotype.Controller;  
        6. import org.springframework.web.bind.annotation.RequestMapping;  
        7. import org.springframework.web.servlet.ModelAndView;  
        8.   
        9. import com.lin.domain.User;  
        10. import com.lin.service.UserService;  
        11.   
        12. /** 
        13.  * 功能概要:UserController 
        14.  *  
        15.  * @author linbingwen 
        16.  * @since  2015年9月28日  
        17.  */  
        18. @Controller  
        19. public class UserController {  
        20.     @Resource  
        21.     private UserService userService;  
        22.       
        23.     @RequestMapping("/")    
        24.     public ModelAndView getIndex(){      
        25.         ModelAndView mav = new ModelAndView("index");   
        26.         User user = userService.selectUserById(1);  
        27.         mav.addObject("user", user);   
        28.         return mav;    
        29.     }    
        30. }  
        package com.lin.controller;
        
        import javax.annotation.Resource;
        
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.servlet.ModelAndView;
        
        import com.lin.domain.User;
        import com.lin.service.UserService;
        
        /**
         * 功能概要:UserController
         * 
         * @author linbingwen
         * @since  2015年9月28日 
         */
        @Controller
        public class UserController {
        	@Resource
        	private UserService userService;
        	
        	@RequestMapping("/")  
            public ModelAndView getIndex(){    
        		ModelAndView mav = new ModelAndView("index"); 
        		User user = userService.selectUserById(1);
        	    mav.addObject("user", user); 
                return mav;  
            }  
        }
        

        (6)最后的运行!
        最后,就是成果了,已经全部都配置好了。我们可以开始以web工程运行了!

        然后一路next


        接下来控制台就会打印日志出来了!如下

        打印浏览器,输入网址:http://localhost:8088/ssm_project/

        本文工程免费下载

        38
        2
         
         

        我的同类文章

        Mybatis(12) SpringMVC(8) Spring(24)
        http://blog.csdn.net
        • Shrio登陆验证实例详细解读2015-12-06阅读6131
        • Spring+Mybatis+Maven+MySql搭建实例2015-09-29阅读4582
        • Mybatis自动生成代码2015-07-23阅读4061
        • Mybatis+Spring整合创建Web项目2015-05-15阅读4891
        • Mybatis关联查询一对一和一对多的实现2015-05-13阅读8715
        • Spring+Mybatis+SpringMVC后台与前台分页展示实例(附工程)2015-10-27阅读15513
        • Ajax+SpringMVC+Spring+Mybatis+MySql+js用户注册实例2015-07-23阅读7848
        • MyBatis动态Sql语句2015-07-09阅读5705
        • Mybatis基于注解实现增删查改和多参数列表查询2015-05-15阅读5037
        • Mybatis多参数查询与列表查询不同方式实现2015-05-13阅读4833
        更多文章

        你可能感兴趣的:(ssm,spring,springmvc,mybatis,maven)