mybaits和spring整合后pojo扫描怎么配置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybaits和spring整合后pojo扫描怎么配置相关的知识,希望对你有一定的参考价值。
参考技术A 这篇文章将介绍Spring整合Mybatis如何完成SqlSessionFactory的动态切换的。并且会简单的介绍下MyBatis整合Spring中的官方的相关代码。Spring整合MyBatis切换SqlSessionFactory有两种方法,第一、继承SqlSessionDaoSupport,重写获取SqlSessionFactory的方法。第二、继承SqlSessionTemplate重写getSqlSessionFactory、getConfiguration和SqlSessionInterceptor这个拦截器。其中最为关键还是继承SqlSessionTemplate并重写里面的方法。而Spring整合MyBatis也有两种方式,一种是配置MapperFactoryBean,另一种则是利用MapperScannerConfigurer进行扫描接口或包完成对象的自动创建。相对来说后者更方便些。MapperFactoryBean继承了SqlSessionDaoSupport也就是动态切换SqlSessionFactory的第一种方法,我们需要重写和实现SqlSessionDaoSupport方法,或者是继承MapperFactoryBean来重写覆盖相关方法。如果利用MapperScannerConfigurer的配置整合来切换SqlSessionFactory,那么我们就需要继承SqlSessionTemplate,重写上面提到的方法。在整合的配置中很多地方都是可以注入SqlSessionTempspring和mybaits整合
spring + mybatis + mysql/oracle开发
- 创建一个spring-mybatis-mysql这么一个javaweb或java工程
- 导入spring-ioc,spring-aop,spring-transaction,mybatis,c3p0,mysql/oracle相关的jar包和spring整合mybatis的jar包
- 创建students.sql
--mysql
create table students(
sid int(5) primary key,
sname varchar(10),
ssal double(8,2)
);
- 创建Student.java
/**
* 学生
* @author csx
*/
public class Student
private Integer id;//编号
private String name;//姓名
private Double sal;//薪水
public Student()
public Student(Integer id, String name, Double sal)
this.id = id;
this.name = name;
this.sal = sal;
public Integer getId()
return id;
public void setId(Integer id)
this.id = id;
public String getName()
return name;
public void setName(String name)
this.name = name;
public Double getSal()
return sal;
public void setSal(Double sal)
this.sal = sal;
- 创建StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="studentNamespace">
<resultMap type="cn.csx.javaee.mybatis.entity.Student" id="studentMap">
<id property="id" column="sid" />
<result property="name" column="sname"/>
<result property="sal" column="ssal"/>
</resultMap>
<insert id="insert" parameterType="cn.csx.javaee.mybatis.entity.Student">
insert into students(sid,sname,ssal) values(#id,#name,#sal)
</insert>
</mapper>
- 创建StudentDao.java
public class StudentDao
private SqlSessionFactory sqlSessionFactory;
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory)
this.sqlSessionFactory = sqlSessionFactory;
public void insert(Student student)
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.insert("studentNamespace.insert",student);
//int i = 10/0;
public static void main(String[] args)
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]"spring.xml");
StudentDao studentDao = (StudentDao) ac.getBean("studentDaoID");
studentDao.insert(new Student(1,"哈哈",7000D));
- 在src目录下创建mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<mapper resource="cn/csx/javaee/mybatis/entity/StudentMapper.xml"/>
</mappers>
</configuration>
- 在src目录下创建spring.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<!-- 配置C3P0连接池(即管理数据库连接) -->
<bean id="comboPooledDataSourceID" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/mybatis"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 配置SqlSessionFactoryBean(即替代MyBatisUtil工具类的作用) -->
<bean id="sqlSessionFactoryBeanID" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="comboPooledDataSourceID"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<!-- 配置事务管理器(即使用JDBC事务管理器) -->
<bean id="dataSourceTransactionManagerID" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="comboPooledDataSourceID"/>
</bean>
<!-- 配置事务通知(即哪些方法需要事务) -->
<tx:advice id="tx" transaction-manager="dataSourceTransactionManagerID">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务切面(即哪些包中的类需要事务通知) -->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* cn.itcast.javaee.mybatis.dao.*.*(..))"/>
<aop:advisor advice-ref="tx" pointcut-ref="pointcut" />
</aop:config>
<!-- 配置StudentDao类 -->
<bean id="studentDaoID" class="cn.itcast.javaee.mybatis.dao.StudentDao">
<property name="sqlSessionFactory" ref="sqlSessionFactoryBeanID"/>
</bean>
</beans>
以上是关于mybaits和spring整合后pojo扫描怎么配置的主要内容,如果未能解决你的问题,请参考以下文章
spring整合mybatis时运行测试代码报错,是怎么回事?
Spring原理篇(12)--架构整合流程;该篇章讲解Spring在整合架构的时候的思路;