Spring实现增删改查

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

使用的知识点:

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

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

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

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

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

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

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

pom.xml中需要的依赖

<dependencies>
    <dependency>
        <groupId>commons-dbutils</groupId>
        <artifactId>commons-dbutils</artifactId>
        <version>1.4</version>
    </dependency>
    <!--c3p0连接池-->
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1.2</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
</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)

package com.itheiam.domain;

import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
@Data
@ToString
public class Account implements Serializable {
    private Integer id;
    private String name;
    private Float money;
}

编写持久层(Dao)

/***
*账户的持久层接口
***/
public interface AccountDao {

    /**
     * 保存
     * @param account
     */
    void save(Account account);

    /**
     * 更新
     * @param account
     */
    void update(Account account);

    /**
     * 删除
     * @param accountId
     */
    void delete(Integer accountId);

    /**
     * 根据id查询
     * @param accountId
     * @return
     */
    Account findById(Integer accountId);

    /**
     * 查询所有
     * @return
     */
    List<Account> findAll();
}

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

/***
*账户的持久层接口实现类
***/
public class AccountDaoImpl implements AccountDao {

    private QueryRunner runner;

    /**
     * 保存操作
     * @param account
     */
    public void save(Account account) {
        try {
            runner.update("insert into account(name,money)values(?,?)",account.getName(),account.getMoney());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /***
     * 修改操作
     * @param account
     */
    public void update(Account account) {
        try {
            runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除操作
     * @param accountId
     */
    public void delete(Integer accountId) {
        try {
            runner.update("delete from account where id=?",accountId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据ID查询
     * @param accountId
     * @return
     */
    public Account findById(Integer accountId) {
        try {
            return runner.query("select * from account where id=?",new BeanHandler<Account>(Account.class),accountId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;
    }

    /***
     * 查询所有
     * @return
     */
    public List<Account> findAll() {
        try {
            return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;
    }

    /***
     * 注入QueryRunner对象
     * @param runner
     */
    public void setRunner(QueryRunner runner) {
        this.runner = runner;
    }
}

编写业务层的接口(Service)

/****
* 业务层接口
***/
public interface AccountService {

    /**
     * 保存
     * @param account
     */
    void save(Account account);

    /**
     * 更新
     * @param account
     */
    void update(Account account);

    /**
     * 删除
     * @param accountId
     */
    void delete(Integer accountId);

    /**
     * 根据id查询
     * @param accountId
     * @return
     */
    Account findById(Integer accountId);

    /**
     * 查询所有
     * @return
     */
    List<Account> findAll();
}

编写业务层实现类(AccountServiceImpl)

/****
* 业务层接口实现类
***/
public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;

    public void save(Account account) {
        accountDao.save(account);
    }

    public void update(Account account) {
        accountDao.update(account);
    }

    public void delete(Integer accountId) {
        accountDao.delete(accountId);
    }

    public Account findById(Integer accountId) {
        return accountDao.findById(accountId);
    }

    public List<Account> findAll() {
        return accountDao.findAll();
    }

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

创建配置文件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: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/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!--创建AccountServiceImpl Bean-->
    <bean class="com.itheima.service.impl.AccountServiceImpl" id="accountService">
        <property name="accountDao" ref="accountDao" />
    </bean>

    <!--创建AccountDaoImpl Bean对象-->
    <bean class="com.itheima.dao.impl.AccountDaoImpl" id="accountDao">
        <property name="runner" ref="runner" />
    </bean>

    <!--创建QueryRunner对象-->
    <bean class="org.apache.commons.dbutils.QueryRunner" id="runner" scope="prototype">
        <constructor-arg name="ds" ref="dataSource" />
    </bean>

    <!--创建数据源-->
    <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/spring5" />
        <property name="user" value="root" />
        <property name="password" value="123456" />
    </bean>
</beans>

编写测试代码(Test)

public class AccountServiceTest {

    private AccountService accountService;

    @Before
    public void init(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        accountService = ac.getBean(AccountService.class);
    }

    /**
     * 保存
     */
    @Test
    public void testSave(){
        Account account = new Account();
        account.setMoney(999f);
        account.setName("小红");

        accountService.save(account);
    }

    /**
     * 更新
     */
    @Test
    public void testUpdate(){
        //查询出id=3的账户,再修改
        Account account = accountService.findById(4);
        account.setName("zhangsan");
        //修改
        accountService.update(account);
    }

    /**
     * 删除
     */
    @Test
    public void testDelete(){
        Integer accountId=3;
        accountService.delete(accountId);
    }

    /**
     * 根据id查询
     * @return
     */
    @Test
    public void testFindById(){
        Integer accountId=3;
        Account account = accountService.findById(accountId);
        System.out.println(account);
    }

    /**
     * 查询所有
     * @return
     */
    @Test
    public void testFindAll(){
        List<Account> accounts = accountService.findAll();
        for (Account account : accounts) {
            System.out.println(account);
        }
    }
}

 

  • 4
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值