Spring整合mybatis
Posted 菜霸
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring整合mybatis相关的知识,希望对你有一定的参考价值。
Maven配置
Maven配置依赖
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency>
Spring中配置 Mybatis。由Spring管理mybatis 直接在spring配置文件中设置Mybatis即可。
Spring-config.xml 内容如下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:property-placeholder location="classpath*:*.properties" ignore-unresolvable="true"/> <context:component-scan base-package="com.wormpex.study"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <context:annotation-config/> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>${driver}</value> </property> <property name="url"> <value>${url}</value> </property> <property name="username"> <value>${username}</value> </property> <property name="password"> <value>${password}</value> </property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 实体类 --> <property name="typeAliasesPackage" value="com.wormpex.study.entity"></property> <!-- 实体类映射的xml层 --> <property name="mapperLocations" value="classpath*:com/wormpex/study/mapper/*Mapper.xml"></property> </bean> <!--4 自动扫描对象关系映射 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--指定会话工厂,如果当前上下文中只定义了一个则该属性可省去 --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <!-- 指定要自动扫描接口的基础包,实现接口(dao层) --> <property name="basePackage" value="com.wormpex.study.dao"></property> </bean> <!--定义事物管理器,由spring管理事务 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--支持注解驱动的事务管理,指定事务管理器 --> <tx:annotation-driven transaction-manager="txManager"/> </beans>
以上是关于Spring整合mybatis的主要内容,如果未能解决你的问题,请参考以下文章
请教mybatis+spring+atomikos的整合问题
基于Spring+SpringMVC+MyBatis开发书评网Spring和MyBatis的整合