Spring与DAO

4.1 Spring 与 JDBC 模板

除了引入必要的jar包,还要导入sql数据库文件。

09-JDBC-Template
学生类 主要赋值三种属性

public class Student {private Integer id;private String name;private int age;public Student() {super();// TODO Auto-generated constructor stub}public Student(String name, int age) {super();this.name = name;this.age = age;}public Integer getId() {return id;}public String getName() {return name;}public int getAge() {return age;}public void setId(Integer id) {this.id = id;}public void setName(String name) {this.name = name;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";}}

学生方法的接口类

public interface IStudentService {void addStudent(Student student);void removeById(int id);void modifyStudent(Student student);List<String> findAllStudentsNames();String findStudentNameById(int id);List<Student> findAllStudents();Student findStudentById(int id);
}

学生方法的实现类
创建了DAO类对象,调用其方法返回值

public class StudentServiceImpl implements IStudentService {private IStudentDao dao;public void setDao(IStudentDao dao) {this.dao = dao;}@Overridepublic void addStudent(Student student) {dao.insertStudent(student);}@Overridepublic void removeById(int id) {dao.deleteById(id);}@Overridepublic void modifyStudent(Student student) {dao.updateStudent(student);}@Overridepublic List<String> findAllStudentsNames() {return dao.selectAllStudentsNames();}@Overridepublic String findStudentNameById(int id) {return dao.selectStudentNameById(id);}@Overridepublic List<Student> findAllStudents() {return dao.selectAllStudents();}@Overridepublic Student findStudentById(int id) {return dao.selectStudentById(id);}}

DAO接口类,各种查找方法

public interface IStudentDao {void insertStudent(Student student);void deleteById(int id);void updateStudent(Student student);List<String> selectAllStudentsNames();String selectStudentNameById(int id);List<Student> selectAllStudents();Student selectStudentById(int id);
}

DAO实现类并继承了JdbcDaoSupport来将sql语句写入jdbctemplate
实现了增删改查方法

public class StudentDaoImpl extends JdbcDaoSupport implements IStudentDao {@Overridepublic void insertStudent(Student student) {String sql = "insert into student(name,age) values(?,?)";this.getJdbcTemplate().update(sql, student.getName(), student.getAge());}@Overridepublic void deleteById(int id) {String sql = "delete from student where id = ?";this.getJdbcTemplate().update(sql, id);}@Overridepublic void updateStudent(Student student) {String sql = "update student set name=?, age=? where id=?";this.getJdbcTemplate().update(sql, student.getName(), student.getAge(), student.getId());}//只查询所有名字@Overridepublic List<String> selectAllStudentsNames() {String sql = "select name from student";return this.getJdbcTemplate().queryForList(sql, String.class);}//只查询指定id的名字@Overridepublic String selectStudentNameById(int id) {String sql = "select name from student where id=?";return this.getJdbcTemplate().queryForObject(sql, String.class, id);}//查询多个目标@Overridepublic List<Student> selectAllStudents() {String sql = "select id,name,age from student";return this.getJdbcTemplate().query(sql, new StudentRowMapper());}@Overridepublic Student selectStudentById(int id) {String sql = "select id,name,age from student where id=?";return this.getJdbcTemplate().queryForObject(sql, new StudentRowMapper(), id);}}

因为查询的方法中需要将多个属性赋给一个对象,所以我们用

pubic T queryForObject (String sql, RowMapper m , Object… args)
pubic List query (String sql, RowMapper m, Object… args)
注意,RowMapper 为记录映射接口,用于将查询结果集中每一条记录包装为指定对象。
该接口中有一个方法需要实现:
public Object mapRow(ResultSet rs, int rowNum)
参数 rowNum 表示总的结果集中当前行的行号,但参数 rs 并不表示总的结果集,而是
表示 rowNum 所代表的当前行的记录所定义的结果集,仅仅是当前行的结果。
一般,该方法体中就是实现将查询结果中当前行的数据包装为一个指定对象。

StudentRowMapper类 将多个属性赋给一个对象

public class StudentRowMapper implements RowMapper<Student> {//rs:当查询出总的结果集后,框架会自动遍历这个结果集,每一次遍历的一行数据,都会被存放到// 这个方法的rs参数中。也就是说,这里的rs代表的是一行数据,并非所有查询结果。换个角度// 来说,只要能执行到这个方法,就说明这里的rs不会是空的@Overridepublic Student mapRow(ResultSet rs, int arg1) throws SQLException {Student student = new Student();student.setId(rs.getInt("id"));student.setName(rs.getString("name"));student.setAge(rs.getInt("age"));return student;}}

配置文件
1、C3P0数据源找到注册驱动driver、url定位符、用户名、密码四个连接属性。
2、从属性文件读取数据库连接信息,使用context:property-placeholder/ 方式导入属性配置文件,要求在 Spring 配置文件头部加入 context 的约束,在属性文件中给出四个属性的值。
3、把数据源赋给DAO类,会自动生成JDBC 模板对象。
4、把DAO类再给学生实现方法类

JDBC 模板类 JdbcTemplate 从其父类 JdbcAccessor 继承了一个属性 dataSource,用于接收
数据源。

<!-- 注册数据源:C3P0 --><bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}"/><property name="jdbcUrl" value="${jdbc.url}"/><property name="user" value="${jdbc.user}"/><property name="password" value="${jdbc.password}"/></bean>这里不用//<!-- 注册属性文件:方式一 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:jdbc.properties"/></bean>--><!-- 注册属性文件:方式二 --><context:property-placeholder location="classpath:jdbc.properties"/>这里不用//<!-- 注册JdbcTemplate和Dao <bean id="myJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="myDataSource"/>//</bean>//<bean id="studentDao" class="com.bjpowernode.dao.StudentDaoImpl">//<property name="jdbcTemplate" ref="myJdbcTemplate"/>//</bean> --><!-- 注册Dao --><bean id="studentDao" class="com.bjpowernode.dao.StudentDaoImpl"><property name="dataSource" ref="myDataSource"/></bean><!-- 注册Service --><bean id="studentService" class="com.bjpwernode.service.StudentServiceImpl"><property name="dao" ref="studentDao"/></bean>

jdbc.properties配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test
jdbc.user=root
jdbc.password=333

测试代码:

public class MyTest {private IStudentService service;@Beforepublic void before() {String resource = "applicationContext.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(resource);service = (IStudentService) ac.getBean("studentService");}//增@Testpublic void test01() {Student student = new Student("张三", 23);service.addStudent(student);}//删@Testpublic void test02() {service.removeById(2);}//修改@Testpublic void test03() {Student student = new Student("张三", 23);student.setId(3);service.modifyStudent(student);}//查询@Testpublic void test04() {List<String> names = service.findAllStudentsNames();System.out.println(names);}@Testpublic void test05() {String name = service.findStudentNameById(3);System.out.println(name);}@Testpublic void test06() {List<Student> students = service.findAllStudents();for(Student student : students) {System.out.println(student);}}@Testpublic void test07() {Student student = service.findStudentById(3);System.out.println(student);}
}

可以使用Navicat查看结果

4.2 Spring的事务管理

举例:购买股票—transaction_buystock 项目
本例要实现模拟购买股票。存在两个实体:银行账户 Account 与股票账户 Stock。当要
购买股票时,需要从 Account 中扣除相应金额的存款,然后在 Stock 中增加相应的股票数量。
而在这个过程中,可能会抛出一个用户自定义的异常。异常的抛出,将会使两个操作回滚。

先建表
定义实体类账户

public class Account {private Integer aid;private String aname;private double balance; //余额public Account() {super();// TODO Auto-generated constructor stub}public Account(String aname, double balance) {super();this.aname = aname;this.balance = balance;}public Integer getAid() {return aid;}public String getAname() {return aname;}public double getBalance() {return balance;}public void setAid(Integer aid) {this.aid = aid;}public void setAname(String aname) {this.aname = aname;}public void setBalance(double balance) {this.balance = balance;}@Override public String toString() { return "Account [aid=" + aid +", aname=" + aname + ", balance=" + balance + "]"; }}

定义实体类股票

public class Stock {private Integer sid;private String sname;  // 股票名称private int count;    // 股票数量public Stock() {super();// TODO Auto-generated constructor stub}public Stock(String sname, int count) {super();this.sname = sname;this.count = count;}public Integer getSid() {return sid;}public String getSname() {return sname;}public int getCount() {return count;}public void setSid(Integer sid) {this.sid = sid;}public void setSname(String sname) {this.sname = sname;}public void setCount(int count) {this.count = count;}@Overridepublic String toString() {return "Stock [sid=" + sid + ", sname=" + sname + ", count=" + count + "]";}}

定义Dao层实现股票和账户的增和改,这里接口类就省略了

public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {@Overridepublic void insertAccount(String aname, double money) {String sql = "insert into account(aname, balance) values(?,?)";this.getJdbcTemplate().update(sql, aname, money);}@Overridepublic void updateAccount(String aname, double money, boolean isBuy) {String sql = "update account set balance=balance+? where aname=?";if (isBuy) {sql = "update account set balance=balance-? where aname=?";}this.getJdbcTemplate().update(sql, money, aname);}}
public class StockDaoImpl extends JdbcDaoSupport implements IStockDao {@Overridepublic void insertStock(String sname, int amount) {String sql = "insert into stock(sname, count) values(?,?)";this.getJdbcTemplate().update(sql, sname, amount);}@Overridepublic void updateStock(String sname, int amount, boolean isBuy) {String sql = "update stock set count=count-? where sname=?";if (isBuy) {sql = "update stock set count=count+? where sname=?";}this.getJdbcTemplate().update(sql, amount, sname);}}

定义service实现类,这里同样省略了接口。调用了Dao类的方法,并设置了一个购买异常。

public class BuyStockServiceImpl implements IBuyStockService {private IAccountDao adao;private IStockDao sdao;public void setAdao(IAccountDao adao) {this.adao = adao;}public void setSdao(IStockDao sdao) {this.sdao = sdao;}@Overridepublic void openAccount(String aname, double money) {adao.insertAccount(aname, money);}@Overridepublic void openStock(String sname, int amount) {sdao.insertStock(sname, amount);}@Overridepublic void buyStock(String aname, double money, String sname, int amount) throws BuyStockException{boolean isBuy = true;adao.updateAccount(aname, money, isBuy);if (1 == 1) {throw new BuyStockException("购买股票异常");}sdao.updateStock(sname, amount, isBuy);}

定义异常类

public class BuyStockException extends Exception {public BuyStockException() {super();// TODO Auto-generated constructor stub}public BuyStockException(String message) {super(message);// TODO Auto-generated constructor stub}}

jdbc配置文件同上
Spring配置文件:约束、xml注入、事务管理、AOP配置

<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- ======================= IoC ============================ --><!-- 注册数据源:C3P0 --><bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}"/><property name="jdbcUrl" value="${jdbc.url}"/><property name="user" value="${jdbc.user}"/><property name="password" value="${jdbc.password}"/></bean><!-- 注册属性文件 --><context:property-placeholder location="classpath:jdbc.properties"/><!-- 注册Dao --><bean id="accountDao" class="com.bjpowernode.dao.AccountDaoImpl"><property name="dataSource" ref="myDataSource"/></bean><!-- 注册Dao --><bean id="stockDao" class="com.bjpowernode.dao.StockDaoImpl"><property name="dataSource" ref="myDataSource"/></bean><!-- 注册Service --><bean id="buyStockService" class="com.bjpowernode.service.BuyStockServiceImpl"><property name="adao" ref="accountDao"/><property name="sdao" ref="stockDao"/></bean><!-- ======================= AOP ============================ --><!-- 注册事务管理器 --><bean id="myTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="myDataSource"/></bean><tx:advice id="txAdvice" transaction-manager="myTransactionManager"><tx:attributes><!-- 这里指定的是:为每一个连接点指定所要应用的事务属性 --><tx:method name="open*" isolation="DEFAULT" propagation="REQUIRED"/><tx:method name="buyStock" isolation="DEFAULT" propagation="REQUIRED" rollback-for="BuyStockException"/></tx:attributes></tx:advice><!-- AOP配置 --><aop:config><!-- 这里指定的是切入点 --><aop:pointcut expression="execution(* *..service.*.*(..))" id="myPointcut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/></aop:config></beans>

测试类

public class MyTest {private IBuyStockService service;@Beforepublic void before() {String resource = "applicationContext.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(resource);service = (IBuyStockService) ac.getBean("buyStockService");}@Testpublic void test01() {service.openAccount("张三", 10000);service.openStock("北京动力节点", 0);}@Testpublic void test02() throws BuyStockException {service.buyStock("张三", 2000, "北京动力节点", 5);}}

三大组件之Spring 第四章Spring与DAO相关推荐

