Spring dark horse: SpringIOC Case (JDBC database): XML configuration


Spring entire horse parent pom.xml configuration
<modules> tag is a subclass

<?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.jh</groupId>
    <artifactId>Spring_黑马</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>Spring_Bean解耦</module>
        <module>SpringIOC</module>
        <module>SpringBean</module>
        <module>Spring依赖</module>
        <module>Spring注解</module>
        <module>SpringIOC_XML</module>
        <module>SpringIOC注解案例</module>
        <module>Spring新注解(无xml)</module>
    </modules>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</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.13</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

1. bean.xml configuration (key)

The following is a progressive layers invoked by the ref attribute layers call
(1) Configuration Service layer
(2) disposed Dao layer
(3) arranged QueryRunner objects
(4) configuration data source

<?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">
    <!--配置Service层-->
    <bean id="accountService" class="com.jh.service.impl.AccountServiceImpl">
        <!--注入dao-->
        <property name="accountDao" ref="accountDao"></property>
    </bean>

    <!--配置Dao层-->
    <bean id="accountDao" class="com.jh.dao.impl.AccountDaoImpl">
        <!--注入QueryRunner对象-->
        <property name="runner" ref="runner"></property>
    </bean>

    <!--配置QueryRunner对象-->
    <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
        <!--注入数据源-->
        <constructor-arg name="ds" ref="dataSource"></constructor-arg>
    </bean>

    <!--配置数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--连接数据库的必备信息-->
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/maven?serverTimezone=GMT%2B8"></property>
        <property name="user" value="root"></property>
        <property name="password" value="jh7851192"></property>
    </bean>
</beans>

2. dao layer, domain level, service level, a test class

(1) dao layer

IAccountDao Interface

public interface IAccountDao {
    //1. 查询所有
    List<Account> findAllAccount();
    //2.查询一个
    Account findAccountById(Integer accountId);
    //3.保存
    void saveAccount(Account account);
    //4.删除操作
    void deleteAccount(Integer accountId);
    //5.更新操作
    void updateAccount(Account account);
}

AccountDaoImpl implementation class

  • Here added QueryRunner objects;
  • SQL statement written in the persistence layer implementation class.
public class AccountDaoImpl implements IAccountDao {
  
    private QueryRunner runner;
    public QueryRunner getRunner() {
        return runner;
    }
    public void setRunner(QueryRunner runner) {
        this.runner = runner;
    }
    @Override
    public List<Account> findAllAccount() {
        try {
            //这里是在Spring里应用数据库连接(sql语句查询);
            //Account.class是查询的实体类关联,这里是List集合,用到BeanListHandler
            return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Account findAccountById(Integer accountId) {
        try {
            //Account.class是查询的实体类关联,这里不是List集合,用BeanHandler
            return runner.query("select * from account where id=?",
                    new BeanHandler<Account>(Account.class),accountId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void saveAccount(Account account) {
        try {
            //account表中id是自增长,故不需要写
            runner.update("insert into account(name,money)values(?,?)",
                    account.getName(),account.getMoney());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteAccount(Integer accountId) {
        try {
            //问号占位符填的是传入参数值accountId
            runner.update("delete from account where id=?",accountId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateAccount(Account account) {
        try {
            /*每个问号代表一个占位符(1,2,3),后面的account.getName(),
            account.getMoney(),account.getId(),按顺序排列在前面的问号里*/
            runner.update("update account set name=?,money=? where id=?",
                    account.getName(),account.getMoney(),account.getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

(2) service layer
IAccountService service layer interface

public interface IAccountService {
    //1. 查询所有
    List<Account> findAllAccount();
    //2.查询一个
    Account findAccountById(Integer accountId);
    //3.保存
    void saveAccount(Account account);
    //4.删除操作
    void deleteAccount(Integer accountId);
    //5.更新操作
    void updateAccount(Account account);
}

AccountServiceImpl service layer calls persistence layer

public class AccountServiceImpl implements IAccountService {
    private IAccountDao accountDao;

    public IAccountDao getAccountDao() {
        return accountDao;
    }

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

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

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

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

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

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

domain subject category

  • Omitting the set and get, toString method
  • If the operation of the database, it is best to set the property name (table) query tables consistent field
package com.jh.domain;
import java.io.Serializable;
public class Account implements Serializable {
    private Integer id;
    private String name;
    private Float money;
}

Test category

public class AccountServiceTest {
    @Autowired
    private ApplicationContext ac =null;
    private IAccountService as = null;
    @Before  //省略这些重复步骤
    public void init(){
        //1.使用注解获取容器
        ac=new ClassPathXmlApplicationContext("bean.xml");
        //2.得到业务层对象(bean对象),name的id要与bean.xml中id一样accountService
        as = ac.getBean("accountService",IAccountService.class);
    }
    @Test
    public void testFindAll(){
        //3.执行方法
        List<Account> accounts=as.findAllAccount();
        for (Account account:accounts){
            System.out.println(account);
            /*Account{id=1, name='aaa', money=1000.0}
            Account{id=2, name='bbb', money=1000.0}
            Account{id=3, name='ccc', money=1000.0}*/
        }
    }
    @Test
    public void testFindOne(){
        //3.执行方法
        System.out.println(as.findAccountById(2));
        /*Account{id=2, name='bbb', money=1000.0}*/
    }
    @Test
    public void testSave(){
        Account account=new Account();
        account.setName("郭会技");
        account.setMoney(12782f);
        //3.执行方法
        as.saveAccount(account);
        /*Account{id=5, name='郭会技', money=12782.0}*/
    }
    @Test
    public void testUpdate(){
        //3.执行方法
        Account account=as.findAccountById(5);
        account.setMoney(12213f);
        account.setName("jh");
        as.updateAccount(account);
    }
    @Test
    public void testDelete(){
        //3.执行方法
        as.deleteAccount(4);
    }
}
Published 101 original articles · won praise 15 · views 6768

Guess you like

Origin blog.csdn.net/JH39456194/article/details/104397738