Spring开发案例1半注解开发
2024-10-07 18:43:19
dao层:
package cn.mepu.dao.imp; import cn.mepu.dao.AccountDao; import cn.mepu.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; /** * @author shkstart * @create 2019-11-08 10:28 */ @Repository("accountDao") public class AccountDaoImp implements AccountDao { @Autowired private QueryRunner runner; @Override public List<Account> findAllAccount() { try { return runner.query("select * from account",new BeanListHandler<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 acc) { try { runner.update("insert into account(name,money) values(?,?)" , acc.getName(),acc.getMoney()); } catch (Exception e) { throw new RuntimeException(e); } } @Override public void updateAccount(Account acc) { try { runner.update("update account set name=? , money=? where id = ? " , acc.getName(),acc.getMoney(),acc.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); } } }
service层:
package cn.mepu.service.imp; import cn.mepu.dao.AccountDao; import cn.mepu.domain.Account; import cn.mepu.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @author shkstart * @create 2019-11-08 10:12 */ @Service("accountService") public class AccountServiceImp implements AccountService { @Autowired private AccountDao accountDao; @Override public List<Account> findAllAccount() { return accountDao.findAllAccount(); } @Override public Account findAccountById(Integer accountId) { return accountDao.findAccountById(accountId); } @Override public void saveAccount(Account acc) { accountDao.saveAccount(acc); } @Override public void updateAccount(Account acc) { accountDao.updateAccount(acc); } @Override public void deleteAccount(Integer accountId) { accountDao.deleteAccount(accountId); } }
servlet层:
package cn.mepu.service; import cn.mepu.domain.Account; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; /** * @author shkstart * @create 2019-11-08 10:45 */ public class AccountServiceTest { @Test public void testFindAll(){ //1.获取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //2.得到业务层对象 AccountService service = (AccountService) ac.getBean("accountService"); //3.执行方法 List<Account> accounts = service.findAllAccount(); for (Account account : accounts) { System.out.println("account = " + account); } } @Test public void testFindOne(){ //1.获取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //2.得到业务层对象 AccountService service = (AccountService) ac.getBean("accountService"); //3.执行方法 Account account = service.findAccountById(1); System.out.println(account); } @Test public void testSave(){ //1.获取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //2.得到业务层对象 AccountService service = (AccountService) ac.getBean("accountService"); //3.执行方法 service.saveAccount(new Account(1,"DDD",1234)); } @Test public void testUpdate(){ //1.获取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //2.得到业务层对象 AccountService service = (AccountService) ac.getBean("accountService"); //3.执行方法 service.updateAccount(new Account(1,"DDD",2345)); } @Test public void testDelete(){ //1.获取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //2.得到业务层对象 AccountService service = (AccountService) ac.getBean("accountService"); //3.执行方法 service.deleteAccount(4); } }
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" 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="cn.mepu"></context:component-scan> <!-- 配置注入QueryRunner scope保证线程安全--> <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/javaee"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean> </beans>
最新文章
- EasyUI使用JSON保存数据
- 使用Wireshark 查看查找未被过滤端口
- Android java传递int类型数据给C
- javascript原型Prototype【转】
- CSS 有关margin padding
- jquery 常用基础方法
- 练习一_使用Git进行代码管理的心得
- ural 1247. Check a Sequence
- 你不知道的JavaScript-- 事件流与事件处理
- WinDBG使用之线程
- poj 1300 Door Man 欧拉回路
- Hbase学习记录(1)|伪分布式安装
- Spring MVC与easyui国际化
- Web APP 随笔
- URAL 1009 K-based numbers(DP递推)
- VS2010中水晶报表应用及实例
- jquery于form正在使用submit问题,未解决
- 面向对象的SOLID原则白话篇
- Integration Services 服务连接失败,拒绝访问以及无法检索数据报错问题
- 理解Spring中的IOC和AOP