SSM框架搭建之spring-datasource.xml配置
SpringMVC作为前端控制器,Spring容器负责管理Bean,MyBatis完成ORM映射,采用c3p0数据库连接池,也可以采用阿里的druid连接池,这里先采用c3p0连接池.
MyBatis-Spring整合
- spring-datasource.xml需要配置的bean
1.引入数据库连接池的properties文件
2.配置c3p0连接池
3.配置SqlSessionFactoryBean
1).配置dataSource;
2).配置*Mapper.xml文件的映射路径,例如:
<property> name=“mapperLocations”
value=“classpath*:mappers/*Mapper.xml”></property>
3).配置MyBatis的配置文件路径(关于缓存等配置的xml文件)(可选),这里我没有去配置MyBatis的setting.
4.Mapper接口的扫描类配置(org.mybatis.spring.mapper.MapperScannerConfigurer)
---------------以上是基础配置,可以通过配置jdbcTemplate,使用junit进行测试---------------
5(可选).jdbcTemplate的配置,只需要配置一个dataSource的属性就可以通过junit进行测试
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
6.事务的配置:
1).声明式事务配置;
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
需要注解驱动:
<!--注解事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
这样就可以在需要事务的方法上加上注解:
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
这样就可以使用事务了
2).另外就是aop代理事务,配置如下:
<!--通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes> <!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="create*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--6.aspectj支持自动代理实现AOP功能-->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* cn.app.service.impl.EmpServiceImpl.*(..))" />
</aop:config>
拦截EmpServiceImpl类的方法,可以在方法中加入一个int a =1/0的除零异常,进行测试.
下面是spring-datasource.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描包-->
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="cn.txx"/>
<!--1.引入属性文件-->
<context:property-placeholder location="classpath:datasource.properties"/>
<!--2.配置c3p0数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="acquireIncrement" value="${jdbc.acquireIncrement}"></property>
<property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
<property name="minPoolSize" value="${jdbc.minPoolSize}"></property>
</bean>
<!--3.会话工厂bean sqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--mybatis.xml中配置mybatis的缓存等相关内容-->
<!--<property name="configLocation" value="classpath:mybatis/mybatis.xml"/>-->
<!--sql映射文件-->
<property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"></property>
</bean>
<!--4.自动扫描对象关系映射-->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="cn.app.mapper"></property>
<property name="annotationClass" value="org.springframework.stereotype.Repository"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--5.声明式事务-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--注解事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes> <!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="create*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--6.aspectj支持自动代理实现AOP功能-->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* cn.app.service.impl.EmpServiceImpl.*(..))" />
</aop:config>
</beans>
测试类代码:
public class TxTest {
@Test
public void test(){
String path = "spring/spring-datasource.xml";
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(path);
IEmpService empService = context.getBean(IEmpService.class);
System.out.println(empService.insertEmp());
}
}
下面式service的代码:
@Service("empService")
public class EmpServiceImpl implements IEmpService {
@Autowired
EmpMapper empMapper;
@Override
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public List<Emp> getAllEmp() {
return empMapper.selectAll();
}
/*
使用注解事务时将事务前的注释删除
*/
@Override
// @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public int insertEmp() {
int i = empMapper.insertEmp();
int a = 1/0;
return i;
}
}
*Mapper接口以及相关xml文件可以使用MyBatis提供的插件去生成,有时间的话写个专门讲这个插件的文章.
请多指教.
ps:机会多半都是自己放走的.