一、项目目录
二、各个文件代码
1.DataSourceConfig类
package com.june.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
@PropertySource("classpath:jdbc.properties")//加载配置文件
public class DataSourceConfig {
//把jdbc.properties配置文件中的值引用到成员变量上
@Value("${jdbc.driverClassName}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean("dataSource") //把方法的返回值存到IOC容器中,为它的key起名dataSource
public DataSource getDataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driver);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
return druidDataSource;
}
}
2.SpringConfig.java类
package com.june.config;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import javax.sql.DataSource;
@Configuration
@ComponentScan("com.june")//注解扫描
@Import(DataSourceConfig.class)
public class SpringConfig {
@Bean("queryRunner")
public QueryRunner getQueryRunner(@Autowired DataSource dataSource){//告诉spring,根据形参类型从IOC容器中找对应的实例对象,进行注入
QueryRunner queryRunner = new QueryRunner(dataSource);
return queryRunner;
}
}
3.Account类
package com.june.domain;
public class Account {
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 +
'}';
}
}
4.AccountDao接口
package com.june.dao;
import com.june.domain.Account;
import java.util.List;
public interface AccountDao {
public List<Account> findAll();
public Account findById(Integer id);
public void save(Account account);
public void update(Account account);
public void delete(Integer id);
}
5.AccountDaoImpl类
package com.june.dao.impl;
import com.june.dao.AccountDao;
import com.june.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 //相当于配置了bean标签
public class AccountDaoImpl implements AccountDao {
@Autowired
private QueryRunner queryRunner;
@Override
public List<Account> findAll() {
List<Account> list=null;
//编写sql
String sql="select * from account";
try {
//执行sql
list = queryRunner.query(sql, new BeanListHandler<Account>(Account.class));
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public Account findById(Integer id) {
Account account=null;
//编写sql
String sql="select * from account where id=?";
//执行sql
try {
account = queryRunner.query(sql, new BeanHandler<Account>(Account.class),id);
} catch (SQLException e) {
e.printStackTrace();
}
return account;
}
@Override
public void save(Account account) {
//编写sql
String sql="insert into account values(null,?,?)";//主键自增,设为null就行
try {
queryRunner.update(sql,account.getName(),account.getMoney());
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void update(Account account) {
//编写sql
String sql="update account set name=?,money=? where id=?";
try {
queryRunner.update(sql,account.getName(),account.getMoney(),account.getId());
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void delete(Integer id) {
//编写sql
String sql="delete from account where id=?";
try {
queryRunner.update(sql,id);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
6.AccountService接口
package com.june.service;
import com.june.domain.Account;
import java.util.List;
public interface AccountService {
public List<Account> findAll();
public Account findById(Integer id);
public void save(Account account);
public void update(Account account);
public void delete(Integer id);
}
7.AccountServiceImpl类
package com.june.service.impl;
import com.june.dao.AccountDao;
import com.june.domain.Account;
import com.june.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service(value = "accountService") //相当于配置了bean标签 id属性+class属性(class就不用了,因为这里已经知道了是AccountServiceImpl了)
public class AccountServiceImpl implements AccountService {
@Autowired//根据类型去完成依赖注入
private AccountDao aDao;
@Override
public List<Account> findAll() {
return aDao.findAll();
}
@Override
public Account findById(Integer id) {
return aDao.findById(id);
}
@Override
public void save(Account account) {
aDao.save(account);
}
@Override
public void update(Account account) {
aDao.update(account);
}
@Override
public void delete(Integer id) {
aDao.delete(id);
}
}
8.AccountServiceTest类
package com.june.test;
import com.june.config.SpringConfig;
import com.june.domain.Account;
import com.june.service.AccountService;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.List;
public class AccountServiceTest {
/* //根据配置文件,生成里面配置的对象,注入IOC容器中
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取accountService实例
AccountService accountService = (AccountService) classPathXmlApplicationContext.getBean("accountService");*/
//当前改成了纯注解形式
AnnotationConfigApplicationContext annotationConfigApplicationContext=new AnnotationConfigApplicationContext(SpringConfig.class);//传入核心配置类
AccountService accountService = (AccountService)annotationConfigApplicationContext.getBean("accountService");
//测试添加
@Test
public void testSave(){
Account account=new Account();
account.setName("June");
account.setMoney(888d);
//调用方法
accountService.save(account);
}
//测试根据id查询
@Test
public void testFindById(){
Account account = accountService.findById(3);
System.out.println(account);
}
//测试查询所有
@Test
public void testFindAll(){
List<Account> accounts = accountService.findAll();
for (Account account : accounts) {
System.out.println(account);
}
}
//测试更新
@Test
public void testUpdate(){
Account account=new Account();
account.setId(3);//更新id为3的那条记录
account.setName("Nancy");
account.setMoney(666d);
accountService.update(account);
}
//测试删除
@Test
public void testDelete(){
accountService.delete(3);//删掉id为3的那条记录
}
}
9.jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring_db
jdbc.username=root
jdbc.password=123456
10.pom.xml
<?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.june</groupId>
<artifactId>spring_dbutils_anno</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</project>