MyBatis框架主要是围绕着SqlSessionFactory这个类进行的
定义一个Configuration对象,其中包含数据源、事务、mapper文件资源以及影响数据库行为属性设置settings通过配置对象,则可以创建一个SqlSessionFactoryBuilder对象通过 SqlSessionFactoryBuilder 获得SqlSessionFactory 的实例。SqlSessionFactory 的实例可以获得操作数据的SqlSession实例,通过这个实例对数据库进行操作
配置文件beans.xml
<!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <!-- 声明式事务配置 开始 --> <!-- 配置事务管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置事务通知 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- 配置哪些方法使用什么样的事务,配置事务的传播特性 --> <tx:method name="add" propagation="REQUIRED"/> <tx:method name="insert" propagation="REQUIRED"/> <tx:method name="update" propagation="REQUIRED"/> <tx:method name="delete" propagation="REQUIRED"/> <tx:method name="remove*" propagation="REQUIRED"/> <tx:method name="get" read-only="true"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* cn.sxt.dao.impl.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> <!-- 声明式事务配置 结束 --> <!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis.cfg.xml"/> </bean> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <bean id="userDao" class="cn.sxt.dao.impl.UserDaoImpl"> <property name="sqlSession" ref="sqlSessionTemplate"/> </bean>
myBatis配置文件
<?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> <typeAliases> <package name="cn.sxt.vo"/> </typeAliases> <mappers> <mapper resource="cn/sxt/vo/user.mapper.xml"/> </mappers> </configuration>
声明式事务管理
使用mybatis-spring-1.2.3整合,在spring配置文件中,不需要管理sqlSessionTemplate,在Dao实现中,需要继承SqlSessionDaoSupport
<!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis.cfg.xml"/> </bean> <bean id="userDao" class="cn.sxt.dao.impl.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean>
public class UserDaoImpl extends SqlSessionDaoSupportimplements UserDao{ @Override public List<User> selectUser() { return this.getSqlSession().selectList("cn.sxt.vo.user.mapper.selectAll"); } }
自动扫描配置包下的所有注释
<context:component-scan base-package="cn,sxt">
dao---@Repository("userDao")
service---@Service("userService")
action---@Controller("userAction")
属性的注入:@autowired
自动