Spring的IOC的实现账户的CRUD
1.1 需求和技术要求
1.1.1 需求
实现账户的 CRUD 操作
1.1.2 技术要求
1.2 开发阶段
1.2.1导入的 jar包
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5cs2UOO2-1571315904079)(file:///C:\Users\lhx\AppData\Local\Temp\msohtmlclip1\01\clip_image002.jpg)]
1.2. 2Pom.xml文件中的坐标如下:
`<**dependencies**> <**dependency**> <**groupId**>org.springframework</**groupId**> <**artifactId**>spring-context</**artifactId**> <**version**>5.1.7.RELEASE</**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.1</**version**> </**dependency**> <**dependency**> <**groupId**>log4j</**groupId**> <**artifactId**>log4j</**artifactId**> <**version**>1.2.17</**version**> </**dependency**> <**dependency**> <**groupId**>commons-logging</**groupId**> <**artifactId**>commons-logging</**artifactId**> <**version**>1.2</**version**> </**dependency**> <**dependency**> <**groupId**>commons-dbutils</**groupId**> <**artifactId**>commons-dbutils</**artifactId**> <**version**>1.4</**version**> </**dependency**> <**dependency**> <**groupId**>dbassit</**groupId**> <**artifactId**>dbassit</**artifactId**> </**dependency**> </**dependencies**>`
1.2.3数据表结构和数据
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);
```
```
1.2.4 编写实体Bean
`*/** \* **@author*** *友信科创**.**刘峰* ** **@company** http://www.ityouxin.com \*/* **public class** Account { **private** Integer **id**; **private** String **name**; **private** Float **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** Float getMoney() { **return** **money**; } **public void** setMoney(Float money) { **this**.**money** = money; } }`
1.2.5编写Dao
*public interface** AccountDao {
**void** saveAccount(Account account);
**void** deleteAccount(Integer accountId);
**void** updateAccount(Account account);
Account findById(Integer accountId);
List<Account> findAll();
}
public class AccountDaoImpl implements AccountDao { `
` private QueryRunner runner;`` ``
public void setRunner(QueryRunner runner) {``
this.runner = runner;``
}`` ``
@Override``
public List<Account> findAllAccount() {``
try{``
return runner.query("select * from account",newBeanListHandler<Account(Account.class));``
}catch (Exception e) {``
throw new RuntimeException(e);``
}``
}`` ``
@Override``
public Account findAccountById(Integer accountId) {``
try{``
return runner.query("select * from account where id = ? ",new BeanHandler<Account>(Account.class),accountId);``
}catch (Exception e) {``
throw new RuntimeException(e);``
}``
}`` ``
@Override``
public void saveAccount(Account account) {``
try{``
runner.update("insert into account(name,money)values(?,?)",account.getName(),account.getMoney());``
}catch (Exception e) {``
throw new RuntimeException(e);``
}``
}``
@Override``
public void updateAccount(Account account) {``
try{``
runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());``
}catch (Exception e) {``
throw new RuntimeException(e);``
}``
}`` ``
@Override``
public void deleteAccount(Integer accountId) {``
try{``
runner.update("delete from account where id=?",accountId);``
}catch (Exception e) {``
throw new RuntimeException(e);``
}``
}
}`
1.2.6编写Service
`*/** \* **@author*** *友信科创**.**刘峰* ** **@company** http://www.ityouxin.com \*/* **public interface** AccountService { **void** saveAccount(Account account); **void** deleteAccount(Account account); **void** updateAccount(Account account); Account findById(Integer accountId); List<Account> findAll(); }` `*/** \* **@author*** *友信科创**.**刘峰* ** **@company** http://www.ityouxin.com \*/* **public class** AccountServiceImpl **implements** AccountService { **private** AccountDao **accountDao**; **public** AccountDao getAccountDao() { **return** **accountDao**; } **public void** setAccountDao(AccountDao accountDao) { **this**.**accountDao** = accountDao; } **public void** saveAccount(Account account) { **accountDao**.saveAccount(account); } **public void** deleteAccount(Account account) { **accountDao**.deleteAccount(account); } **public void** updateAccount(Account account) { **accountDao**.updateAccount(account); } **public** Account findById(Integer accountId) { **return** **accountDao**.findById(accountId); } **public** List<Account> findAll() { **return** **accountDao**.findAll(); } }`
1.2.7编写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.itheima.service.impl.AccountServiceImpl">
<!-- 注入dao -->
<property name="accountDao" ref="accountDao"></property> </bean> <!--配置Dao对象--> <bean id="accountDao" class="com.itheima.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://localhost:3306/eesy"></property>
<property name="user" value="root"></property>
<property name="password" value="1234"></property>
</bean>
</beans>