本节内容
本节将结合数据库的操作使用讲解IOC的实际案例使用。
数据库准备
首先要在本机安装MYSQl,如果装的是8.0+的mysql版本,在写数据库的时候要注意了,可能需要花点时间适配一下。
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.sankuai</groupId> <artifactId>lujuan</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <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.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</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> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.2.RELEASE</version> </dependency> </dependencies> </project>
在这个配置中,我们为了进行数据库操作要导入一个mysql驱动,一个commons-dbutils用来创建数据库的,c3p0用来操作数据库链接池的(虽然不是很明白为什么链接池不是在commons-dbutils中一起封装的)。
数据库配置
insert into account(name,money)values('aaa',1000);
insert into account(name,money)values('bbb',1000);
insert into account(name,money)values('ccc',1000);
然后我们就有链接数据库的内容了。
代码展示-测试文件
package com.lujuan; import com.lujuan.domain.Account; import com.lujuan.service.impl.AccountServiceImpl; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:bean.xml") public class AccountServiceTest { @Autowired private AccountServiceImpl ac; @Test public void testFindAll(){ List<Account> accounts = ac.findAllAccount(); for(Account account:accounts) System.out.println(account.toString()); } @Test public void testFindAccountById() { Account account = ac.findAccountById(1); System.out.println(account); } @Test public void testAddAccount() { Account account = new Account(); account.setName("ddd"); account.setMoney(2000f); ac.addAccount(account); } @Test public void testDeleteAccountById() { ac.deleteAccountById(1); } @Test public void testUpdateAccount() { Account account = ac.findAccountById(4); account.setName("eeee"); account.setMoney(3333f); ac.updateAccount(account); } }
代码展示-bean配置文件
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在bean约束中, 而是在一个名称为context的名称空间和约束中--> <context:component-scan base-package="com.lujuan"/> <bean id = "accountService" class="com.lujuan.service.impl.AccountServiceImpl"> <property name="accountDao" ref ="accountDao"> </property> </bean> <bean id = "accountDao" class="com.lujuan.dao.imp.AccountDaoImpl"> <property name="queryRunner" ref = "queryRunner"> </property> </bean> <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/eesy?characterEncoding=utf8"> </property> <property name="user" value="root"> </property> <property name="password" value="Admin@123"> </property> </bean> </beans>
在这个里面根据一层一层的调用关系进行了相关的bean文件配置,但是其实我还是不是很理解有关数据库的一些配置规则。
Dao代码文件
package com.lujuan.dao.imp; import com.lujuan.dao.IAccountDao; import com.lujuan.domain.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; /** * @项目: spring-learn-demo * @描述: * @作者: lujuan03 * @创建日期: 2020-03-28 **/ public class AccountDaoImpl implements IAccountDao { private QueryRunner queryRunner;//这是commons.dbutils提供的 public void setQueryRunner(QueryRunner queryRunner) { this.queryRunner = queryRunner; } public List<Account> findAllAccount() { List<Account> reV = null; try{ //查询之后顺便映射为实体类 reV = queryRunner.query("select * from account", new BeanListHandler<Account>(Account.class)); }catch (Exception e){ throw new RuntimeException(e); } return reV; } public Account findAccountById(Integer id) { Account reV = null; try{ reV = queryRunner.query("select * from account where id = ?", new BeanHandler<Account>(Account.class), id); }catch (Exception e){ throw new RuntimeException(e); } return reV; } public void addAccount(Account account) { try{ queryRunner.update("insert into account(name, money) values (?, ?)", account.getName(), account.getMoney()); }catch (Exception e){ throw new RuntimeException(e); } } public void deleteAccountById(Integer id) { try{ queryRunner.update("delete from account where id = ?", id); }catch (Exception e){ throw new RuntimeException(e); } } public void updateAccount(Account account) { try{ queryRunner.update("update account set name= ?, money = ? where id = ?", account.getName(), account.getMoney(), account.getId()); }catch (Exception e){ throw new RuntimeException(e); } } }
Dao的接口文件
package com.lujuan.dao; import com.lujuan.domain.Account; import java.util.List; public interface IAccountDao { List<Account> findAllAccount(); Account findAccountById(Integer id); void addAccount(Account account); void deleteAccountById(Integer id); void updateAccount(Account account); }
servie接口文件
package com.lujuan.service; import com.lujuan.domain.Account; import java.util.List; public interface IAccountService { List<Account> findAllAccount(); Account findAccountById(Integer id); void addAccount(Account account); void deleteAccountById(Integer id); void updateAccount(Account account); }
service实现文件
package com.lujuan.service.impl; import com.lujuan.dao.IAccountDao; import com.lujuan.domain.Account; import com.lujuan.service.IAccountService; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import java.util.*; /** * @项目: spring-learn-demo * @描述: * @作者: lujuan03 * @创建日期: 2020-03-28 **/ //@Component(value="accountService") //@Controller public class AccountServiceImpl implements IAccountService { private IAccountDao accountDao; public void setAccountDao(IAccountDao accountDao) { this.accountDao = accountDao; } public List<Account> findAllAccount() { return accountDao.findAllAccount(); } public Account findAccountById(Integer id) { return accountDao.findAccountById(id); } public void addAccount(Account account) { accountDao.addAccount(account); } public void deleteAccountById(Integer id) { accountDao.deleteAccountById(id); } public void updateAccount(Account account) { accountDao.updateAccount(account); } public void saveAccount() { System.out.println("AccountServiceImpl: saveAccount"); } @PostConstruct public void init(){ System.out.println("init......"); } @PreDestroy public void destroy(){ System.out.println("destroy......"); } }
运行结果
执行我们写的test文件就可以检查我们这个IOC实例项目了。
不明白的地方
我现在对数据库的连接池以及对连接池对使用真的是一头蒙蔽,还有什么事务管理,更是不知道是什么。
但是按照教程交给我们的那样,第一个spring的二层架构的项目完成,执行没有问题。
至于其他的内容待学完这个课程继续深究。