在pom.xml 中引入jar包
我这里是使用c3p0连接的数据池,不知道为啥我用druid 连接数据池时报错了
<?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>cn.kitey</groupId>
<artifactId>demo02_eesy_account_XMl_IOC</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.3.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.13</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
这里的数据库数据是上次创建的eesy数据库中account数据
对应的实体类创建
package cn.kitey.domain;
/**
* 账户的实体类
*
*/
public class Account {
private Integer id;
private Integer uid;
private double money;
public Account() {
}
public Account(Integer id, Integer uid, double money) {
this.id = id;
this.uid = uid;
this.money = 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 +
'}';
}
}
在dao包下创建持久层接口accountDao
package cn.kitey.dao;
import cn.kitey.domain.Account;
import java.util.List;
public interface AccountDao {
/**
* 查询所用
* @return
*/
List<Account> findAll();
/**
* 按照id进行查询
* @return
*/
Account findById(Integer accountId);
/**
* 数据的保存
*/
void saveAccount(Account account);
/**
* 数据的更新操作
* @param account
*/
void updateAccount(Account account);
/**
* 数据的删除
* @param accountId
*/
void deleteAccount(Integer accountId);
}
在dao包下创建impl包,再该包下创建accountDaoImpl类,继承accountDao接口
注意:
1.我们这里使用QueryRunner创建了queryRunner对象,因为我们要在IOC中 对queryRunner注入数据,所以生成了set方法。
2.再书写sql语句时,可以现在mysql中进行检测
package cn.kitey.dao.impl;
import cn.kitey.dao.AccountDao;
import cn.kitey.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
/**
* 持久层实现类
*/
public class AccountDaoImpl implements AccountDao {
QueryRunner queryRunner;
/**
* 从spring中配置
* @param queryRunner
*/
public void setQueryRunner(QueryRunner queryRunner) {
this.queryRunner = queryRunner;
}
public List<Account> findAll() {
try {
return queryRunner.query("select * from account", new BeanListHandler<Account>(Account.class));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public Account findById(Integer accountId) {
try {
return queryRunner.query("select * from account where id = ?", new BeanHandler<Account>(Account.class),accountId);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void saveAccount(Account account) {
try {
queryRunner.update("insert into account(id,uid, money) values(?,?,?)", account.getId(),account.getUid(),account.getMoney());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void updateAccount(Account account) {
try {
queryRunner.update("update account set uid = ?, money = ? where id = ?", account.getUid(),account.getMoney(),account.getId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void deleteAccount(Integer accountId) {
try {
queryRunner.update("delete from account where id = ?",accountId);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
创建service包,在该包下创建AccountService接口
package cn.kitey.service;
import cn.kitey.domain.Account;
import java.util.List;
/**
* 账户的业务层接口
*
*/
public interface AccountService {
/**
* 查询所用
* @return
*/
List<Account> findAll();
/**
* 按照id进行查询
* @return
*/
Account findById(Integer accountId);
/**
* 数据的保存
*/
void saveAccount(Account account);
/**
* 数据的更新操作
* @param account
*/
void updateAccount(Account account);
/**
* 数据的删除
* @param accountId
*/
void deleteAccount(Integer accountId);
}
在service包下创建impl包,在该包下创建实现类AccountServiceImpl类
注意:
1.一定要创建accountDao的set方法,因为要在bean.xml中注入数据
package cn.kitey.service.impl;
import cn.kitey.dao.AccountDao;
import cn.kitey.dao.impl.AccountDaoImpl;
import cn.kitey.domain.Account;
import cn.kitey.service.AccountService;
import java.util.List;
/**
* 账户的业务实现层
*/
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
/**
* 通过IOC容器进行配置
* @param accountDao
*/
public void setAccountDao(AccountDaoImpl accountDao) {
this.accountDao=accountDao;
}
public List<Account> findAll() {
return accountDao.findAll();
}
public Account findById(Integer accountId) {
return accountDao.findById(accountId);
}
public void saveAccount(Account account) {
accountDao.saveAccount(account );
}
public void updateAccount(Account account) {
accountDao.updateAccount(account);
}
public void deleteAccount(Integer accountId) {
accountDao.deleteAccount(accountId);
}
}
在resources下创建bean.xml配置文件,进行数据的注入
书写思路:
1.首先我们想调用service方法,就要配置service
2.当配置service时需要注入accountDao,这时,就需要配置accountDao
3.配置accountDao时发现要注入queryRunner数据,这时有需要配置queryRunner
4.配置queryRunner时,要注入数据连接的数据源数据,这时只能采用构造方法的形式进行数据注入
5.配置数据连接的数据源
当以上配置完后我们才能配置好accountService
<?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="cn.kitey.service.impl.AccountServiceImpl">
<!--注入dao-->
<property name="accountDao" ref="accountDao"></property>
</bean>
<!--配置Dao对象-->
<bean id="accountDao" class="cn.kitey.dao.impl.AccountDaoImpl">
<!-- 注入QueryRunner -->
<property name="queryRunner" 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.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy?serverTimezone=GMT%2B8"></property>
<property name="user" value="root"></property>
<property name="password" value="25002500"></property>
</bean>
</beans>
在test中创建单元测试方法:
package cn.kitey.test;
import cn.kitey.domain.Account;
import cn.kitey.service.AccountService;
import cn.kitey.service.impl.AccountServiceImpl;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
import static org.junit.Assert.*;
public class AccountServiceTest {
private AccountService xmlBean;
@Before
public void init(){
ApplicationContext xml = new ClassPathXmlApplicationContext("bean.xml");
xmlBean = xml.getBean("accountService", AccountServiceImpl.class);
}
@Test
public void findAll() {
List<Account> all = xmlBean.findAll();
System.out.println("findAll()数据结果为:");
for (Account account : all) {
System.out.println(account);
}
}
@Test
public void findById() {
Account byId = xmlBean.findById(1);
System.out.println(byId);
}
@Test
public void saveAccount() {
Account acc = new Account();
acc.setId(6);
acc.setUid(1);
acc.setMoney(123456);
xmlBean.saveAccount(acc);
List<Account> all = xmlBean.findAll();
for (Account account : all) {
System.out.println("进行数据插入后的结果:");
System.out.println(account);
}
}
@Test
public void updateAccount() {
Account acc = new Account(6, 1, 8888);
xmlBean.updateAccount(acc);
List<Account> all = xmlBean.findAll();
for (Account account : all) {
System.out.println("进行数据更新后的结果后的结果:");
System.out.println(account);
}
}
@Test
public void deleteAccount() {
xmlBean.deleteAccount(6);
List<Account> all = xmlBean.findAll();
for (Account account : all) {
System.out.println("进行数据删除后的结果:");
System.out.println(account);
}
}
}
1.findAll()方法测试结果:
2.findById()方法测试结果:
3.saveAccount()测试结果:
3.updateAccount()测试结果:
以上的类容就是使用ioc容器实现crud的操作。
202/6/14/15:32