Spring实现增删改查

使用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中需要的依赖


    
        commons-dbutils
        commons-dbutils
        1.4
    
    
    
        c3p0
        c3p0
        0.9.1.2
    
    
        mysql
        mysql-connector-java
        5.1.6
    
    
        junit
        junit
        4.12
    
    
        org.springframework
        spring-context
        5.0.2.RELEASE
    

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

表结构和数据

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 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.class),accountId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;
    }

    /***
     * 查询所有
     * @return
     */
    public List findAll() {
        try {
            return runner.query("select * from account",new BeanListHandler(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 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 findAll() {
        return accountDao.findAll();
    }

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

创建配置文件XML




    
    
        
    

    
    
        
    

    
    
        
    

    
    
        
        
        
        
    

编写测试代码(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 accounts = accountService.findAll();
        for (Account account : accounts) {
            System.out.println(account);
        }
    }
}

 

你可能感兴趣的:(java)