1. 创建项目导入jar包
下面使用spring基于xml的IoC配置,模拟账户的CRUD操作
- 创建maven项目,导入pom依赖
<?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.wink</groupId>
<artifactId>spring01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<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>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.12</version>
</dependency>
</dependencies>
</project>
2. 创建数据库并编写实体类
/**************SQL***************/
creat database springIoC;
use springIoC;
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
public class Account implements Serializable {
private Integer id;
private String name;
private Double money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
3. 编写持久层
- 编写账户的持久层接口和持久层接口的实现类
/*****************IAccountDao.java**************/
public interface IAccountDao {
//根据Id查询
Account findById(Integer accountId);
//查询所有
List<Account> findAll();
//增加操作
void save(Account account);
//删除操作
void delete(Integer accountId);
//更新操作
void update(Account account);
}
/***************AccountDaoImpl.java**************/
public class AccountDaoImpl implements IAccountDao {
private QueryRunner runner;
public void setRunner(QueryRunner runner) {
this.runner = runner;
}
//根据Id查询
public Account findById(Integer accountId) {
try {
return runner.query("select * from account where id= ?",
new BeanHandler<Account>(Account.class),accountId);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
//查询所有
public List<Account> findAll() {
try {
return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
//增加操作
public void save(Account account) {
try {
runner.update("insert into account(name,money)values(?,?)",
account.getName(),account.getMoney());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
//删除操作
public void delete(Integer accountId) {
try {
runner.update("delete from account where id = ?",accountId);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
//更新操作
public void update(Account account) {
try {
runner.update("update account set name = ?,money= ? where id= ?",account.getName(),account.getMoney(),account.getId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
4. 编写业务层
- 编写账户的业务层接口和业务层接口的实现类
/**************IAccountService.java**************/
public interface IAccountService {
Account findById(Integer accountId);
List<Account> findAll();
void save(Account account);
void delete(Integer accountId);
void update(Account account);
}
/*************AccountServieImpl.java*************/
public class AccountServieImpl implements IAccountService {
private IAccountDao accountDao;
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
public Account findById(Integer accountId) {
return accountDao.findById(accountId);
}
public List<Account> findAll() {
return accountDao.findAll();
}
public void save(Account account) {
accountDao.save(account);
}
public void delete(Integer accountId) {
accountDao.delete(accountId);
}
public void update(Account account) {
accountDao.update(account);
}
}
5. 编写配置文件
- 编写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">
<!--配置Service-->
<bean id="accountService" class="com.wink.service.impl.AccountServieImpl">
<!-- 注入dao -->
<property name="accountDao" ref="accountDao"></property>
</bean>
<!--配置Dao对象-->
<bean id="accountDao" class="com.wink.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.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///springIoC"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
6. 进行测试
- 上述步骤完成了账户的基本配置工作,下面编写测试类使用Junit进行数据的增删改操作
public class AccountTest {
@Test //查询账户id为1的账户信息
public void testFindById(){
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService as = ac.getBean("accountService",IAccountService.class);
Account account = as.findById(1);
System.out.println(account);
}
}
@Test //查询所有账户信息
public void testFindAll(){
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService as = ac.getBean("accountService",IAccountService.class);
List<Account> accounts = as.findAll();
for (Account account : accounts) {
System.out.println(account);
}
}
@Test //修改账户1的信息
public void testUpdate(){
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService as = ac.getBean("accountService",IAccountService.class);
Account account = as.findById(1);
account.setMoney(666.6);
as.update(account);
System.out.println(as.findById(1));
}
@Test //添加一个账户信息
public void testsave(){
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService as = ac.getBean("accountService",IAccountService.class);
Account account = new Account();
account.setName("叶子");
account.setMoney(100.0);
as.save(account);
}
@Test //根据id删除账户
public void testdelete(){
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService as = ac.getBean("accountService",IAccountService.class);
as.delete(4);
}