提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
提示:以下整理了spring-mybatis整合的相关知识与配置资料
一、Spring-Mybatis整合所需要的jar包
点击下载jar包
提取码:yo4l
二、如何整合
1.配置Spring的applicationContext.xml文件
代码如下(示例):
spring的自动装配会自动扫描目录注解
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="dept01" class="com.hwua.po.Dept">
<property name="deptno" value="1"></property>
<property name="dname" value="张三"></property>
<property name="loc" value="海南"></property>
</bean>
<!-- 自动装配 自动扫描指定目录下注解 -->
<context:component-scan base-package="com.hwua"></context:component-scan>
</beans>
2.配置Spring-Mybatis的 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: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/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 组件扫描 -->
<context:component-scan base-package="com.hwua"></context:component-scan>
<!-- 进行数据库的配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="user" value="scott"></property>
<property name="password" value="root"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 启用事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- Spring整合Mybatis -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- MyBatis的配置文件 -->
<property name="configLocation"
value="classpath:mybatis-config.xml"></property>
<!-- 配置mybatis映射文件 -->
<property name="mapperLocations"
value="classpath:com/hwua/dao/*.xml"></property>
<!-- 处理别名 -->
<property name="typeAliasesPackage"
value="com.hwua.po"></property>
</bean>
<!-- Mapper接口
MapperScannerConfigurer 为指定包下的Mapper接口批量生成代理实现类.bean的默认id是接口名首字母小写.
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hwua.dao"></property>
</bean>
</beans>
3.测试
注意:Po类与Dao类、Dao.xml文件的创建方式与mybatis中配置的方式样,只是测试的时候getMapper()改为了getBean()。
点击跳转mybatis中的配置
@org.junit.Test
public void findById() {
//读取配置文件
ApplicationContext app =
new ClassPathXmlApplicationContext("spring-mybatis.xml");
//获取bean
DeptDao dd = app.getBean("deptDao", DeptDao.class);
//调用方法
Dept d = dd.findById(40);
System.out.println(d);
}