• eclipse + maven 搭建springMVC+Spring+mybatis 系统


    首先需要下载maven 安装maven插件。自行百度。

    1: 创建maven系统 http://huxiaoheihei.iteye.com/blog/1766986

    2:添加pom依赖:

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.alipay.demo</groupId>
      <artifactId>demo</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      
     	 <!-- 设置 Spring 的版本 --> 
        <properties> 
            <org.springframework.version>3.0.0.RELEASE</org.springframework.version> 
        </properties>
    
        <dependencies> 
    <!--         <!– 此处开始就是Spring 所有的jar了,spring3.0的jar包拆分了,所以很多 –>  -->
    <!--         <!– Core utilities used by other modules. Define this if you use Spring  -->
    <!--             Utility APIs (org.springframework.core.*/org.springframework.util.*) –>  -->
            <dependency> 
                <groupId>org.springframework</groupId> 
                <artifactId>spring-core</artifactId> 
                <version>${org.springframework.version}</version> 
            </dependency>
    
    <!--         <!– Expression Language (depends on spring-core) Define this if you use  -->
    <!--             Spring Expression APIs (org.springframework.expression.*) –>  -->
            <dependency> 
                <groupId>org.springframework</groupId> 
                <artifactId>spring-expression</artifactId> 
                <version>${org.springframework.version}</version> 
            </dependency>
    
    <!--         <!– Bean Factory and JavaBeans utilities (depends on spring-core) Define  -->
    <!--             this if you use Spring Bean APIs (org.springframework.beans.*) –>  -->
            <dependency> 
                <groupId>org.springframework</groupId> 
                <artifactId>spring-beans</artifactId> 
                <version>${org.springframework.version}</version> 
            </dependency>
    
    <!--         <!– Aspect Oriented Programming (AOP) Framework (depends on spring-core,  -->
    <!--             spring-beans) Define this if you use Spring AOP APIs (org.springframework.aop.*) –>  -->
            <dependency> 
                <groupId>org.springframework</groupId> 
                <artifactId>spring-aop</artifactId> 
                <version>${org.springframework.version}</version> 
            </dependency>
    
    <!--         <!– Application Context (depends on spring-core, spring-expression, spring-aop,  -->
    <!--             spring-beans) This is the central artifact for Spring’s Dependency Injection  -->
    <!--             Container and is generally always defined –>  -->
            <dependency> 
                <groupId>org.springframework</groupId> 
                <artifactId>spring-context</artifactId> 
                <version>${org.springframework.version}</version> 
            </dependency>
    
    <!--         <!– Various Application Context utilities, including EhCache, JavaMail,  -->
    <!--             Quartz, and Freemarker integration Define this if you need any of these integrations –>  -->
            <dependency> 
                <groupId>org.springframework</groupId> 
                <artifactId>spring-context-support</artifactId> 
                <version>${org.springframework.version}</version> 
            </dependency>
    
    <!--         <!– Transaction Management Abstraction (depends on spring-core, spring-beans,  -->
    <!--             spring-aop, spring-context) Define this if you use Spring Transactions or  -->
    <!--             DAO Exception Hierarchy (org.springframework.transaction.*/org.springframework.dao.*) –>  -->
            <dependency> 
                <groupId>org.springframework</groupId> 
                <artifactId>spring-tx</artifactId> 
                <version>${org.springframework.version}</version> 
            </dependency>
    
    <!--         <!– JDBC Data Access Library (depends on spring-core, spring-beans, spring-context,  -->
    <!--             spring-tx) Define this if you use Spring’s JdbcTemplate API (org.springframework.jdbc.*) –>  -->
            <dependency> 
                <groupId>org.springframework</groupId> 
                <artifactId>spring-jdbc</artifactId> 
                <version>${org.springframework.version}</version> 
            </dependency>
    
    <!--         <!– Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA,  -->
    <!--             and iBatis. (depends on spring-core, spring-beans, spring-context, spring-tx)  -->
    <!--             Define this if you need ORM (org.springframework.orm.*) –>  -->
            <dependency> 
                <groupId>org.springframework</groupId> 
                <artifactId>spring-orm</artifactId> 
                <version>${org.springframework.version}</version> 
            </dependency>
    
    <!--         <!– Object-to-XML Mapping (OXM) abstraction and integration with JAXB,  -->
    <!--             JiBX, Castor, XStream, and XML Beans. (depends on spring-core, spring-beans,  -->
    <!--             spring-context) Define this if you need OXM (org.springframework.oxm.*) –>  -->
            <dependency> 
                <groupId>org.springframework</groupId> 
                <artifactId>spring-oxm</artifactId> 
                <version>${org.springframework.version}</version> 
            </dependency>
    
    <!--         <!– Web application development utilities applicable to both Servlet and  -->
    <!--             Portlet Environments (depends on spring-core, spring-beans, spring-context)  -->
    <!--             Define this if you use Spring MVC, or wish to use Struts, JSF, or another  -->
    <!--             web framework with Spring (org.springframework.web.*) –>  -->
            <dependency> 
                <groupId>org.springframework</groupId> 
                <artifactId>spring-web</artifactId> 
                <version>${org.springframework.version}</version> 
            </dependency>
    
    <!--         <!– Spring MVC for Servlet Environments (depends on spring-core, spring-beans,  -->
    <!--             spring-context, spring-web) Define this if you use Spring MVC with a Servlet  -->
    <!--             Container such as Apache Tomcat (org.springframework.web.servlet.*) –>  -->
            <dependency> 
                <groupId>org.springframework</groupId> 
                <artifactId>spring-webmvc</artifactId> 
                <version>${org.springframework.version}</version> 
            </dependency>
    
    <!--         <!– Spring MVC for Portlet Environments (depends on spring-core, spring-beans,  -->
    <!--             spring-context, spring-web) Define this if you use Spring MVC with a Portlet  -->
    <!--             Container (org.springframework.web.portlet.*) –>  -->
            <dependency> 
                <groupId>org.springframework</groupId> 
                <artifactId>spring-webmvc-portlet</artifactId> 
                <version>${org.springframework.version}</version> 
            </dependency>
    
    <!--         <!– Support for testing Spring applications with tools such as JUnit and  -->
    <!--             TestNG This artifact is generally always defined with a ‘test’ scope for  -->
    <!--             the integration testing framework and unit testing stubs –>  -->
            <dependency> 
                <groupId>org.springframework</groupId> 
                <artifactId>spring-test</artifactId> 
                <version>${org.springframework.version}</version> 
                <scope>test</scope> 
            </dependency> 
    <!--         <!– 上面的都是Spring的jar包,可以选择性的添加 –>  -->
    
    
    <!--         <!– tomcat servlet开发包 –>  -->
            <dependency> 
                <groupId>javax.servlet</groupId> 
                <artifactId>jstl</artifactId> 
                <version>1.2</version> 
            </dependency> 
    <!--         <!– JSTL标签库 –>  -->
            <dependency> 
                <groupId>javax.servlet</groupId> 
                <artifactId>servlet-api</artifactId> 
                <version>2.5</version> 
            </dependency> 
    <!--         <!– mysql的数据库驱动包 –>  -->
            <dependency> 
                <groupId>mysql</groupId> 
                <artifactId>mysql-connector-java</artifactId> 
                <version>5.1.21</version> 
            </dependency> 
    <!--         <!– 日志打印 log4j包 –>  -->
            <dependency> 
                <groupId>log4j</groupId> 
                <artifactId>log4j</artifactId> 
                <version>1.2.14</version> 
                <scope>runtime</scope> 
            </dependency> 
    <!--         <!– 下面两个包 commons-dbcp,commons-pool 是配置数据源的包–>  -->
            <dependency> 
                <groupId>commons-dbcp</groupId> 
                <artifactId>commons-dbcp</artifactId> 
                <version>1.4</version> 
            </dependency> 
            <dependency> 
                <groupId>commons-pool</groupId> 
                <artifactId>commons-pool</artifactId> 
                <version>1.4</version> 
            </dependency>
    
    <!--         <!– 日志记录依赖包,很多都依赖此包,像log4j,json-lib等等 –>  -->
            <dependency> 
                <groupId>commons-logging</groupId> 
                <artifactId>commons-logging-api</artifactId> 
                <version>1.1</version> 
            </dependency> 
    <!--         <!– Spring 文件上传的包 –>  -->
            <dependency> 
                <groupId>commons-fileupload</groupId> 
                <artifactId>commons-fileupload</artifactId> 
                <version>1.2.2</version> 
            </dependency> 
    <!--         <!– Spring 文件上传的依赖包 –>  -->
            <dependency> 
                <groupId>org.apache.commons</groupId> 
                <artifactId>commons-io</artifactId> 
                <version>1.3.2</version> 
            </dependency> 
    <!--         <!– dom4j 解析 XML文件的包 –>  -->
            <dependency> 
                <groupId>dom4j</groupId> 
                <artifactId>dom4j</artifactId> 
                <version>1.6.1</version> 
            </dependency> 
    <!--         <!– 下面的三个包是在配置事务的时候用到的 spring的依赖包  –>  -->
            <dependency> 
                <groupId>org.aspectj</groupId> 
                <artifactId>aspectjweaver</artifactId> 
                <version>1.7.0</version> 
            </dependency> 
            <dependency> 
                <groupId>aopalliance</groupId> 
                <artifactId>aopalliance</artifactId> 
                <version>1.0</version> 
            </dependency> 
            <dependency> 
                <groupId>cglib</groupId> 
                <artifactId>cglib-nodep</artifactId> 
                <version>2.2.2</version> 
            </dependency>
    
    <!--         <!– JSON lib 开发包 以及它的依赖包 –>  -->
            <dependency> 
                <groupId>net.sf.json-lib</groupId> 
                <artifactId>json-lib</artifactId> 
                <classifier>jdk15</classifier> 
                <version>2.4</version> 
            </dependency> 
            <dependency> 
                <groupId>commons-beanutils</groupId> 
                <artifactId>commons-beanutils</artifactId> 
                <version>1.8.3</version> 
            </dependency> 
            <dependency> 
                <groupId>commons-collections</groupId> 
                <artifactId>commons-collections</artifactId> 
                <version>3.2.1</version> 
            </dependency> 
            <dependency> 
                <groupId>commons-lang</groupId> 
                <artifactId>commons-lang</artifactId> 
                <version>2.6</version> 
            </dependency> 
            <dependency> 
                <groupId>net.sf.ezmorph</groupId> 
                <artifactId>ezmorph</artifactId> 
                <version>1.0.5</version> 
            </dependency> 
    <!--         <!– junit 测试包 –>  -->
            <dependency> 
                <groupId>junit</groupId> 
                <artifactId>junit</artifactId> 
                <version>3.8.1</version> 
            </dependency> 
            
            <dependency>
    	    	<groupId>org.mybatis</groupId>
    		    <artifactId>mybatis</artifactId>
    		    <version>3.2.2</version>
    		</dependency>
    		
    		<dependency>
    		    <groupId>org.mybatis</groupId>
    		    <artifactId>mybatis-spring</artifactId>
    		    <version>1.2.0</version>
    		</dependency>
    		
    		<dependency>
    		    <groupId>javax.annotation</groupId>
    		    <artifactId>javax.annotation-api</artifactId>
    		    <version>1.2</version>
    		</dependency>
      </dependencies>
      <build>
         <finalName>demo</finalName>
         <plugins>
    	    <plugin>
    	       <groupId>org.mortbay.jetty</groupId>
    	       <artifactId>maven-jetty-plugin</artifactId>
    	       <version>6.1.10</version>
    	       <configuration>
    	               <scanIntervalSeconds>10</scanIntervalSeconds>
    	               <stopKey>foo</stopKey>
    	               <stopPort>9999</stopPort>
    	       </configuration>
    	       <executions>
    	               <execution>
    	                       <id>start-jetty</id>
    	                       <phase>pre-integration-test</phase>
    	                       <goals>
    	                               <goal>run</goal>
    	                       </goals>
    	                       <configuration>
    	                               <scanIntervalSeconds>0</scanIntervalSeconds>
    	                               <daemon>true</daemon>
    	                       </configuration>
    	               </execution>
    	               <execution>
    	                       <id>stop-jetty</id>
    	                       <phase>post-integration-test</phase>
    	                       <goals>
    	                               <goal>stop</goal>
    	                       </goals>
    	               </execution>
    	       </executions>
    		</plugin>
    	</plugins>
       </build>
    
      
    </project>
    

      

    3: 配置web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    		http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
    	<!--  <!– Spring context startup Spring的初始化–>  -->
        <context-param> 
            <param-name>contextConfigLocation</param-name> 
            <param-value>
            classpath:/applicationContext.xml 
            </param-value> 
        </context-param> 
        <listener> 
            <listener-class> 
            org.springframework.web.context.ContextLoaderListener 
            </listener-class> 
        </listener>
    
    	<!--     <!– Spring character filter 处理乱码–>  -->
        <filter> 
            <filter-name>encodingFilter</filter-name> 
            <filter-class> 
            org.springframework.web.filter.CharacterEncodingFilter 
            </filter-class> 
            <init-param> 
                <param-name>encoding</param-name> 
                <param-value>UTF-8</param-value> 
            </init-param> 
        </filter> 
        <filter-mapping> 
            <filter-name>encodingFilter</filter-name> 
            <url-pattern>/*</url-pattern> 
        </filter-mapping>
    
    	<!--     <!– Configuration of Log4j log4j的配置 –>  -->
        <context-param> 
            <param-name>log4jConfigLocation</param-name> 
            <param-value>
            classpath:/log4j.properties 
            </param-value> 
        </context-param> 
        <context-param> 
            <param-name>log4jRefreshInterval</param-name> 
            <param-value>60000</param-value> 
        </context-param> 
        <listener> 
            <listener-class> 
            org.springframework.web.util.Log4jConfigListener 
            </listener-class> 
        </listener>
    
    	<!--     <!– Spring MVC Configuration Spring MVC的配置–>  -->
        <servlet> 
            <servlet-name>dispatcher</servlet-name> 
            <servlet-class> 
            	org.springframework.web.servlet.DispatcherServlet 
            </servlet-class> 
            <init-param> 
               <param-name>contextConfigLocation</param-name> 
               <param-value>	<!-- dispatcher-servlet.xml文件路径  -->
               classpath:dispatcher-servlet.xml 
               </param-value> 
              </init-param> 
            <load-on-startup>1</load-on-startup> 
        </servlet> 
        <servlet-mapping> 
            <servlet-name>dispatcher</servlet-name> 
            <url-pattern>*.htm</url-pattern> 
        </servlet-mapping> 
    </web-app>
    

      

    4: log4j.properties jdbc.properties

     
    log4j.rootLogger=DEBUG,CONSOLE,FILEOUT 
    log4j.addivity.org.apache=true
    
    # CONSOLE 
    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 
    log4j.appender.Threshold=DEBUG 
    log4j.appender.CONSOLE.Target=System.out 
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 
    #log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d u2013 %c -%-4r [%t] %-5p %c %x u2013 %m%n 
    log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} :%m%n
    
    # 
    # FILEOUT 
    log4j.appender.FILEOUT=org.apache.log4j.RollingFileAppender 
    log4j.appender.FILEOUT.File=${catalina.home}\file.log 
    log4j.appender.fileout.MaxFileSize=100000KB 
    # default is trueuFF0Cappend to the file; if false, the replace the log file whenever restart system 
    log4j.appender.FILEOUT.Append=true 
    #RollingFileAppenderu6CA1u6709DatePatternu8FD9u4E2Au5C5Eu6027 
    log4j.appender.FILEOUT.layout=org.apache.log4j.PatternLayout 
    #log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d u2013 %c -%-4r [%t] %-5p %c %x u2013 %m%n 
    log4j.appender.FILEOUT.layout.ConversionPattern=[%-5p]_%d{yyyy-MM-dd HH:mm:ss} :%m%n
    

      

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/test
    user=root
    password=123
    

      

    5: Spring mvc 配置 

    applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:context="http://www.springframework.org/schema/context"
                 xmlns:aop="http://www.springframework.org/schema/aop"
                 xmlns:tx="http://www.springframework.org/schema/tx"
                 xsi:schemaLocation="http://www.springframework.org/schema/beans
                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                         http://www.springframework.org/schema/context
                         http://www.springframework.org/schema/context/spring-context-3.0.xsd
                         http://www.springframework.org/schema/aop
                         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                         http://www.springframework.org/schema/tx
                         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    	
    	<!--     jdbc.properties文件路径    -->
        <bean 
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
            <property name="locations" 
            value="classpath:jdbc.properties" /> 
        </bean> 
        
    	<!--     <!– 数据源的配置 –>  -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
            destroy-method="close"> 
            <property name="driverClassName" value="${driver}" /> 
            <property name="url" value="${url}" /> 
            <property name="username" value="${user}" /> 
            <property name="password" value="${password}" /> 
    	<!--         <!– data source configuration –>  -->
            <property name="initialSize" value="60" />	
            <property name="maxActive" value="100" />	
            <property name="maxIdle" value="50" />	  
            <property name="minIdle" value="10" />	
    	<!--         <!– 处理mysql 8小时自动断开连接的问题 –>  -->
            <property name="testWhileIdle" value="true" /> 
            <property name="testOnBorrow" value="false" /> 
            <property name="testOnReturn" value="false" /> 
            <property name="validationQuery" value="select 1" /> 
            <property name="timeBetweenEvictionRunsMillis" value="20000" /> 
            <property name="numTestsPerEvictionRun" value="100" /> 
        </bean> 
        
    	<!--     <!–====事务相关控制==–>  -->
        <bean id="transactionManager" 
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
            <property name="dataSource" ref="dataSource" /> 
        </bean> 
        <tx:advice id="userTxAdvice" transaction-manager="transactionManager"> 
            <tx:attributes> 
                <tx:method name="*" propagation="REQUIRED" read-only="false" 
                    rollback-for="java.lang.Exception"/> 
            </tx:attributes> 
        </tx:advice> 
        <aop:config> 
            <aop:pointcut id="pc" 
                expression="execution(* com.demo.web.controller.*.*(..))" /> 
    	<!--              <!– 把事务控制在Business层 –>  -->
            <aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" /> 
        </aop:config> 
    	<!--     <!– MyBatis sqlSessionFactory 配置 mybatis–>  -->
    	
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
            <property name="configLocation" 
            value="classpath:/mybatis/sqlMapConfig.xml" /> 
            <property name="dataSource" ref="dataSource" /> 
        </bean> 
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> 
            <constructor-arg index="0" ref="sqlSessionFactory" /> 
        </bean> 
    	
    	<!--     <!– business AND implementation beans 包含ImplAndBusinessBeans.xml文件 –>  -->
        <import resource="implAndBusinessBeans.xml" /> 
        
    </beans>
    

      

    dispatcher-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:context="http://www.springframework.org/schema/context"
                 xmlns:aop="http://www.springframework.org/schema/aop"
                 xmlns:tx="http://www.springframework.org/schema/tx"
                 xsi:schemaLocation="http://www.springframework.org/schema/beans
                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                         http://www.springframework.org/schema/context
                         http://www.springframework.org/schema/context/spring-context-3.0.xsd
                         http://www.springframework.org/schema/aop
                         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                         http://www.springframework.org/schema/tx
                         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    	
    	<!--     jdbc.properties文件路径    -->
        <bean 
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
            <property name="locations" 
            value="classpath:jdbc.properties" /> 
        </bean> 
        
    	<!--     <!– 数据源的配置 –>  -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
            destroy-method="close"> 
            <property name="driverClassName" value="${driver}" /> 
            <property name="url" value="${url}" /> 
            <property name="username" value="${user}" /> 
            <property name="password" value="${password}" /> 
    	<!--         <!– data source configuration –>  -->
            <property name="initialSize" value="60" />	
            <property name="maxActive" value="100" />	
            <property name="maxIdle" value="50" />	  
            <property name="minIdle" value="10" />	
    	<!--         <!– 处理mysql 8小时自动断开连接的问题 –>  -->
            <property name="testWhileIdle" value="true" /> 
            <property name="testOnBorrow" value="false" /> 
            <property name="testOnReturn" value="false" /> 
            <property name="validationQuery" value="select 1" /> 
            <property name="timeBetweenEvictionRunsMillis" value="20000" /> 
            <property name="numTestsPerEvictionRun" value="100" /> 
        </bean> 
        
    	<!--     <!–====事务相关控制==–>  -->
        <bean id="transactionManager" 
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
            <property name="dataSource" ref="dataSource" /> 
        </bean> 
        <tx:advice id="userTxAdvice" transaction-manager="transactionManager"> 
            <tx:attributes> 
                <tx:method name="*" propagation="REQUIRED" read-only="false" 
                    rollback-for="java.lang.Exception"/> 
            </tx:attributes> 
        </tx:advice> 
        <aop:config> 
            <aop:pointcut id="pc" 
                expression="execution(* com.demo.web.controller.*.*(..))" /> 
    	<!--              <!– 把事务控制在Business层 –>  -->
            <aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" /> 
        </aop:config> 
    	<!--     <!– MyBatis sqlSessionFactory 配置 mybatis–>  -->
    	
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
            <property name="configLocation" 
            value="classpath:/mybatis/sqlMapConfig.xml" /> 
            <property name="dataSource" ref="dataSource" /> 
        </bean> 
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> 
            <constructor-arg index="0" ref="sqlSessionFactory" /> 
        </bean> 
    	
    	<!--     <!– business AND implementation beans 包含ImplAndBusinessBeans.xml文件 –>  -->
        <import resource="implAndBusinessBeans.xml" /> 
        
    </beans>
    

      

    6:配置业务bean .xml文件

    implAndBusinessBeans.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:context="http://www.springframework.org/schema/context"
                 xmlns:aop="http://www.springframework.org/schema/aop"
                 xmlns:tx="http://www.springframework.org/schema/tx"
                 xsi:schemaLocation="http://www.springframework.org/schema/beans
                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                         http://www.springframework.org/schema/context
                         http://www.springframework.org/schema/context/spring-context-3.0.xsd
                         http://www.springframework.org/schema/aop
                         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                         http://www.springframework.org/schema/tx
                         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    	
    	<!--     jdbc.properties文件路径    -->
        <bean 
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
            <property name="locations" 
            value="classpath:jdbc.properties" /> 
        </bean> 
        
    	<!--     <!– 数据源的配置 –>  -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
            destroy-method="close"> 
            <property name="driverClassName" value="${driver}" /> 
            <property name="url" value="${url}" /> 
            <property name="username" value="${user}" /> 
            <property name="password" value="${password}" /> 
    	<!--         <!– data source configuration –>  -->
            <property name="initialSize" value="60" />	
            <property name="maxActive" value="100" />	
            <property name="maxIdle" value="50" />	  
            <property name="minIdle" value="10" />	
    	<!--         <!– 处理mysql 8小时自动断开连接的问题 –>  -->
            <property name="testWhileIdle" value="true" /> 
            <property name="testOnBorrow" value="false" /> 
            <property name="testOnReturn" value="false" /> 
            <property name="validationQuery" value="select 1" /> 
            <property name="timeBetweenEvictionRunsMillis" value="20000" /> 
            <property name="numTestsPerEvictionRun" value="100" /> 
        </bean> 
        
    	<!--     <!–====事务相关控制==–>  -->
        <bean id="transactionManager" 
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
            <property name="dataSource" ref="dataSource" /> 
        </bean> 
        <tx:advice id="userTxAdvice" transaction-manager="transactionManager"> 
            <tx:attributes> 
                <tx:method name="*" propagation="REQUIRED" read-only="false" 
                    rollback-for="java.lang.Exception"/> 
            </tx:attributes> 
        </tx:advice> 
        <aop:config> 
            <aop:pointcut id="pc" 
                expression="execution(* com.demo.web.controller.*.*(..))" /> 
    	<!--              <!– 把事务控制在Business层 –>  -->
            <aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" /> 
        </aop:config> 
    	<!--     <!– MyBatis sqlSessionFactory 配置 mybatis–>  -->
    	
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
            <property name="configLocation" 
            value="classpath:/mybatis/sqlMapConfig.xml" /> 
            <property name="dataSource" ref="dataSource" /> 
        </bean> 
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> 
            <constructor-arg index="0" ref="sqlSessionFactory" /> 
        </bean> 
    	
    	<!--     <!– business AND implementation beans 包含ImplAndBusinessBeans.xml文件 –>  -->
        <import resource="implAndBusinessBeans.xml" /> 
        
    </beans>
    

      

    7. 配置mybatis文件

    sqlMapConfig.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:context="http://www.springframework.org/schema/context"
                 xmlns:aop="http://www.springframework.org/schema/aop"
                 xmlns:tx="http://www.springframework.org/schema/tx"
                 xsi:schemaLocation="http://www.springframework.org/schema/beans
                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                         http://www.springframework.org/schema/context
                         http://www.springframework.org/schema/context/spring-context-3.0.xsd
                         http://www.springframework.org/schema/aop
                         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                         http://www.springframework.org/schema/tx
                         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    	
    	<!--     jdbc.properties文件路径    -->
        <bean 
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
            <property name="locations" 
            value="classpath:jdbc.properties" /> 
        </bean> 
        
    	<!--     <!– 数据源的配置 –>  -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
            destroy-method="close"> 
            <property name="driverClassName" value="${driver}" /> 
            <property name="url" value="${url}" /> 
            <property name="username" value="${user}" /> 
            <property name="password" value="${password}" /> 
    	<!--         <!– data source configuration –>  -->
            <property name="initialSize" value="60" />	
            <property name="maxActive" value="100" />	
            <property name="maxIdle" value="50" />	  
            <property name="minIdle" value="10" />	
    	<!--         <!– 处理mysql 8小时自动断开连接的问题 –>  -->
            <property name="testWhileIdle" value="true" /> 
            <property name="testOnBorrow" value="false" /> 
            <property name="testOnReturn" value="false" /> 
            <property name="validationQuery" value="select 1" /> 
            <property name="timeBetweenEvictionRunsMillis" value="20000" /> 
            <property name="numTestsPerEvictionRun" value="100" /> 
        </bean> 
        
    	<!--     <!–====事务相关控制==–>  -->
        <bean id="transactionManager" 
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
            <property name="dataSource" ref="dataSource" /> 
        </bean> 
        <tx:advice id="userTxAdvice" transaction-manager="transactionManager"> 
            <tx:attributes> 
                <tx:method name="*" propagation="REQUIRED" read-only="false" 
                    rollback-for="java.lang.Exception"/> 
            </tx:attributes> 
        </tx:advice> 
        <aop:config> 
            <aop:pointcut id="pc" 
                expression="execution(* com.demo.web.controller.*.*(..))" /> 
    	<!--              <!– 把事务控制在Business层 –>  -->
            <aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" /> 
        </aop:config> 
    	<!--     <!– MyBatis sqlSessionFactory 配置 mybatis–>  -->
    	
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
            <property name="configLocation" 
            value="classpath:/mybatis/sqlMapConfig.xml" /> 
            <property name="dataSource" ref="dataSource" /> 
        </bean> 
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> 
            <constructor-arg index="0" ref="sqlSessionFactory" /> 
        </bean> 
    	
    	<!--     <!– business AND implementation beans 包含ImplAndBusinessBeans.xml文件 –>  -->
        <import resource="implAndBusinessBeans.xml" /> 
        
    </beans>
    

      user.xml

    <?xml version="1.0" encoding="UTF-8"?> 
    <!DOCTYPE mapper 
    PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" 
    "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> 
    <mapper namespace="com.demo.domain.UserDO">
    
    	<resultMap type="UserDO" id="userDOResultMap">  
            <id property="id" column="id"/>  
            <result property="name" column="name"/>  
            <result property="password" column="password"/>  
        </resultMap>  
    
    	<!--     定义一条查询语句,在bean的implementation中会引用此语句的id  -->
        <insert id="add" parameterType="UserDO" 
            useGeneratedKeys="true" keyProperty="id"> 
            INSERT INTO user (name, password) 
            VALUES(#{name},#{password}) 
        </insert> 
        
        
         <select id="select" resultMap="userDOResultMap">
            select id, name, password from user
        </select> 
    </mapper>
    

      

    Java代码:

    UserDO

    /**
     * Alipay.com Inc.
     * Copyright (c) 2004-2015 All Rights Reserved.
     */
    package com.demo.domain;
    
    /**
     * 
     * @author baoxing.gbx
     * @version $Id: UserDO.java, v 0.1 2015年8月15日 下午10:22:41 baoxing.gbx Exp $
     */
    public class UserDO {
    
        private int    id;
    
        private String name;
    
        private String password;
    
        /**
         * Getter method for property <tt>id</tt>.
         * 
         * @return property value of id
         */
        public int getId() {
            return id;
        }
    
        /**
         * Setter method for property <tt>id</tt>.
         * 
         * @param id value to be assigned to property id
         */
        public void setId(int id) {
            this.id = id;
        }
    
        /**
         * Getter method for property <tt>name</tt>.
         * 
         * @return property value of name
         */
        public String getName() {
            return name;
        }
    
        /**
         * Setter method for property <tt>name</tt>.
         * 
         * @param name value to be assigned to property name
         */
        public void setName(String name) {
            this.name = name;
        }
    
        /**
         * Getter method for property <tt>password</tt>.
         * 
         * @return property value of password
         */
        public String getPassword() {
            return password;
        }
    
        /**
         * Setter method for property <tt>password</tt>.
         * 
         * @param password value to be assigned to property password
         */
        public void setPassword(String password) {
            this.password = password;
        }
    
    }
    

      

    BasicSqlSupport

    /**
     * Alipay.com Inc.
     * Copyright (c) 2004-2015 All Rights Reserved.
     */
    package com.demo.dao;
    
    import org.apache.ibatis.session.SqlSession;
    
    /**
     * 
     * @author baoxing.gbx
     * @version $Id: BasicSqlSupport.java, v 0.1 2015年8月15日 下午10:27:20 baoxing.gbx Exp $
     */
    public class BasicSqlSupport {
        protected SqlSession session;
    
        public SqlSession getSession() {
            return session;
        }
    
        public void setSession(SqlSession session) {
            this.session = session;
        }
    
    }
    

      DAO

    /**
     * Alipay.com Inc.
     * Copyright (c) 2004-2015 All Rights Reserved.
     */
    package com.demo.dao;
    
    import java.util.List;
    
    import com.demo.domain.UserDO;
    
    /**
     * 
     * @author baoxing.gbx
     * @version $Id: UserDAO.java, v 0.1 2015年8月15日 下午10:24:23 baoxing.gbx Exp $
     */
    public interface UserDAO {
    
        public int add(UserDO userDO);
    
        public List<UserDO> queryAll();
    }
    
    
    package com.demo.dao.impl;
    
    import java.util.List;
    
    import com.demo.dao.BasicSqlSupport;
    import com.demo.dao.UserDAO;
    import com.demo.domain.UserDO;
    
    public class UserDAOImpl extends BasicSqlSupport implements UserDAO {
    
        public int add(UserDO userDO) {
            return this.session.insert("com.demo.domain.UserDO.add", userDO);
        }
    
        public List<UserDO> queryAll() {
            return this.session.selectList("com.demo.domain.UserDO.select");
        }
    
    }
    

      

    controller

    RegisterPageController

    /**
     * Alipay.com Inc.
     * Copyright (c) 2004-2015 All Rights Reserved.
     */
    package com.demo.web.controller;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    /**
     * 
     * @author baoxing.gbx
     * @version $Id: RegisterPageController.java, v 0.1 2015年8月17日 下午4:23:30 baoxing.gbx Exp $
     */
    @Controller
    @RequestMapping("/registerPage.htm")
    public class RegisterPageController {
    
        @RequestMapping(method = RequestMethod.GET)
        public String handleGet(HttpServletRequest request, ModelMap modelMap) throws Exception {
            return "/register/register";
        }
    }
    

      

    RegisterController

    /**
     * Alipay.com Inc.
     * Copyright (c) 2004-2015 All Rights Reserved.
     */
    package com.demo.web.controller;
    
    import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    import com.demo.dao.UserDAO;
    import com.demo.domain.UserDO;
    
    /**
     * 
     * @author baoxing.gbx
     * @version $Id: RegisterController.java, v 0.1 2015年8月17日 下午4:17:07 baoxing.gbx Exp $
     */
    @Controller
    @RequestMapping("/register.htm")
    public class RegisterController {
        private static final Log LOGGER = LogFactory.getLog(LoginController.class);
    
        @Resource
        private UserDAO          userDAO;
    
        @RequestMapping(method = RequestMethod.POST)
        public String handlePost(HttpServletRequest request, ModelMap modelMap) throws Exception {
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            LOGGER.info("name = " + name + "password = " + password);
            UserDO userDO = new UserDO();
            userDO.setName(name);
            userDO.setPassword(password);
            userDAO.add(userDO);
    
            modelMap.put("result", userDO);
            return "result";
        }
    
        /**
         * Setter method for property <tt>userDAO</tt>.
         * 
         * @param userDAO value to be assigned to property userDAO
         */
        public void setUserDAO(UserDAO userDAO) {
            this.userDAO = userDAO;
        }
    }
    

      

    LoginPageController

    /**
     * Alipay.com Inc.
     * Copyright (c) 2004-2015 All Rights Reserved.
     */
    package com.demo.web.controller;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    /**
     * 
     * @author baoxing.gbx
     * @version $Id: LoginPageController.java, v 0.1 2015年8月17日 下午4:23:46 baoxing.gbx Exp $
     */
    @Controller
    @RequestMapping("/loginPage.htm")
    public class LoginPageController {
    
        @RequestMapping(method = RequestMethod.GET)
        public String handleGet(HttpServletRequest request, ModelMap modelMap) throws Exception {
            return "/login/login";
        }
    
    }
    

      

    LoginController

    /**
     * Alipay.com Inc.
     * Copyright (c) 2004-2015 All Rights Reserved.
     */
    package com.demo.web.controller;
    
    import java.util.List;
    
    import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    
    import org.apache.commons.lang.StringUtils;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    import com.demo.dao.UserDAO;
    import com.demo.domain.UserDO;
    
    /**
     * 
     * @author baoxing.gbx
     * @version $Id: LoginController.java, v 0.1 2015年8月15日 下午8:51:54 baoxing.gbx Exp $
     */
    @Controller
    @RequestMapping("/login.htm")
    public class LoginController {
    
        private static final Log LOGGER = LogFactory.getLog(LoginController.class);
    
        @Resource
        private UserDAO          userDAO;
    
        @RequestMapping(method = RequestMethod.POST)
        public String handlePost(HttpServletRequest request, ModelMap modelMap) throws Exception {
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            LOGGER.info("name = " + name + "password = " + password);
    
            List<UserDO> userDOs = userDAO.queryAll();
    
            for (UserDO user : userDOs) {
                if (StringUtils.equals(name, user.getName())
                    && StringUtils.equals(password, user.getPassword())) {
                    modelMap.put("result", "login success!");
                } else {
                    modelMap.put("result", "login failed!");
                }
            }
    
            return "result";
        }
    
        /**
         * Setter method for property <tt>userDAO</tt>.
         * 
         * @param userDAO value to be assigned to property userDAO
         */
        public void setUserDAO(UserDAO userDAO) {
            this.userDAO = userDAO;
        }
    
    }
    

      

    界面:

    demo : 地址 https://github.com/EstarG/demo1

    坑: 

    如果conroller使用了

    <!-- 把标记了@Controller注解的类转换为bean -->
    <context:component-scan base-package="com.demo.web.controller" /> 自动扫描的方式的话, 

    那么controller的属性注入也需要使用注解的方式

  • 相关阅读:
    javascript高级编程笔记03(正则表达式)
    javascript高级编程笔记02(基本概念)
    javascript高级编程笔记01(基本概念)
    ExtJS4加载FormPanel数据的几种方式
    Extjs 更新数据集Ext.PagingToolbar的start参数重置的处理
    四川绵阳 晴
    四川绵阳 阴
    四川绵阳 晴
    在IntelliJ IDEA中添加repository模板
    List分组 用于客服对话分组场景
  • 原文地址:https://www.cnblogs.com/E-star/p/4740182.html
Copyright © 2020-2023  润新知