MyBatis与Spring整合
(2)然后通过手动或者配置自动的方式把SqSessionlFactory注入到Dao层的对象中,
整合步骤:
1,导入依赖(MyBatis与Spring整合的依赖包
mybatis-spring
)下面是ssm的整合的pom配置
properties
!-- spring版本号 --
spring.version4.3.0.RELEASE/spring.version
!-- mybatis版本号 --
mybatis.version3.2.6/mybatis.version
!-- log4j日志文件管理包版本 --
slf4j.version1.7.7/slf4j.version
log4j.version1.2.17/log4j.version
/properties
dependencies
dependency
groupIdjunit/groupId
artifactIdjunit/artifactId
version4.11/version
!-- 表示开发的时候引入,发布的时候不会加载此包 --
scopetest/scope
/dependency
!-- spring核心包 --
dependency
groupIdorg.springframework/groupId
artifactIdspring-core/artifactId
version${spring.version}/version
/dependency
dependency
groupIdorg.springframework/groupId
artifactIdspring-web/artifactId
version${spring.version}/version
/dependency
dependency
groupIdorg.springframework/groupId
artifactIdspring-oxm/artifactId
version${spring.version}/version
/dependency
dependency
groupIdorg.springframework/groupId
artifactIdspring-tx/artifactId
version${spring.version}/version
/dependency
dependency
groupIdorg.springframework/groupId
artifactIdspring-jdbc/artifactId
version${spring.version}/version
/dependency
dependency
groupIdorg.springframework/groupId
artifactIdspring-webmvc/artifactId
version${spring.version}/version
/dependency
dependency
groupIdorg.springframework/groupId
artifactIdspring-aop/artifactId
version${spring.version}/version
/dependency
dependency
groupIdorg.springframework/groupId
artifactIdspring-context-support/artifactId
version${spring.version}/version
/dependency
dependency
groupIdorg.springframework/groupId
artifactIdspring-test/artifactId
version${spring.version}/version
/dependency
!-- mybatis核心包 --
dependency
groupIdorg.mybatis/groupId
artifactIdmybatis/artifactId
version${mybatis.version}/version
/dependency
!-- mybatis/spring包 --
dependency
groupIdorg.mybatis/groupId
artifactIdmybatis-spring/artifactId
version1.2.2/version
/dependency
!-- 导入java ee jar 包 --
dependency
groupIdjavax/groupId
artifactIdjavaee-api/artifactId
version7.0/version
/dependency
!-- 导入Mysql数据库链接jar包 --
dependency
groupIdmysql/groupId
artifactIdmysql-connector-java/artifactId
version5.1.30/version
/dependency
!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 --
dependency
groupIdcommons-dbcp/groupId
artifactIdcommons-dbcp/artifactId
version1.2.2/version
/dependency
!-- JSTL标签类 --
dependency
groupIdjstl/groupId
artifactIdjstl/artifactId
version1.2/version
/dependency
!-- 日志文件管理包 --
!-- log start --
dependency
groupIdlog4j/groupId
artifactIdlog4j/artifactId
version${log4j.version}/version
/dependency
!-- 格式化对象,方便输出日志 --
dependency
groupIdcom.alibaba/groupId
artifactIdfastjson/artifactId
version1.1.41/version
/dependency
dependency
groupIdorg.slf4j/groupId
artifactIdslf4j-api/artifactId
version${slf4j.version}/version
/dependency
dependency
groupIdorg.slf4j/groupId
artifactIdslf4j-log4j12/artifactId
version${slf4j.version}/version
/dependency
!-- log end --
!-- 映入JSON --
dependency
groupIdorg.codehaus.jackson/groupId
artifactIdjackson-mapper-asl/artifactId
version1.9.13/version
/dependency
!-- 上传组件包 --
dependency
groupIdcommons-fileupload/groupId
artifactIdcommons-fileupload/artifactId
version1.3.1/version
/dependency
dependency
groupIdcommons-io/groupId
artifactIdcommons-io/artifactId
version2.4/version
/dependency
dependency
groupIdcommons-codec/groupId
artifactIdcommons-codec/artifactId
version1.9/version
/dependency
/dependencies
2,选择整合方式,进行代码配置以及配置文件的书写。这里只提供第三种的方式的书写。
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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"
!-- 自动扫描 --
context:component-scan base-package="com.lifeibai.mybatis" /
!-- 引入配置文件 --
bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
property name="location" 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="${username}" /
property name="password" value="${password}" /
!-- 初始化连接大小 --
property name="initialSize" value="${initialSize}"/property
!-- 连接池最大数量 --
property name="maxActive" value="${maxActive}"/property
!-- 连接池最大空闲 --
property name="maxIdle" value="${maxIdle}"/property
!-- 连接池最小空闲 --
property name="minIdle" value="${minIdle}"/property
!-- 获取连接最大等待时间 --
property name="maxWait" value="${maxWait}"/property
/bean
!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --
bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
property name="dataSource" ref="dataSource" /
!-- 自动扫描mapping.xml文件 配置这个在配置自动扫描包Mapper 可以让mapper与xml分离,否则的话,两者要在同一个包中--
!-- property name="mapperLocations" value="classpath:com/cn/hnust/mapping/*.xml"/property --
/bean
!-- DAO接口所在包名,Spring会自动查找其下的类 --
bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
property name="basePackage" value="om.lifeibai.mybatis.mapper" /
!-- 这个属性可以不配置,因为有个默认值就是sqlSessionFactory --
!--property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/property --
/bean
/beans
文章如有错误,请您一定指出,感谢之至!
如果你有不同的见解,欢迎留言,或者加我QQ986320270
图片可能来源于网络,如有侵权请告知。
最后:关注一下呗
长按二维码识别关注