一、参考文档(参考此文档后成功整合)
二、Spring、mysql数据库、MyBatis版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <!--① 依赖的Spring模块类库 --> < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-context</ artifactId > < version >3.1.1.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-beans</ artifactId > < version >3.1.1.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-web</ artifactId > < version >3.1.1.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-webmvc</ artifactId > < version >3.1.1.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-test</ artifactId > < version >3.1.1.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-jdbc</ artifactId > < version >3.1.1.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-orm</ artifactId > < version >3.1.1.RELEASE</ version > </ dependency > < dependency > < groupId >commons-dbcp</ groupId > < artifactId >commons-dbcp</ artifactId > < version >1.4</ version > </ dependency > < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > < version >5.1.34</ version > </ dependency > <!-- 数据源 --> < dependency > < groupId >org.mybatis</ groupId > < artifactId >mybatis</ artifactId > < version >3.1.1</ version > </ dependency > < dependency > < groupId >org.mybatis</ groupId > < artifactId >mybatis-spring</ artifactId > < version >1.2.0</ version > </ dependency > |
三、配置文件(均在resources文件夹中)
1、spring-mvc.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "" xmlns:xsi = "" xmlns:context = "" xmlns:mvc = "" xmlns:p = "" xmlns:tx = "" xmlns:aop = "" xsi:schemaLocation=" " default-init-method = "init" > <!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 --> < mvc:annotation-driven /> < context:component-scan base-package = "com.fly" /> <!-- 引入jdbc配置文件 --> < context:property-placeholder location = "classpath:jdbc.properties" /> <!--创建jdbc数据源 --> < bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method = "close" > < property name = "driverClassName" value = "${jdbc.driverClassName}" /> < property name = "url" value = "${jdbc.url}" /> < property name = "username" value = "${jdbc.username}" /> < property name = "password" value = "${jdbc.password}" /> </ bean > <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> < bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name = "dataSource" ref = "dataSource" /> </ bean > <!-- 创建SqlSessionFactory,同时指定数据源 --> < bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > < property name = "configLocation" value = "classpath:mybatis.xml" ></ property > < property name = "dataSource" ref = "dataSource" /> </ bean > <!-- 可通过注解控制事务 --> < tx:annotation-driven transaction-manager = "transactionManager" /> <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper --> < bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" > < property name = "basePackage" value = "com.fly.dao" /> </ bean > </ beans > |
2、jdbc.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #数据库连接 jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql: //127.0.0.1:3306/hr?useUnicode=true&characterEncoding=UTF-8 jdbc.username=root jdbc.password=root jdbc.minPoolSize= 5 jdbc.maxPoolSize= 20 jdbc.maxIdleTime= 1800 jdbc.acquireIncrement= 2 jdbc.maxStatements= 50 jdbc.initialPoolSize= 10 jdbc.idleConnectionTestPeriod= 1800 jdbc.acquireRetryAttempts= 2 cpool.automaticTestTable=Test cpool.preferredTestQuery=select 1 from Test cpool.testConnectionOnCheckin= true |
3、mybatis.xml(mapper节点对应xml文件)
1 2 3 4 5 6 7 8 9 10 11 12 | <? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" ""> < configuration > <!-- <typeAliases> <typeAlias alias="Student" type="com.matol.entity.User" /> </typeAliases> --> < mappers > < mapper resource = "com/fly/dao/UserMapper.xml" /> </ mappers > </ configuration > |
四、Dao层
1、Java文件
1 2 3 4 5 6 7 | package com.fly.dao; import org.springframework.stereotype.Repository; @Repository public interface UserMapper { Integer count(); } |
2、xml文件(xml中的namespace对应java文件的完整目录)
1 2 3 4 5 6 7 8 | <? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ""> < mapper namespace = "com.fly.dao.UserMapper" > < select id = "count" resultType = "Integer" > SELECT count(*) FROM operatelog </ select > </mapper |
五、Service层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.fly.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.fly.dao.UserMapper; @Service public class UserService { @Autowired private UserMapper userMapper; public int count() { return this .userMapper.count(); } } |