Spring实现增删改查

桃扇骨 2022-01-23 13:49 485阅读 0赞

使用sping的Ioc的实现账户的CRUD(增删改查)

使用的知识点:

QueryRunner:SQL语句的操作对象,可以设置查询结果集的封装策略

创建方法:QueryRunner runner=new QueryRunner(dataSource)

ResultSetHandler:封装数据的策略的对象

BeanHandler:将SQL查询的结果集转换成一个javaBean

Account account=runner.query(sql,new BeanHandler(Account.class),3);

BeanListHandler:**将SQL查询的结果集转换成一个集合的对象,集合中存储javaBean**

用法:**list list=runner.query(sql,new BeanListHandler(Account.class));**

pom.xml中需要的依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>commons-dbutils</groupId>
  4. <artifactId>commons-dbutils</artifactId>
  5. <version>1.4</version>
  6. </dependency>
  7. <!--c3p0连接池-->
  8. <dependency>
  9. <groupId>c3p0</groupId>
  10. <artifactId>c3p0</artifactId>
  11. <version>0.9.1.2</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>mysql</groupId>
  15. <artifactId>mysql-connector-java</artifactId>
  16. <version>5.1.6</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>junit</groupId>
  20. <artifactId>junit</artifactId>
  21. <version>4.12</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework</groupId>
  25. <artifactId>spring-context</artifactId>
  26. <version>5.0.2.RELEASE</version>
  27. </dependency>
  28. </dependencies>

创建数据库和编写的实体类

表结构和数据

SQL语句

CREATE TABLE account(

id int primary key AUTO_increment,

name varchar(40),

money float

);

insert into account(name,money) values(‘aaa’,1000);
insert into account(name,money) values(‘bbb’,1000);
insert into account(name,money) values(‘ccc’,1000);

账户的实体类Account(domain)

  1. package com.itheiam.domain;
  2. import lombok.Data;
  3. import lombok.ToString;
  4. import java.io.Serializable;
  5. @Data
  6. @ToString
  7. public class Account implements Serializable {
  8. private Integer id;
  9. private String name;
  10. private Float money;
  11. }

编写持久层(Dao)

  1. /***
  2. *账户的持久层接口
  3. ***/
  4. public interface AccountDao {
  5. /**
  6. * 保存
  7. * @param account
  8. */
  9. void save(Account account);
  10. /**
  11. * 更新
  12. * @param account
  13. */
  14. void update(Account account);
  15. /**
  16. * 删除
  17. * @param accountId
  18. */
  19. void delete(Integer accountId);
  20. /**
  21. * 根据id查询
  22. * @param accountId
  23. * @return
  24. */
  25. Account findById(Integer accountId);
  26. /**
  27. * 查询所有
  28. * @return
  29. */
  30. List<Account> findAll();
  31. }

