zoukankan      html  css  js  c++  java
  • commons-dbutils实现增删改查

    1、maven依赖

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.ly.spring</groupId>
        <artifactId>spring03</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.0.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.6</version>
            </dependency>
            <dependency>
                <groupId>commons-dbutils</groupId>
                <artifactId>commons-dbutils</artifactId>
                <version>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>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>

    2、实体类

    package com.ly.spring.domain;
    
    import java.io.Serializable;
    
    public class Account implements Serializable {
        private Integer id;
        private Integer uid;
        private double money;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public Integer getUid() {
            return uid;
        }
    
        public void setUid(Integer uid) {
            this.uid = uid;
        }
    
        public double getMoney() {
            return money;
        }
    
        public void setMoney(double money) {
            this.money = money;
        }
    
        @Override
        public String toString() {
            return "Account{" +
                    "id=" + id +
                    ", uid=" + uid +
                    ", money=" + money +
                    '}';
        }
    }

    3、Service接口

    package com.ly.spring.service;
    
    import com.ly.spring.domain.Account;
    
    import java.sql.SQLException;
    import java.util.List;
    
    public interface IAccountService {
        public List<Account> findAll() throws SQLException;
        public Account findOne(Integer id) throws SQLException;
        public void save(Account account) throws SQLException;
        public void update(Account account) throws SQLException;
        public void delete(Integer id) throws SQLException;
    }

    4、Service实现类

    package com.ly.spring.service.impl;
    
    import com.ly.spring.dao.IAccountDao;
    import com.ly.spring.domain.Account;
    import com.ly.spring.service.IAccountService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.sql.SQLException;
    import java.util.List;
    
    @Service("accountService")
    public class AccountServiceImpl implements IAccountService {
        @Autowired
        private IAccountDao accountDao;
        public List<Account> findAll() throws SQLException {
            return accountDao.findAll();
        }
    
        @Override
        public Account findOne(Integer id) throws SQLException {
            return accountDao.findOne(id);
        }
    
        @Override
        public void save(Account account) throws SQLException {
            accountDao.save(account);
        }
    
        @Override
        public void update(Account account) throws SQLException {
            accountDao.update(account);
        }
    
        @Override
        public void delete(Integer id) throws SQLException {
            accountDao.delete(id);
        }
    }

    5、Dao接口

    package com.ly.spring.dao;
    
    import com.ly.spring.domain.Account;
    
    import java.sql.SQLException;
    import java.util.List;
    
    public interface IAccountDao {
        public List<Account> findAll() throws SQLException;
        public Account findOne(Integer id) throws SQLException;
        public void save(Account account) throws SQLException;
        public void update(Account account) throws SQLException;
        public void delete(Integer id) throws SQLException;
    }

    6、Dao实现类

    package com.ly.spring.dao.impl;
    
    import com.ly.spring.dao.IAccountDao;
    import com.ly.spring.domain.Account;
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.BeanHandler;
    import org.apache.commons.dbutils.handlers.BeanListHandler;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;
    
    import java.sql.SQLException;
    import java.util.List;
    
    @Repository("accountDao")
    public class AccountDaoImpl implements IAccountDao {
        @Autowired
        private QueryRunner queryRunner;
        public List<Account> findAll() throws SQLException {
            return queryRunner.query("select * from account",new BeanListHandler<Account>(Account.class));
        }
    
        @Override
        public Account findOne(Integer id) throws SQLException {
            return queryRunner.query("select * from account where id = ?",new BeanHandler<Account>(Account.class),id);
        }
    
        @Override
        public void save(Account account) throws SQLException {
            queryRunner.update("insert into account(uid,money) values(?,?)",account.getUid(),account.getMoney());
        }
    
        @Override
        public void update(Account account) throws SQLException {
            queryRunner.update("update account set money = ? where id = ?",account.getMoney(),account.getId());
        }
    
        @Override
        public void delete(Integer id) throws SQLException {
            queryRunner.update("delete from account where id = ?",id);
        }
    }

    7、测试类

    package com.ly.spring.test;
    import com.ly.spring.domain.Account;
    import com.ly.spring.service.IAccountService;
    import org.junit.Before;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.sql.SQLException;
    import java.util.List;
    
    public class MainTest {
        private ApplicationContext context;
        private IAccountService accountService;
        @Before
        public void init() {
            context = new ClassPathXmlApplicationContext("bean.xml");
            accountService = context.getBean("accountService", IAccountService.class);
        }
        @Test
        public void findAll() throws SQLException {
            List<Account> accounts = accountService.findAll();
            System.out.println(accounts);
        }
    
        @Test
        public void findOne() throws SQLException {
            Account account = accountService.findOne(3);
            System.out.println(account);
        }
    
        @Test
        public void save() throws SQLException {
            Account account = new Account();
            account.setUid(51);
            account.setMoney(8000);
            accountService.save(account);
        }
    
        @Test
        public void update() throws SQLException {
            Account account = new Account();
            account.setId(4);
            account.setMoney(100000);
            accountService.update(account);
        }
        @Test
        public void delete() throws SQLException {
            accountService.delete(4);
        }
    }

    8、spring配置文件

    <?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">
        <!--配置注解扫描包-->
        <context:component-scan base-package="com.ly.spring"></context:component-scan>
        <!--引入外部properties文件-->
        <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
        <!--配置QueryRunner并注入数据源-->
        <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
            <constructor-arg name="ds" ref="dataSource"></constructor-arg>
        </bean>
        <!--配置c3p0数据源并注入相关属性-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="jdbcUrl" value="${jdbc.url}"></property>
            <property name="driverClass" value="${jdbc.driver}"></property>
            <property name="user" value="${jdbc.user}"></property>
            <property name="password" value="${jdbc.password}"></property>
        </bean>
    </beans>

    9、数据库配置文件

    jdbc.url = jdbc:mysql://localhost:3306/db01
    jdbc.driver = com.mysql.jdbc.Driver
    jdbc.user = root
    jdbc.password = root
  • 相关阅读:
    Maria 与Ann的故事
    引语
    Preface
    Chapter 1 Foundation
    Roman to Integer
    Integer to Roman
    Container with most water
    palindrome number
    String to Integer (atoi)
    Reverse Integer
  • 原文地址:https://www.cnblogs.com/liuyang-520/p/12340922.html
Copyright © 2011-2022 走看看