九点钟☆方向

记录成长点滴

欢迎来到我的个人博客~


Spring笔记(四) 使用spring的IOC实现账户的CRUD

目录

使用 spring 的 IOC 实现对象的管理 使用 dbutils作为持久层解决方案 使用 c3p0 数据源

XML方式

引入依赖

Spring,dbutils,Mysql,C3P0,Junit

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>commons-dbutils</groupId>
        <artifactId>commons-dbutils</artifactId>
        <version>1.7</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.13</version>
    </dependency>
</dependencies>

建表

create table account (
	id int primary key auto_increment,
	name varchar (40),
	money float
	)character set utf8 collate utf8_general_ci;


	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.java

public class Account implements Serializable {
    private Integer id;
    private String name;
    private Float money;
    //生成set/get
}

Service接口及实现类

//service接口
public interface IAccountService {
    List<Account> findAllAccount();
}

//实现类
public class AccountServiceImpl implements IAccountService {

    private IAccountDao accountDao;
    //生成set/get
    
    public List<Account> findAllAccount() {
        return accountDao.findAllAccount();
    }
}

Dao接口及实现类

//DAO接口
public interface IAccountDao {
    List<Account> findAllAccount();
}

//实现类
public class AccountDaoImpl implements IAccountDao {

    private QueryRunner queryRunner;
	//生成set/get

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

Spring 配置文件

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">
    
    <bean id="accountService" class="com.mg.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>

    <bean id="accountDao" class="com.mg.dao.impl.AccountDaoImpl">
        <property name="queryRunner" ref="queryRunner"></property>
    </bean>

    <!--单例时,多个DAO同时使用会产生线程安全问题,所以改为多例-->
    <bean id="queryRunner" 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.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
</beans>

测试使用

public class MyTest {
    @Test
    public void test1() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService accountService = ac.getBean("accountService", IAccountService.class);
        List<Account> accounts = accountService.findAllAccount();
        for(Account account:accounts) {
            System.out.println(account);
        }
    }
}

注解方式

注:此处的注解方式并不彻底,还会存在Spring的配置文件。彻底的解决方式会在下篇讲解其他注解中进行实现。

引入依赖

同xml方式

建表

同xml方式

实体类

同xml方式

Service接口及实现类

//service接口,同xml方式
public interface IAccountService {
    List<Account> findAllAccount();
}

//实现类,加入相应注解
@Service("accountService")
public class AccountServiceImpl implements IAccountService {

    @Autowired
    private IAccountDao accountDao;

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

DAO接口及实现类

//DAO接口,同xml方式
public interface IAccountDao {
    List<Account> findAllAccount();
}


//实现类,加入相应注解
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {

    @Autowired
    private QueryRunner queryRunner;
    
    public List<Account> findAllAccount() {
        try {
            return queryRunner.query("select * from account",
                                     new BeanListHandler<Account>(Account.class)
                                    );
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

Spring配置文件

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--导入约束时需要多导入一个 context 名称空间下的约束-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">
    <!--需要扫描的包-->
    <context:component-scan base-package="com.mg"></context:component-scan>

    <bean id="queryRunner" 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.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
</beans>

测试使用

同xml方式

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