Spring深入研究(三)
来源:互联网 发布:逻辑回归 知乎 编辑:程序博客网 时间:2024/05/22 14:19
- 基于aspectJ的注解aop操作
- jdbcTemplate实现crud操作添加 修改和删除操作
- 增加
- jdbcTemplate实现 CRUD 操作
- 查询实现
- 第一种 查询返回某一个值
- 第二种 查询返回对象
- 第三种 查询返回List
- 查询实现
- Spring配置c3p0连接池
- Spring配置c3p0连接池
- dao使用jdbcTemplate
- Spring的事务Api
- 事务概念
- Spring事务管理api
- Spring的事务管理
- 转账环境搭建
- 配置文件实现
- 注解实现
基于aspectJ的注解aop操作
使用注解方式实现aop操作
第一步 创建对象
<!-- 创建对象 --> <bean id="car" class="com.jia.aop.Car"></bean> <bean id="myCar" class="com.jia.aop.MyCar"></bean>
第二步 在spring核心配置文件中,开启aop操作
<!-- 开启aop操作 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
第三步 在增强类上面使用注解完成aop操作
@Aspectpublic class MyCar { /** * 在方法上面使用注解完成增强配置 */ @Before(value="execution(* com.jia.aop.Car.*(..))") public void before1() { System.out.println("MyCar====before()"); }}
jdbcTemplate实现crud操作(添加 修改和删除操作)
1 Spring框架一站式框架
- (1) 针对javaee三层,每一层都有解决技术
- (2) 在dao层,使用JDBCTemplate
2 Spring对不同的持久化层技术都进行封装
JDBC———->JdbcTemplate
Hibernate5.0——————>HibernateTemplate
MyBatis——————–>SqlMapClientTemplate
JPA—————————–>JpaTemplate
3 jdbcTemplate 使用和dbutils使用很相似,都对数据库进行crud操作
使用JdbcTemplate流程
- 导入jdbcTemplate使用的jar包
- spring-jdbc.RELEASE.jar
- spring-tx.RELEASE.jar
- 数据库驱动
- 创建对象,设置数据库信息
- 创建jdbcTemplate对象,设置数据源
- 调用jdbcTemplate对象里面的方法实现操作
增加
/** * 添加操作 */ @Test public void add() { // 创建对象,设置数据库信息 DriverManagerDataSource dataSource=new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql:///tt_1"); dataSource.setUsername("root"); dataSource.setPassword("root"); // 创建jdbcTemplate对象,设置数据源 JdbcTemplate template=new JdbcTemplate(dataSource); // 调用jdbcTemplate对象里面的方法实现操作 //创建sql语句 String sql="insert into a values(?)"; int rows = template.update(sql,"aldrich"); System.out.println(rows); }
jdbcTemplate实现 CRUD 操作
查询实现
第一种 查询返回某一个值
public <T> T queryForObject(String sql, Class<T> requiredType)
- (1) 第一个参数是sql语句
- (2) 第二个参数 返回类型的class
/** * JdbcTemplate实现查询操作 查询返回某一个值 */ @Test public void testCount() { // 创建对象,设置数据库信息 DriverManagerDataSource dataSource=new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql:///customer1"); dataSource.setUsername("root"); dataSource.setPassword("root"); // 创建jdbcTemplate对象,设置数据源 JdbcTemplate template=new JdbcTemplate(dataSource); // 调用jdbcTemplate对象里面的方法实现操作 //创建sql语句 String sql="select count(*) from customer"; int count = template.queryForObject(sql, Integer.class); System.out.println(count); }
/** * JDBC 实现查询操作 查询返回某一个值 */ @Test public void testJDBC() { Connection conn = null; PreparedStatement psmt = null; ResultSet rs = null; try { // 加载驱动 Class.forName("com.mysql.jdbc.Driver"); // 创建连接 conn = DriverManager.getConnection("jdbc:mysql:///customer1", "root", "root"); // 创建SQL语句 String sql = "select * from customer where name=?"; // 预编译sql psmt = conn.prepareStatement(sql); // 设置参数值 psmt.setString(1, "aldrich"); // 执行SQL rs = psmt.executeQuery(); // 遍历结果集 while (rs.next()) { // 得到返回结果 String id = rs.getString("id"); String name = rs.getString("name"); String gender = rs.getString("gender"); String type = rs.getString("type"); Customer customer = new Customer(); customer.setId(id); customer.setName(name); customer.setGender(gender); customer.setType(type); System.out.println(customer); } } catch (Exception e) { e.printStackTrace(); } finally { try { rs.close(); psmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
第二种 查询返回对象
public <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args)
- 第一个参数是sql语句
- 第二个参数是RowMapper是接口,类似于dbutils里面接口
- 第三个参数是 可变参数
/** * org.springframework.jdbc.core.RowMapper<>实现类 * @author hasee * */class MyRowMapper implements RowMapper<Customer> { @Override public Customer mapRow(ResultSet rs, int rowNum) throws SQLException { // 从结果集中得到数据 String id = rs.getString("id"); String name = rs.getString("name"); String gender = rs.getString("gender"); String type = rs.getString("type"); // 把得到的数据封装到对象里面 Customer customer = new Customer(); customer.setId(id); customer.setName(name); customer.setGender(gender); customer.setType(type); return customer; }}
第三种 查询返回List
public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args)
- sql语句
- RowMapper接口,自己写类实现数据封装
- 可变参数
// 创建对象,设置数据库信息 DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql:///customer1"); dataSource.setUsername("root"); dataSource.setPassword("root"); // 创建jdbcTemplate对象,设置数据源 JdbcTemplate template = new JdbcTemplate(dataSource); // 调用jdbcTemplate对象里面的方法实现操作 // 创建sql语句 String sql = "select * from customer"; List<Customer> query = template.query(sql, new MyRowMapper()); System.out.println(query.toString());
Spring配置c3p0连接池
Spring配置连接池和dao使用jdbcTemplate
Spring配置c3p0连接池
- 第一步 导入jar包
- c3p0.jar
- mchange-commons-java.jar
- 第二步 创建Spring配置文件,配置连接池
<!-- 配置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:///customer1"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean>
dao使用jdbcTemplate
- 创建service和dao,配置service和dao对象,在service注入dao对象
- 创建jdbcTemplate对象,把模板对象注入到dao里面
- 在jdbcTemplate对象里面注入
public class UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void add(){ userDao.add(); }}public class UserDao { /** * TODO 得到JdbcTemplate对象 */ private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } /** * 添加操作 */ public void add() { // TODO Auto-generated method stub }}
<!-- 创建service对象,在service注入dao对象 --> <bean id="userService" class="com.jia.c3p0.UserService"> <!-- 注入dao对象 --> <property name="userDao" ref="userDao"></property> </bean> <!-- 创建dao对象 --> <bean id="userDao" class="com.jia.c3p0.UserDao"> <!-- 注入jdbcTemplate对象 --> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <!-- 创建jdbcTemplate对象 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <!-- 把dataSource传递到模板对象里面 --> <property name="dataSource" ref="dataSource"></property> </bean>
Spring的事务Api
事务概念
- 什么是事务
- 事务是对数据库操作的最基本单元,指一组操作,要么都成功,有一个失败都失败.
- 事务特性
- 原子性
- 一致性
- 隔离性: 多个事务之间不会产生影响
- 持久性
- 不考虑隔离性产生读问题
- 脏读
- 不可重复读
- 虚读
- 幻读
- 解决读问题
- 设置隔离级别
Spring事务管理api
1 Spring事务管理两种方式
- 第一种 编程式事务管理
- 第二种 声明式事务管理
- 基于xml配置文件实现
- 基于注解实现
2 Spring事务管理的API介绍
- PlatformTransactionManager 事务管理器
- TransactionDefinition 事务定义信息(隔离 传播 超时 只读)
- TransactionStatus 事务具体运行状态
Spring的事务管理
转账环境搭建
1 创建数据库,添加数据
create database transfer_spring default character set utf8 collate utf8_general_ci; create table account ( id int(11) not null primary key auto_increment, username varchar(100), salary int default '0');
2 创建service和dao类,完成注入关系
- service层又叫业务逻辑层
- dao层,单纯对数据库操作层,在dao层不添加业务
配置文件实现
声明式事务管理(XML配置)
配置文件方式使用aop 思想配置
- 第一步 配置事务管理器
<!-- 第一步 配置事务管理器 --> <bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入dataSource --> <property name="dataSource" ref="dataSource"></property> </bean>
- 第二步 配置事务增强
<!-- 第二步 配置事务增强 --> <tx:advice id="txadvice" transaction-manager="transactionManager"> <!-- 做事务操作 --> <tx:attributes> <!-- 设置进行事务操作的方法匹配规则 --> <tx:method name="account*" propagation="REQUIRED"/> </tx:attributes> </tx:advice>
- 第三步 配置切面
<!-- 第三步 配置切面 --> <aop:config> <!-- 切入点 --> <aop:pointcut expression="execution(* com.jia.service.OrdersService.*(..))" id="pointcut1"/> <!-- 切面 --> <aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/> </aop:config>
注解实现
声明式事务管理(注解)
- 第一步 配置事务管理器
<!-- 第一步 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入dataSource --> <property name="dataSource" ref="dataSource"></property> </bean>
- 第二步 配置事务注解
<!-- 第二步 开启事务注解 --> <tx:annotation-driven transaction-manager="transactionManager"/>
- 第三步 在要使用事务的方法所在类上面添加注解
@Transactionalpublic class OrdersService {}
阅读全文
1 0
- Spring深入研究(三)
- Spring深入研究(一)
- Spring深入研究(二)
- Android 深入研究SQLite实例(三)
- 深入研究Clang(三) libclang
- Android 深入研究adb(三)
- Spring(之二)--深入研究
- ThreadLocal深入研究-spring
- Spring值得深入研究挖掘!
- Spring 3 MVC深入研究
- Spring Mvc3.0深入研究
- Spring 3 MVC深入研究
- Spring 3 MVC深入研究
- Spring 3 MVC深入研究
- Spring 3 MVC深入研究
- Spring 3 MVC深入研究
- Spring 3 MVC深入研究
- Spring 3 MVC深入研究
- 伸展树模板题
- Activity 启动模式(任务和返回栈)
- RecyclerView系列(一)添加头部、尾部View
- 廖雪峰的Git教程十--解决冲突
- linux串口编程
- Spring深入研究(三)
- 二叉树的一些总结
- 算法课第十三周作业 | Counting Bits
- LINUX GDB调试器
- 拾遗
- Android Studio常用技巧汇总
- 25道核心JavaScript面试题
- 1. ESP8266固件的编译(交叉编译工具链的建立)
- 查频段