Analysis of ideas for building a spring development environment based on an xml-based ioc case

XML-based ioc case

structure diagram
insert image description here

1. Pom.xml guide coordinates

<packaging>jar</packaging>
    <dependencies>
        <!--spring的坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <!--dbutils的坐标-->
        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.7</version>
        </dependency>
        <!--mysql驱动坐标-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</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.12</version>
        </dependency>
    </dependencies>

Second, the persistence layer implementation class

Add, delete, modify and check the code here

public class AccountDaoImpl implements IAccountDao {
    
    
    //QueryRunner是dbutils中的一个方法,封装了jdbc的代码,在dao层使用简单框架
    private QueryRunner runner;

    public QueryRunner getRunner() {
    
    
        return runner;
    }

    public void setRunner(QueryRunner runner) {
    
    
        this.runner = runner;
    }

    public List<Account> findAllAccount() {
    
    
        try {
    
    
            return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
        } catch (Exception e) {
    
    
            throw new RuntimeException();
        }
    }
}

3. Add spring to build a spring development environment

focus

bean.xml import constraints to configure the environment
prerequisites

<?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">

Layer-by-layer configuration thinking analysis

    <!-- 业务层对象 配置Service-->
    <bean id="accountService" class="bruce.service.impl.AccountServiceImpl">
        <!-- 注入dao对象 -->
        <property name="accountDao" ref="accountDao"></property>
    </bean>

Configure without accountDao, and assign ref to it

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

Configure without QueryRunner (need to use constructor injection), and assign ref to the above

    <!-- 配置QueryRunner
    默认是单例对象 多个dao在使用同一个对象 可能用完它的时候一个在用另一个还没用完 导致线程互相干扰
    多例的话会保证每次使用这个对象都是创建一个新的-->
    <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
        <!-- 注入数据源 QueryRunner是没有set方法和注入 需要使用构造函数注入(前面的都是set方法注入 -->
        <constructor-arg name="ds" ref="dataSource"></constructor-arg>
    </bean>

Configure without dataSource, and assign ref to the above

    <!-- 配置数据源 (导c3p0的包-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 连接数据库的必备信息 -->
        <!--mysql的驱动-->
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
        <!--连接字符串-->
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/springxmlioc?serverTimezone=Asia/Shanghai"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
</beans>

The test method is the same as before

Guess you like

Origin blog.csdn.net/weixin_42727032/article/details/104617636