6.spring中数据库的增删改查

1.看图

 2.实体Account:

package com.domin;

import java.io.Serializable;

//账户的实体类(继承了一个Serializable)
public class Account implements Serializable {
    private  Integer id;
    private  String name;
    private  Float money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Float getMoney() {
        return money;
    }

    public void setMoney(Float money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

3.业务层接口IAccountService:

package com.service;

import com.domin.Account;

import java.util.List;

//账户的业务层接口
public interface IAccountService {
    /*查询所有*/
    List<Account> findAllAccount();
    /*查询一个:类型 变量*/
    Account findAccountById(Integer accountId);
    /*保存操作*/
    void saveAccount(Account account);
    /*更新操作*/
    void updateAccout(Account account);
    /*删除操作*/
    void delectAccout(Integer accountId);
}

4.业务层实现类AccountServiceIpm:

package com.service.Ipm;

import com.dao.IAccountDao;
import com.domin.Account;
import com.service.IAccountService;

import java.util.List;

/*账户的实现类*/
public class AccountServiceIpm implements IAccountService {
// 访问修饰符 类类型 对象;
   private IAccountDao accountDao;
    //accountDao的set方法
    public void setAccountDao(IAccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public List<Account> findAllAccount() {
        return accountDao.findAllAccount();
    }

    @Override
    public Account findAccountById(Integer accountId) {
        return accountDao.findAccountById(accountId);
    }

    @Override
    public void saveAccount(Account account) {
        accountDao.saveAccount(account);
    }

    @Override
    public void updateAccout(Account account) {
        accountDao.updateAccout(account);
    }

    @Override
    public void delectAccout(Integer accountId) {
        accountDao.delectAccout(accountId);
    }
}

5.持久层接口IAccountDao:

package com.dao;

import com.domin.Account;

import java.util.List;

public interface IAccountDao {
    /*查询所有*/
    List<Account> findAllAccount();
    /*查询一个*/
    Account findAccountById(Integer accountId);
    /*保存操作*/
    void saveAccount(Account account);
    /*更新操作*/
    void updateAccout(Account account);
    /*删除操作*/
    void delectAccout(Integer accountId);
}

6.持久层实现类:AccountDaoImp

package com.dao.Ipm;

import com.dao.IAccountDao;
import com.domin.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

/*账户的持久层实现类*/
public class AccountDaoImp implements IAccountDao {
    private QueryRunner runner;
    /*让spring容器提供的set方法*/
    /*使用runner对象*/
    public void setRunner(QueryRunner runner) {

        this.runner = runner;
    }

    @Override
    public List<Account> findAllAccount() {
        /*泛型使用字解码创建对象*/
        try{
            return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public Account findAccountById(Integer accountId) {
        try{
            return runner.query("select * from account where id = ?",new BeanHandler<Account>(Account.class),accountId);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public void saveAccount(Account account) {
        try{
            runner.update("insert into account(name,money)value(?,?)",account.getName(),account.getMoney());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateAccout(Account account) {
        try{
            runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public void delectAccout(Integer accountId) {
        try{
            runner.update("delete from account where id=?",accountId);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}

7.bean.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--        配置IAccountService的实例-->
    <bean id="accountService" class="com.service.Ipm.AccountServiceIpm">
        <!--注入类的实例accountDao,so创建持久层类的实例就不用写了-->
        <!--这时候还没有accountDao的创建实体类,这时候他会报错,所以引入一下创建accountDao对象,然后这里注入的地方就是实现了调用:也就是说:注入accountDao的调用,实现类层调用-->
        <property name="accountDao" ref="accountDao"></property>
    </bean>
//方法也要注入
    <bean id="accountDao" class="com.dao.Ipm.AccountDaoImp">
        <property name="runner" ref="runner"></property>
    </bean>
    <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
        <!--注入数据源ds-->
        <constructor-arg name="ds" ref="dataSource"></constructor-arg>
    </bean>
    <!--引入配置数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--连接数据库的必备信息-->
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/数据库名"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>

</beans>

8.测试类TestCode

package com.itheima;

import com.domin.Account;
import com.service.IAccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class TestCode {
    @Test
    public void findAllAccount() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = ac.getBean("accountService",IAccountService.class);
        List<Account> accounts = as.findAllAccount();
        //每一个a的内容  Account类型
        for (Account a: accounts) {
            System.out.println(a);
        }
    }
    @Test
    public void findAccountById() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = ac.getBean("accountService",IAccountService.class);
        Account findById = as.findAccountById(1);
        System.out.println(findById);
    }
    @Test
    public void saveAccount() {
        Account account = new Account();
        account.setName("zhou");
        account.setMoney((float) 1000.00);
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = ac.getBean("accountService",IAccountService.class);
        as.saveAccount(account);
        System.out.println("添加成功");
    }
    @Test
    public void updateAccout() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = ac.getBean("accountService",IAccountService.class);
        //执行方法
        //先取到id
        Account acc = as.findAccountById(3);
        //添加
        acc.setName("qi");
        //添加
        acc.setMoney((float) 10000.00);
        //添加的保存起来
        as.updateAccout(acc);
        System.out.println("更新成功");

    }
    @Test
    public void delectAccout() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = ac.getBean("accountService",IAccountService.class);
//      as.findAccountById(1);
        as.delectAccout(2);
        System.out.println("删除成功");
    }
}

9.依赖导入pom.xml

<!--依赖添加-->
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>commons-dbutils</groupId>
      <artifactId>commons-dbutils</artifactId>
      <version>1.6</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
    </dependency>

    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
    </dependency>

  </dependencies>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值