项目结构如下:
1.首先配置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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sxb</groupId>
<artifactId>springmvc_mybatis_demo</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>springmvc_mybatis_demo Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.0.9.RELEASE</spring.version>
<jstl.version>1.2</jstl.version>
<mybatis.generator.generatorConfig.xml>${basedir}/src/main/resources/generatormybatis/generatorConfig.xml</mybatis.generator.generatorConfig.xml>
<mybatis.generator.generatorConfig.properties>file:///${basedir}/src/main/resources/generatormybatis/generatorConfig.properties</mybatis.generator.generatorConfig.properties>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- 添加mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<!-- 添加mybatis/spring整合包依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 添加mysql驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.9</version>
</dependency>
</dependencies>
<build>
<finalName>springmvc_mybatis_demo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- Mybatis generator代码生成插件 配置 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>${mybatis.generator.generatorConfig.xml}</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.配置web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>springmvcDispatchServelet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvcDispatchServelet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3.编写springmvc.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 开启注解 --> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <!-- 解决@ResponseBody中文乱码 --> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes" value = "text/html;charset=UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven> <!--<bean id="UserService111" class="com.sxb.impl.UserServiceImpl"></bean>--> <!--<bean name="/test" class="com.sxb.controller.TestController" >--> <!--<property name="userService" ref="UserService111"></property>--> <!--</bean>--> <!--<!–配置处理器适配器 –>--> <!--<bean--> <!--class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />--> <!--<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>--> <!--<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>--> <!--<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>--> <!-- 扫描Controller所在的包 --> <context:component-scan base-package="com.lhxbb"></context:component-scan> <!-- 事务管理 --> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <!-- 配置试图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <import resource="classpath*:/spring/spring-mybatis.xml" /> </beans>
3.编写spring-mybatis.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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" 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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 加载db.properties文件中的内容 中的key要有一定的规则 --> <context:property-placeholder location="classpath*:/conf/jdbc.properties"/> <!-- 配置数据源 --> <!--<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="${username}"/>--> <!--<property name="password" value="${password}"/>--> <!--<property name="maxActive" value="30"/>--> <!--<property name="maxIdle" value="5"/>--> <!--</bean>--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxActive" value="${jdbc.maxActive}"/> <property name="maxIdle" value="${jdbc.maxIdle}"/> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 加载数据库连接池 --> <property name="dataSource" ref="dataSource"></property> <!-- 加载mybatis全局配置文件 --> <!--<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>--> <property name="typeAliasesPackage" value="com.lhxbb.dao.bean"/> <property name="mapperLocations" value="classpath:mybatisxml/*.xml"/> </bean> <!-- 配置mapper扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描包路径,如果需要扫描多个,中间使用半角的逗号隔开 --> <property name="basePackage" value="com.lhxbb.dao.mapper"></property> <property name="SqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
5.编写jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/practice jdbc.username=xxx jdbc.password=ooo #定义初始连接数 jdbc.initialSize=0 #定义最大连接数 jdbc.maxActive=20 #定义最大空闲 jdbc.maxIdle=20 #定义最小空闲 jdbc.minIdle=1 #定义最长等待时间 jdbc.maxWait=60000
按照上述文件配置后,即可启动项目。