作者:jhb852 | 来源:互联网 | 2023-09-25 16:13
1.Spring同mybatis的整合步骤?
整合的代码:
"dataSource" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource">
"driverClass" value="oracle.jdbc.driver.OracleDriver"/>
"jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
"user" value="hotelsup"/>
"password" value="123"/>
"maxPoolSize" value="40"/>
"minPoolSize" value="1"/>
"initialPoolSize" value="1"/>
"maxIdleTime" value="60"/>
"checkoutTimeout" value="2000"/>
"sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
"dataSource" ref="dataSource"/>
"configLocation" value="classpath:mybatis-config.xml">
|
2.spring提供的aop对于业务层的事务实现(声明式事务处理)。
在applicationContext.xml里的配置:
"transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
"dataSource" ref="dataSource" />
"txAdvice" transaction-manager="transactionManager">
"*" rollback-for="EXCEPTION" propagation="REQUIRED"/>
"get*" propagation="NEVER"/>
"set*" propagation="NEVER"/>
"execution(* com.jinglin.hotelsup.service..*(..))" id="allMethods"/>
"txAdvice" pointcut-ref="allMethods"/>
"transactionManager" proxy-target-class="true" />
|
3.将三层架构中的dao层(mapper接口)以及业务层加入到spring中。(开启包的扫描,就是直接将一个包里所有的类扫描到spring中),首先扫描mapper代理对象(就是dao层),然后扫描service层(里面会用到自动装配)
"org.mybatis.spring.mapper.MapperScannerConfigurer">
"basePackage" value="com.jinglin.hotelsup.dao.imp">
"sqlSessionFactoryBeanName" value="sqlSessionFactory">
"com.jinglin.hotelsup.service"/>
|
然后在service层里注解每个service类,然后调用dao层的对象的时候直接自动装配,代码如下:
@Service表示的是它是一个业务层的类,被扫描到spring中的
@Transactional
@Service
public class UserInfoService {
//注入的dao层,自动装配
@Autowired
private UserInfoMapper userInfoMapper;
public List getallusers(){
return userInfoMapper.getlist();
}
public int adduser(UserInfo u){
userInfoMapper.additem(u);
return u.getUserid();
}
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public List allusers(){
return userInfoMapper.getlist();
}
}
|
spring同mybatis整合的总结及配置