1、maven依赖

<?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.ly.spring</groupId>
<artifactId>spring03</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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>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.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

2、实体类

package com.ly.spring.domain;

import java.io.Serializable;

public class Account implements Serializable {
private Integer id;
private Integer uid;
private double 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 +
'}';
}
}

3、Service接口

package com.ly.spring.service;

import com.ly.spring.domain.Account;

import java.sql.SQLException;
import java.util.List; public interface IAccountService {
public List<Account> findAll() throws SQLException;
public Account findOne(Integer id) throws SQLException;
public void save(Account account) throws SQLException;
public void update(Account account) throws SQLException;
public void delete(Integer id) throws SQLException;
}

4、Service实现类

package com.ly.spring.service.impl;

import com.ly.spring.dao.IAccountDao;
import com.ly.spring.domain.Account;
import com.ly.spring.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.sql.SQLException;
import java.util.List; @Service("accountService")
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
public List<Account> findAll() throws SQLException {
return accountDao.findAll();
} @Override
public Account findOne(Integer id) throws SQLException {
return accountDao.findOne(id);
} @Override
public void save(Account account) throws SQLException {
accountDao.save(account);
} @Override
public void update(Account account) throws SQLException {
accountDao.update(account);
} @Override
public void delete(Integer id) throws SQLException {
accountDao.delete(id);
}
}

5、Dao接口

package com.ly.spring.dao;

import com.ly.spring.domain.Account;

import java.sql.SQLException;
import java.util.List; public interface IAccountDao {
public List<Account> findAll() throws SQLException;
public Account findOne(Integer id) throws SQLException;
public void save(Account account) throws SQLException;
public void update(Account account) throws SQLException;
public void delete(Integer id) throws SQLException;
}

6、Dao实现类

package com.ly.spring.dao.impl;

import com.ly.spring.dao.IAccountDao;
import com.ly.spring.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.sql.SQLException;
import java.util.List; @Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
@Autowired
private QueryRunner queryRunner;
public List<Account> findAll() throws SQLException {
return queryRunner.query("select * from account",new BeanListHandler<Account>(Account.class));
} @Override
public Account findOne(Integer id) throws SQLException {
return queryRunner.query("select * from account where id = ?",new BeanHandler<Account>(Account.class),id);
} @Override
public void save(Account account) throws SQLException {
queryRunner.update("insert into account(uid,money) values(?,?)",account.getUid(),account.getMoney());
} @Override
public void update(Account account) throws SQLException {
queryRunner.update("update account set money = ? where id = ?",account.getMoney(),account.getId());
} @Override
public void delete(Integer id) throws SQLException {
queryRunner.update("delete from account where id = ?",id);
}
}

7、测试类

package com.ly.spring.test;
import com.ly.spring.domain.Account;
import com.ly.spring.service.IAccountService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.sql.SQLException;
import java.util.List; public class MainTest {
private ApplicationContext context;
private IAccountService accountService;
@Before
public void init() {
context = new ClassPathXmlApplicationContext("bean.xml");
accountService = context.getBean("accountService", IAccountService.class);
}
@Test
public void findAll() throws SQLException {
List<Account> accounts = accountService.findAll();
System.out.println(accounts);
} @Test
public void findOne() throws SQLException {
Account account = accountService.findOne(3);
System.out.println(account);
} @Test
public void save() throws SQLException {
Account account = new Account();
account.setUid(51);
account.setMoney(8000);
accountService.save(account);
} @Test
public void update() throws SQLException {
Account account = new Account();
account.setId(4);
account.setMoney(100000);
accountService.update(account);
}
@Test
public void delete() throws SQLException {
accountService.delete(4);
}
}

8、spring配置文件

<?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">
<!--配置注解扫描包-->
<context:component-scan base-package="com.ly.spring"></context:component-scan>
<!--引入外部properties文件-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--配置QueryRunner并注入数据源-->
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
<!--配置c3p0数据源并注入相关属性-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
</beans>

9、数据库配置文件

jdbc.url = jdbc:mysql://localhost:3306/db01
jdbc.driver = com.mysql.jdbc.Driver
jdbc.user = root
jdbc.password = root

最新文章

  1. iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
  2. rem计算适配
  3. 如何高效部署前端代码,如css,js...
  4. Splay 伸展树
  5. ThinkPHP公共配置文件与各自项目中配置文件组合的方法
  6. C 基于UDP实现一个简易的聊天室
  7. [Oracle EBS R12]SQL Queries and Multi-Org Architecture in Release 12 (Doc ID 462383.1)
  8. MYSQL命令cmd操作
  9. 最近跟mysql的自动shutdown干上了。。。
  10. [已解决]import pymssql ImportError: libsybdb.so.5
  11. 使用定时器限制点击按钮发送短信(附源码)--JavaScript小案例
  12. PVLAN 简介
  13. 改变input的值不会触发change事件的解决思路
  14. EasyUI 树形菜单加载父/子节点
  15. VitrualBox、vagrant、homestead的关系
  16. WebLogic 11gR1修改jdk版本
  17. ie6兼容性处理
  18. 6.Xilinx RapidIO核仿真与包时序分析
  19. KbmMW 服务器架构简介
  20. awk多列匹配

热门文章

  1. Go语言实现:【剑指offer】合并两个排序的链表
  2. CAS 分析
  3. 【JavaScript】进制转换&amp;位运算,了解一下?
  4. 【WPF学习】第四十七章 WriteableBitmap类
  5. VFP 图形文件与剪切板互换的API解决方法
  6. 持续集成:jenkins集合
  7. zabbix-proxy配置文件参数说明
  8. Python——格式输出,基本数据
  9. ABP框架迁移到Mysql
  10. 带输入提示的搜索框ajax请求