实体类:
package com.itheima.domain; import java.io.Serializable; /** * ClassName:Account * Package:com.itheima.domain * Description: * * @Date:2019/10/31 19:44 * @Author:liangshuai@shuaige.com */ //账户的实体类 /* *为什么实体类要实现serializable接口 序列化 *最重要的两个原因是: 1、将对象的状态保存在存储媒体中以便可以在以后重新创建出完全相同的副本; 2、按值将对象从一个应用程序域发送至另一个应用程序域。 实现serializable接口的作用是就是可以把对象存到字节流,然后可以恢复。所以你想如果你的对象没实现序列化怎么才能进行网络传输呢,要网络传输就得转为字节流,所以在分布式应用中,你就得实现序列化,如果你不需要分布式应用,那就没那个必要实现序列化。 * * */ public class Account implements Serializable { 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; } @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; } }
持久层:
package com.itheima.dao; import com.itheima.domain.Account; import java.util.List; /** * ClassName:IAccountDao * Package:com.itheima.dao * Description: * * @Date:2019/10/31 20:07 * @Author:liangshuai@shuaige.com */ //账户的持久层接口 public interface IAccountDao { //查询所有 List<Account> findAllAccount(); //查询一个 Account findAccountById(Integer accountId); //保存 void saveAccount(Account account); //更新 void updateAccount(Account account); //删除 void deleteAccount(Integer accountId); }package com.itheima.dao.impl; import com.itheima.dao.IAccountDao; import com.itheima.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; /** * ClassName:AccountDaoImpl * Package:com.itheima.dao.impl * Description: * * @Date:2019/10/31 20:15 * @Author:liangshuai@shuaige.com */ //账户的持久层实现类 public class AccountDaoImpl implements IAccountDao { private QueryRunner runner; public void setRunner(QueryRunner runner) { this.runner = runner; } public List<Account> findAllAccount() { try { return runner.query("select *from account",new BeanListHandler<Account>(Account.class)); }catch (Exception e){ throw new RuntimeException(e); } } 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); } } 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); } } 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); } } public void deleteAccount(Integer accountId) { try { runner.update("delete from account where id=?",accountId); }catch (Exception e){ throw new RuntimeException(e); } } }
业务层:
package com.itheima.service; import com.itheima.domain.Account; import java.util.List; /** * ClassName:IAccountService * Package:com.itheima.service * Description: * * @Date:2019/10/31 19:43 * @Author:liangshuai@shuaige.com */ //账户的业务层接口 public interface IAccountService { //查询所有 List<Account> findAllAccount(); //查询一个 Account findAccountById(Integer accountId); //保存 void saveAccount(Account account); //更新 void updateAccount(Account account); //删除 void deleteAccount(Integer accountId); } package com.itheima.service.impl; import com.itheima.dao.IAccountDao; import com.itheima.domain.Account; import com.itheima.service.IAccountService; import java.util.List; /** * ClassName:AccountServiceImpl * Package:com.itheima.service.impl * Description: * * @Date:2019/10/31 20:05 * @Author:liangshuai@shuaige.com */ //账户的业务层实现类 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 accountId) { return accountDao.findAccountById(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); } }
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.cj.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybase?serverTimezone=UTC"></property> <property name="user" value="root"></property> <property name="password" value="ls12345"></property> </bean> </beans>
测试类:
package com.itheima.test; import com.itheima.domain.Account; import com.itheima.service.IAccountService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; /** * ClassName:AccountServiceTest * Package:com.itheima.test * Description: * * @Date:2019/11/1 16:11 * @Author:liangshuai@shuaige.com */ //使用Junit单元测试,测试我们的配置 public class AccountServiceTest { @Test public void testFindAll() { //1.获取容器 ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml"); //2.得到业务层对象 IAccountService as= (IAccountService) ac.getBean("accountService"); //3.执行方法 List<Account> accounts=as.findAllAccount(); for (Account account:accounts){ System.out.println(account); } } @Test public void testFindOne() { //1.获取容器 ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml"); //2.得到业务层对象 IAccountService as= (IAccountService) ac.getBean("accountService"); //3.执行方法 Account account=as.findAccountById(1); System.out.println(account); } @Test public void testSave() { Account account=new Account(); account.setName("test"); account.setMoney(12345f); //1.获取容器 ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml"); //2.得到业务层对象 IAccountService as= (IAccountService) ac.getBean("accountService"); //3.执行方法 as.saveAccount(account); } @Test public void testUpdate() { //1.获取容器 ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml"); //2.得到业务层对象 IAccountService as= (IAccountService) ac.getBean("accountService"); //3.执行方法 Account account=as.findAccountById(22); account.setMoney(23456f); as.updateAccount(account); } @Test public void testDelete() { //1.获取容器 ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml"); //2.得到业务层对象 IAccountService as= (IAccountService) ac.getBean("accountService"); //3.执行方法 as.deleteAccount(24); } }如果使用注解的方式只需改这几个:
package com.itheima.service.impl; import com.itheima.dao.IAccountDao; import com.itheima.domain.Account; import com.itheima.service.IAccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * ClassName:AccountServiceImpl * Package:com.itheima.service.impl * Description: * * @Date:2019/10/31 20:05 * @Author:liangshuai@shuaige.com */ //账户的业务层实现类 @Service("accountService") public class AccountServiceImpl implements IAccountService { @Autowired private IAccountDao accountDao; public List<Account> findAllAccount() { return accountDao.findAllAccount(); } public Account findAccountById(Integer accountId) { return accountDao.findAccountById(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); } }package com.itheima.dao.impl; import com.itheima.dao.IAccountDao; import com.itheima.domain.Account; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import java.util.List; /** * ClassName:AccountDaoImpl * Package:com.itheima.dao.impl * Description: * * @Date:2019/10/31 20:15 * @Author:liangshuai@shuaige.com */ //账户的持久层实现类 @Repository("accountDao") public class AccountDaoImpl implements IAccountDao { @Autowired private QueryRunner runner; public List<Account> findAllAccount() { try { return runner.query("select *from account",new BeanListHandler<Account>(Account.class)); }catch (Exception e){ throw new RuntimeException(e); } } 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); } } 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); } } 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); } } public void deleteAccount(Integer accountId) { try { runner.update("delete from account where id=?",accountId); }catch (Exception e){ throw new RuntimeException(e); } } }
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中, 而是一个名称为context名称空间和约束中--> <context:component-scan base-package="com.itheima"></context:component-scan> <!--配置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.cj.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybase?serverTimezone=UTC"></property> <property name="user" value="root"></property> <property name="password" value="ls12345"></property> </bean> </beans>