账户的持久层的实现类(AccountDaoImpl)

  1. /***
  2. *账户的持久层接口实现类
  3. ***/
  4. public class AccountDaoImpl implements AccountDao {
  5. private QueryRunner runner;
  6. /**
  7. * 保存操作
  8. * @param account
  9. */
  10. public void save(Account account) {
  11. try {
  12. runner.update("insert into account(name,money)values(?,?)",account.getName(),account.getMoney());
  13. } catch (SQLException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. /***
  18. * 修改操作
  19. * @param account
  20. */
  21. public void update(Account account) {
  22. try {
  23. runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
  24. } catch (SQLException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. /**
  29. * 删除操作
  30. * @param accountId
  31. */
  32. public void delete(Integer accountId) {
  33. try {
  34. runner.update("delete from account where id=?",accountId);
  35. } catch (SQLException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. /**
  40. * 根据ID查询
  41. * @param accountId
  42. * @return
  43. */
  44. public Account findById(Integer accountId) {
  45. try {
  46. return runner.query("select * from account where id=?",new BeanHandler<Account>(Account.class),accountId);
  47. } catch (SQLException e) {
  48. e.printStackTrace();
  49. }
  50. return null;
  51. }
  52. /***
  53. * 查询所有
  54. * @return
  55. */
  56. public List<Account> findAll() {
  57. try {
  58. return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
  59. } catch (SQLException e) {
  60. e.printStackTrace();
  61. }
  62. return null;
  63. }
  64. /***
  65. * 注入QueryRunner对象
  66. * @param runner
  67. */
  68. public void setRunner(QueryRunner runner) {
  69. this.runner = runner;
  70. }
  71. }

编写业务层的接口(Service)

  1. /****
  2. * 业务层接口
  3. ***/
  4. public interface AccountService {
  5. /**
  6. * 保存
  7. * @param account
  8. */
  9. void save(Account account);
  10. /**
  11. * 更新
  12. * @param account
  13. */
  14. void update(Account account);
  15. /**
  16. * 删除
  17. * @param accountId
  18. */
  19. void delete(Integer accountId);
  20. /**
  21. * 根据id查询
  22. * @param accountId
  23. * @return
  24. */
  25. Account findById(Integer accountId);
  26. /**
  27. * 查询所有
  28. * @return
  29. */
  30. List<Account> findAll();
  31. }

编写业务层实现类(AccountServiceImpl)

  1. /****
  2. * 业务层接口实现类
  3. ***/
  4. public class AccountServiceImpl implements AccountService {
  5. private AccountDao accountDao;
  6. public void save(Account account) {
  7. accountDao.save(account);
  8. }
  9. public void update(Account account) {
  10. accountDao.update(account);
  11. }
  12. public void delete(Integer accountId) {
  13. accountDao.delete(accountId);
  14. }
  15. public Account findById(Integer accountId) {
  16. return accountDao.findById(accountId);
  17. }
  18. public List<Account> findAll() {
  19. return accountDao.findAll();
  20. }
  21. public void setAccountDao(AccountDao accountDao) {
  22. this.accountDao = accountDao;
  23. }

创建配置文件XML

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9. <!--创建AccountServiceImpl Bean-->
  10. <bean class="com.itheima.service.impl.AccountServiceImpl" id="accountService">
  11. <property name="accountDao" ref="accountDao" />
  12. </bean>
  13. <!--创建AccountDaoImpl Bean对象-->
  14. <bean class="com.itheima.dao.impl.AccountDaoImpl" id="accountDao">
  15. <property name="runner" ref="runner" />
  16. </bean>
  17. <!--创建QueryRunner对象-->
  18. <bean class="org.apache.commons.dbutils.QueryRunner" id="runner" scope="prototype">
  19. <constructor-arg name="ds" ref="dataSource" />
  20. </bean>
  21. <!--创建数据源-->
  22. <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
  23. <property name="driverClass" value="com.mysql.jdbc.Driver" />
  24. <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/spring5" />
  25. <property name="user" value="root" />
  26. <property name="password" value="123456" />
  27. </bean>
  28. </beans>

编写测试代码(Test)

  1. public class AccountServiceTest {
  2. private AccountService accountService;
  3. @Before
  4. public void init(){
  5. ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
  6. accountService = ac.getBean(AccountService.class);
  7. }
  8. /**
  9. * 保存
  10. */
  11. @Test
  12. public void testSave(){
  13. Account account = new Account();
  14. account.setMoney(999f);
  15. account.setName("小红");
  16. accountService.save(account);
  17. }
  18. /**
  19. * 更新
  20. */
  21. @Test
  22. public void testUpdate(){
  23. //查询出id=3的账户,再修改
  24. Account account = accountService.findById(4);
  25. account.setName("zhangsan");
  26. //修改
  27. accountService.update(account);
  28. }
  29. /**
  30. * 删除
  31. */
  32. @Test
  33. public void testDelete(){
  34. Integer accountId=3;
  35. accountService.delete(accountId);
  36. }
  37. /**
  38. * 根据id查询
  39. * @return
  40. */
  41. @Test
  42. public void testFindById(){
  43. Integer accountId=3;
  44. Account account = accountService.findById(accountId);
  45. System.out.println(account);
  46. }
  47. /**
  48. * 查询所有
  49. * @return
  50. */
  51. @Test
  52. public void testFindAll(){
  53. List<Account> accounts = accountService.findAll();
  54. for (Account account : accounts) {
  55. System.out.println(account);
  56. }
  57. }
  58. }

发表评论

表情:
评论列表 (有 0 条评论,485人围观)

还没有评论,来说两句吧...

相关阅读