  1. JAVA面试汇总第四章 Spring及数据库相关

    Spring 核心功能演示 + 面试题 Spring Framework 简称 Spring,是 Java 开发中最常用的框架,地位仅次于 Java API,就连近几年比较流行的微服务框架 Sprin ...

  2. Spring boot 第一章 Spring发展历史

    2019独角兽企业重金招聘Python工程师标准>>> Spring 的发展 1.Spring 1.x 时代 在spring 1.x 时代,都是通过XML文件配置bean,随着项目的 ...

  3. 框架学习之Spring 第四节 Spring集成JDBC组件开发

    1.与JDBC集成的配置步骤: ①配置数据源,如: 第一种方式:直接在XML中配置数据源:<bean id="dataSource" class="org.apac ...

  4. 第四章 Spring.Net 如何管理您的类___统一资源访问接口

    在前面章节有童鞋提到过 关于配置文件 Objects.xml 路径的相关问题,这些东西是 IResource 接口的一些内容,接下来就详细介绍一下 IResource 接口. IResource 接口 ...

  5. 学习Spring(四) -- Spring的继承与依赖

    2019独角兽企业重金招聘Python工程师标准>>> 继承 在spring配置文件中,在bean元素里可以使用parent属性来实现配置信息的继承,比如需要十个学生类的bean,其 ...

