整合mybatis-spring
- SqlSessionFactoryBean
- 包扫描接口 MapperScannerConfigurer 或 @MapperScan
- 事务管理器 DataSourceTransactionManager
用到的注解 @Transactional 需要开事务的 annotation-driver。。@Transaction在方法上表示该方法开启事务,,标注在类上,表示类中所有方法都开启事务 @Aspect @Mapper 感觉@Mapper没什么用?????? @MapperScan mybatis扫描接口。 @EnableAspectJAutoProxy @RunWith @ContxtConfiguration
实体类
@Component
public class Account {
private String name;
private Double money;
@Override
public String toString() {
return "com.com.cj.bean.Account{" +
"name='" + name + '\'' +
", money=" + money +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
}
配置文件
package com.cj.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@ComponentScan("com.cj")
@MapperScan("com.cj.mapper")
@PropertySource("classpath:db.properties")
@ImportResource("classpath:spring.xml")
@EnableAspectJAutoProxy
public class ConfigApp {
@Value("jdbc:mysql:///test01")
private String url;
@Value("com.mysql.jdbc.Driver")
private String driver;
@Value("root")
private String username;
@Value("root")
private String password;
@Bean
public DataSource dataSource() {
DruidDataSource ds = new DruidDataSource();
System.out.println("url:"+url);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
ds.setDriverClassName(driver);
return ds;
}
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
return sqlSessionFactoryBean.getObject();
}
@Bean
public DataSourceTransactionManager transactionManager(){
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource());
return dataSourceTransactionManager;
}
}
mapper
@Repository
public interface AccountMapper1 {
Account findAccountByName(String name);
int update(Account account);
}
<mapper namespace="com.cj.mapper.AccountMapper1">
<select id="findAccountByName" resultType="com.cj.bean.Account" parameterType="java.lang.String">
select * from account where name= #{name}
</select>
<update id="update">
update account set money=#{money} where name=#{name}
</update>
</mapper>
service
@Component
@Transactional
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper1 accountMapper;
@Override
public void transfer(String send, String recieve, Double money) {
Account sendA = accountMapper.findAccountByName(send);
Account recieveA = accountMapper.findAccountByName(recieve);
sendA.setMoney(sendA.getMoney()-money);
recieveA.setMoney(recieveA.getMoney()+money);
accountMapper.update(sendA);
accountMapper.update(recieveA);
}
}
@Aspect
@Component
public class AccountTransaction {
@Pointcut("execution(* com.cj.service.*.*(..))")
public void pointcut(){}
@Before("pointcut()")
public void before(JoinPoint joinPoint){
Signature signature = joinPoint.getSignature();
System.out.println(signature.getName()+"执行了");
}
}
xml配置事务扫描器
<tx:annotation-driven transaction-manager="transactionManager"/>
测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ConfigApp.class)
public class Main {
@Autowired
AccountMapper1 accountMapper;
@Test
public void test0(){
System.out.println(accountMapper);
System.out.println(accountMapper.findAccountByName("aaa"));
}
@Autowired
AccountService accountService;
@Test
public void test1(){
accountService.transfer("aaa","bbb",500D);
}
|