首先需要下载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 true\uFF0Cappend to the file; if false, the replace the log file whenever restart system
log4j.appender.FILEOUT.Append=true
#RollingFileAppender\u6CA1\u6709DatePattern\u8FD9\u4E2A\u5C5E\u6027
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的属性注入也需要使用注解的方式

eclipse + maven 搭建springMVC+Spring+mybatis 系统的更多相关文章

  1. 手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)

    手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版) SSM(Spring+SpringMVC+Mybatis),目前较为主流的企业级架构方案.标准的MVC设计模式, ...

  2. Maven 搭建SpringMvc+Spring+Mybatis详细记录

    总觉得,看比人写的总是那么好,每次搭建框架时都会找博客,找教程来跟着一步一步走,虽然很快搭建成功了,但是经常情况是我并不知道我干了什么,也不记得具体步骤,到底为什么要这么做,今天我详细记录了一下自己搭 ...

  3. 详解手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)

    转载(https://www.jb51.net/article/130560.htm) SSM(Spring+SpringMVC+Mybatis),目前较为主流的企业级架构方案.标准的MVC设计模式, ...

  4. 单工程搭建springmvc+spring+mybatis(maven,idea)

    单工程搭建springmvc+spring+mybatis(maven,idea) 1.pom.xml <properties> <project.build.sourceEncod ...

  5. Maven搭建springMVC+spring+hibernate环境

    这次不再使用struts2做控制器,采用spring自己的springMVC框架实现. 首先,改写pom.xml文件,不需要struts2的相关jar了. pom.xml <project xm ...

  6. Maven构建 SpringMVC+Spring+MyBatis 环境整合

    目录 1. Maven 项目搭建 2. Maven 插件生成 MyBatis 代码 3. 待续 ... 开发环境 开发环境请尽量保持一致,不一致的情况可能存在问题. JDK 1.7 MyEclipse ...

  7. JavaWeb_(SpringMVC框架)测试SpringMVC&Spring&MyBatis三大整合

    搭建 SpringMVC&Spring&MyBatis三大整合 传送门 1.准备 测试搭建S pringMVC&Spring&MyBatis三大整合 用例   a)准备 ...

  8. 手把手教你使用VUE+SpringMVC+Spring+Mybatis+Maven构建属于你自己的电商系统之vue后台前端框架搭建——猿实战01

            猿实战是一个原创系列文章,通过实战的方式,采用前后端分离的技术结合SpringMVC Spring Mybatis,手把手教你撸一个完整的电商系统,跟着教程走下来,变身猿人找到工作不是 ...

  9. SpringMVC+Spring+mybatis+maven+搭建多模块框架前后端分离开发框架的完整demo,拿走不谢。——猿实战02

            猿实战是一个原创系列文章,通过实战的方式,采用前后端分离的技术结合SpringMVC Spring Mybatis,手把手教你撸一个完整的电商系统,跟着教程走下来,变身猿人找到工作不是 ...

随机推荐

  1. mysql5.5手册读书日记(2)

    <?php /* * * MySQL_5.5中文参考手册 485开始 * * mysql> SELECT CASE 1 WHEN 1 THEN 'one' -> WHEN 2 THE ...

  2. JavaScript 入门教程二 在HTML中使用 JavaScript

    一.使用 <script> 元素的方式有两种:直接在页面中嵌入 JavaScript 代码和引用外部 JavaScript 文件. 二.使用内嵌方式,一般写法为: <script t ...

  3. JavaScript方法的调用

    1.假如是有名字的函数 调用是用名字调用,并且会把返回值赋值给接受它的参数 代码: function f() { alert("我是f"); return '我是返回值'; } f ...

  4. switch多分支语句

    1.switch多分支语句的语法 switch(表达式){ case 常量值:要执行的语句; break; case 常量值:要执行的语句; break; case 常量值:要执行的语句; break ...

  5. request.querystring和request.form、session的区别

    1. request.querystring是用来接收地址里面问号“?”后面的参数的内容,  用get方法读取的 不安全   request.form是用来接收表单递交来的数据 ,是用post方法读取 ...

  6. php应用路径变量问题总结

    实际效果测试,不考虑原理! 本地服务器,域名http://d.com,根路径D:\phpnow\vhosts\d.com.yii,相对根目录拥有文件/x.php代码里requeir_once /a/a ...

  7. 用Appium进行android自动化测试

    appium是开源的移动端自动化测试框架,可以测试ios,android应用.appium让移动端自动化测试不必限定在某种语言和某个具体的框架:也就是说任何人都可以使用自己最熟悉最顺手的语言以及框架来 ...

  8. 猿题库 iOS 客户端架构设计-唐巧

    序 猿题库是一个拥有数千万用户的创业公司,从20013年题库项目起步到2015年,团队保持了极高的生产效率,使我们的产品完成了五个大版本和数十个小版本的高速迭代. 在如此快速的开发过程中,如何保证代码 ...

  9. 图像的 SNR 和 PSNR 的计算

    PSNR 的公式很容易搜到. http://www.360doc.com/content/12/0605/21/4129998_216244993.shtml http://blog.sina.com ...

  10. python install (version problem-method ln -s)

    一般情况下,无需自己安装Python.因为在大多数Linux版本中,如Fedora,Ubuntu等,都已经默认安装了Python,但也可以根据需要自定义安装Python.下面使用源码编译安装来举个例子 ...