(spring-mybatis-mysql整合)纯Java配置类配置事务管理

spring整合mybatis主要需要配置连接池、sessionfactory以及事务管理器这几个方面。其中使用xml方式配置事务管理器需要配置相应的切面等内容,而使用@Transactional注解直接配置时可以不用指明切点表达式,直接作用于需要事务管理的类或者方法。

代码如下:


@Configuration
@ComponentScan(basePackages = "zcc.mms.dao")
@EnableTransactionManagement
public class JDBCConfig implements TransactionManagementConfigurer {
    @Bean(name = "comboPooledDataSourceID")
    public ComboPooledDataSource getC3P0() {
        ComboPooledDataSource ds = new ComboPooledDataSource();
        try {
            ds.setDriverClass("com.mysql.jdbc.Driver");
        } catch (PropertyVetoException e) {
            e.printStackTrace();
            throw new RuntimeException("数据库驱动加载错误!");
        }
        ds.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test");
        ds.setUser("root");
        ds.setPassword("root");
        return ds;
    }
    @Bean(name = "sqlSessionFactoryBeanID")
    public SqlSessionFactoryBean getSessionFactory(ComboPooledDataSource ds){
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setConfigLocation(new ClassPathResource("mybatis.xml"));
        factory.setDataSource(ds);
        return factory;
    }
    @Bean(name = "dataSourceTransactionManagerID")
    public DataSourceTransactionManager getTransactionManager(ComboPooledDataSource ds){
        DataSourceTransactionManager manager = new DataSourceTransactionManager();
        manager.setDataSource(ds);
        return manager;
    }

    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return getTransactionManager(getC3P0());
    }
}

	主要使用@EnableTransactionManagement注解来启用注解驱动的事务管理,而通过实现TransactionManagementConfigurer接口可以提供自定义
的事务管理器bean。
 	简单构造实体类如下:

public class Emp {
    private Integer id;
    private String name;
    private Double sal;
    private String sex;
    public Emp(){}
    public Emp(Integer id, String name, Double sal, String sex) {
        this.id = id;
        this.name = name;
        this.sal = sal;
        this.sex = sex;
    }
    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;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
}
相关的dao类中使用@Transactional注解,事务按理说是作用与service的,这里为了简便直接注解在dao类上。在类上使用此注解作用于类内的所有方法
,readOnly设为true即将内部的方法的事务隔离级别设置为只读。而针对更新插入等写操作,单独在方法上指定此注解,readOnly默认为false。方法和
类上都使用此注解的,以方法上的为准。

@Component(value = "empDaoID")
@Transactional(readOnly=true)//注解方式设置事务、在类的级别上设置只读事务
public class EmpDao {
    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }
    /**
     * 增加员工
     */
    @Transactional(propagation= Propagation.REQUIRED, isolation= Isolation.DEFAULT)
    public void add(Emp emp) throws Exception{
        SqlSession sqlSession = sqlSessionFactory.openSession();
        sqlSession.insert("empNamespace.add",emp);
        sqlSession.close();
    }
}

mybatis相关的配置文件内容如下:
mybatis.xml



    
        
    
EmpMapper.xml




    
        
        
        
        
    

    
    
        insert into emps(eid,ename,esal,esex) values(#{id},#{name},#{sal},#{sex});
    
最后是测试类:
public class EmpDaoTest {
    @Test
    public void testInsert() throws Exception {
        ApplicationContext app = new AnnotationConfigApplicationContext(JDBCConfig.class);
        EmpDao dao = (EmpDao)app.getBean("empDaoID");
        dao.add(new Emp(3, "qw", 1000.12, "wqe"));
    }
}
一开始总是报java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedT的错误,后来发现是mybatis包
和mybatis-spring插件包的版本不匹配所致,一定要使用兼容的版本。




你可能感兴趣的:(Spring)