  6. 框架设计--第一章 Spring的基本应用--习题答案

    摘要:微信搜索[三桥君] 课程介绍:"框架技术"是软件工程专业的核心课程,是本专业限选课,是Java 应用开发课程,是本专业学生就业的主要方向. 说明:框架设计其他章节的习题答案也 ...

  7. Spring Boot基础学习笔记13:路径扫描整合Servlet三大组件

    文章目录 零.学习目标 一.创建Spring Boot项目 - IntegrateThreeComponents02 二.使用路径扫描方式整合Servlet三大组件 (一)创建MyServlet类 ( ...

  8. Spring Boot基础学习笔记12:组件注册整合Servlet三大组件

    文章目录 零.学习目标 一.整合Servlet三大组件概述 (一)组件注册整合Servlet三大组件 (二)路径扫描整合Servlet三大组件 二.创建Spring Boot项目 - Integrat ...

  9. 《深入理解 Spring Cloud 与微服务构建》第十四章 服务链路追踪 Spring Cloud Sleuth

    <深入理解 Spring Cloud 与微服务构建>第十四章 服务链路追踪 Spring Cloud Sleuth 文章目录 <深入理解 Spring Cloud 与微服务构建> ...

  10. 《深入理解 Spring Cloud 与微服务构建》第四章 Dubbo

    <深入理解 Spring Cloud 与微服务构建>第四章 Dubbo 文章目录 <深入理解 Spring Cloud 与微服务构建>第四章 Dubbo 一.Dubbo 简介 ...

最新文章

  1. 解决Python自带的json序列化工具不能序列化datetime类型数据问题
  2. python web为什么不火-pythonweb为什么不火-问答-阿里云开发者社区-阿里云
  3. git2.29.2.2怎么安装_制作Win10安装U盘时install.wim大于4G怎么办?
  4. 求1+2+……+N的和
  5. linux mysql定时备份并压缩
  6. python数据分析-数据准备
  7. 合并bin文件-----带boot发布版本比较好用的bat(便捷版)
  8. GNU/Linux与开源文化的那些人和事
  9. matlab 归一化_机器学习中如何用Fscore进行特征选择(附Matlab代码)
  10. 生命银行怎么样_银行双职工家庭现状实录
  11. android 图标 自定义,巧用 Drawable 之实现一个最简单的自定义电池图标
  12. 服务器iis建站维护,云服务器iis建站教程
  13. dpdk加速网络协议栈ANS用户手册
  14. 用vc++穷举windows应用程序密码
  15. undefined reference to `timersub‘ 错误处理
  16. 企业管理之道:中层管理者,到底该管理什么?
  17. Go应用构建工具(3)--cobra
  18. 效率工具集 uTools v1.3.1
  19. 简易功率测量仪代码解析(一)题目要求分析
  20. html5清新文艺,小清新文艺范句子大全

热门文章

  1. STM32F103 DMA方式GPIO输出
  2. 五百万微商都在用微信智能获客小程序,你还不加入?
  3. 江苏考生小高考计算机网,江苏小高考
  4. Chrome网页视频加速器介绍
  5. 2021年下半年软考信息安全工程师下午案例题及解析
  6. vue 点击图标 显示/隐藏 密码
  7. 毕业设计——阶段性成果展示
  8. 三维分布图 matlab,怎样用matlab画三维三点分布图
  9. Flink DataStream Keyed和Non-Keyed窗口、WindowAssigner、窗口生命周期、窗口函数、迟到数据和窗口结果的处理
  10. android睡眠伴侣,GitHub - HuangZengPei/Sleepmon: 我的Android团队项目,一款可以监测、统计睡眠质量情况的睡眠健康伴侣应用。...