基于AspectJ的注解AOP操作
- 开启AOP操作及类的配置
<!-- 开启自动代理(aop操作的注解方式) -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 创建对象 -->
<bean id="user" class="tqb.anno.test.User"></bean>
<bean id="myUser" class="tqb.anno.test.MyUser"></bean>
- 相关类
public class User {
public void add(){
System.out.println("add。。。。。。。");
}
}
@Aspect
public class MyUser {
//切入点
@Before(value="execution(* tqb.anno.test.User.*(..))")
public void before(){
System.out.println("前置增强。。。。。。。。");
}
@AfterReturning(value="execution(* tqb.anno.test.User.*(..))")
public void after(){
System.out.println("后置增强。。。。。。。。");
}
@Around(value="execution(* tqb.anno.test.User.*(..))")
public void around(ProceedingJoinPoint point) throws Throwable{
System.out.println("环绕增强。。。。。。。前");
point.proceed();
System.out.println("环绕增强。。。。。。。后");
}
}
public class TestDemo {
@Test
public void fun(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user");
user.add();
}
}
JdbcTemplate实现的crud操作
- 添加操作
public class InsertDemo {
@Test
public void insert(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///spring_03");
dataSource.setUsername("root");
dataSource.setPassword("");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "insert into t_user value(?,?)";
jdbcTemplate.update(sql,"zhangsan","123456");
}
}
- 修改操作
public class UpdateDemo {
@Test
public void update(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///spring_03");
dataSource.setUsername("root");
dataSource.setPassword("123321tqb");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "update t_user set password=? where username=?";
jdbcTemplate.update(sql,"654321","zhangsan");
}
}
- 删除操作
public class DeleteDemo {
@Test
public void delete(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///spring_03");
dataSource.setUsername("root");
dataSource.setPassword("123321tqb");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "delete from t_user where username=?";
jdbcTemplate.update(sql,"zhangsan");
}
}
-
查询操作
注:查询操作对于查出来的数据需要自己进行数据的封装(大部分功能框架已实现,只需要实现RowMapper接口)- 查询具体的数值(如
select count(*) from t_user
,这里可直接采用Integer.class对数据进行封装)
//返回具体的值 @Test public void update1(){ DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql:///spring_03"); dataSource.setUsername("root"); dataSource.setPassword("123321tqb"); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); String sql = "select count(*) from t_user"; Integer integer = jdbcTemplate.queryForObject(sql, Integer.class); System.out.println(integer); }
- 返回一个对象(这里需要实现RowMapper接口)
注:实现类MyRowMapper可以对单个对象(如:user)和List集合进行封装,只不过查询采用的方法不同,需要注意
public class MyRowMapper implements RowMapper<User>{ @Override public User mapRow(ResultSet rs, int i) throws SQLException { String username = rs.getString("username"); String password = rs.getString("password"); User user = new User(); user.setUsername(username); user.setPassword(password); return user; } }
//返回对象 @Test public void update2(){ DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql:///spring_03"); dataSource.setUsername("root"); dataSource.setPassword("123321tqb"); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); String sql = "select * from t_user where username=?"; User user = jdbcTemplate.queryForObject(sql,new MyRowMapper(),"zhangsan"); System.out.println(user); }
- 返回一个List集合
//返回list集合 @Test public void update3(){ DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql:///spring_03"); dataSource.setUsername("root"); dataSource.setPassword("123321tqb"); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); String sql = "select * from t_user"; List<User> list = jdbcTemplate.query(sql,new MyRowMapper()); System.out.println(list.get(0)); }
- 查询具体的数值(如
Spring配置c3p0连接池
- 导入jar包(红框)
- 配置xml文件
<!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring_03"></property>
<property name="user" value="root"></property>
<property name="password" value=""></property>
</bean>
DAO层使用JdbcTemplate
- xml配置(结合c3p0的配置)
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring_03"></property>
<property name="user" value="root"></property>
<property name="password" value=""></property>
</bean>
<bean id="userService" class="tqb.jdbc.test.c3p0.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userDao" class="tqb.jdbc.test.c3p0.UserDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
- 相关类
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
userDao.add();
}
}
public class UserDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void add() {
String sql = "insert into t_user value(?,?)";
int update = jdbcTemplate.update(sql,"lisi","111111");
System.out.println(update);
}
}
public class TestDemo {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}
Spring的事务管理
- Spring事务的管理方式
- 编程式事务管理(不用)
- 声明式事务管理
- xml配置实现
- 注解实现
- Spring事务管理的API
- 接口:PlatformTransactionManage:事务管理器
- 实现类:
- 使用Spring JDBC:DataSourceTransactionManage
- 使用Hibernate:HibernateTransactionManage
- 约束
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
</beans>
- 场景:转账服务(发生异常进行回滚,不修改数据)
- 配置文件
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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"> <!-- 配置c3p0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///spring_03"></property> <property name="user" value="root"></property> <property name="password" value=""></property> </bean> <bean id="userService" class="tqb.tx.test.UserService"> <property name="userDao" ref="userDao"></property> </bean> <bean id="userDao" class="tqb.tx.test.UserDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core. > <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入dataSource --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事务增强 --> <tx:advice id="adviceId" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="zhuanZhang"/><!-- zhuanZhang* --> </tx:attributes> </tx:advice> <aop:config> <!-- 配置切入点 --> <aop:pointcut expression="execution(* tqb.tx.test.UserService.*(..))" id="pointcut1"/> <!-- 配置切面 --> <aop:advisor advice-ref="adviceId" pointcut-ref="pointcut1"/> </aop:config> </beans>
- 相关类
public class UserDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } /* * 转账方 */ public void less() { String sql = "update t_trans set salary=salary-? where username=?"; jdbcTemplate.update(sql,1000,"zhangsan"); } /* * 收钱方 */ public void more() { String sql = "update t_trans set salary=salary+? where username=?"; jdbcTemplate.update(sql,1000,"lisi"); } }
public class UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } /* * 转账 */ public void zhuanZhang(){ userDao.less(); userDao.more(); } }
public class TestDemo { @Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext3.xml"); UserService userService = (UserService) context.getBean("userService"); userService.zhuanZhang(); } }
声明的方式进行事务管理
- 配置文件
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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">
<!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring_03"></property>
<property name="user" value="root"></property>
<property name="password" value=""></property>
</bean>
<bean id="userService" class="tqb.tx.anno.test.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userDao" class="tqb.tx.anno.test.UserDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入dataSource -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
- 注解:@Transactional
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
/* * 转账 */
public void zhuanZhang(){
userDao.less();
//int i = 10/0;
userDao.more();
}
}