1. 创建数据库
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);
2. 注入依赖
<?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>org.example</groupId>
<artifactId>Springdemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--连接池-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
</dependencies>
</project>
3. 创建三层架构
AccountService.java
package service;
import pojo.Account;
import java.util.List;
public interface AccountService {
List<Account> findAll();
Account findAccountById(Integer id);
void addAccount(Account account);
void updateAccount(Account account);
void delAccount(Integer id);
}
AccountDao.java
package dao;
import pojo.Account;
import java.util.List;
public interface AccountDao {
List<Account> findAll();
Account findAccountById(Integer id);
void addAccount(Account account);
void updateAccount(Account account);
void delAccount(Integer id);
}
package service.impl;
import dao.AccountDao;
import pojo.Account;
import service.AccountService;
import java.util.List;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public List<Account> findAll() {
return accountDao.findAll();
}
public Account findAccountById(Integer id) {
return accountDao.findAccountById(id);
}
public void addAccount(Account account) {
accountDao.addAccount(account);
}
public void updateAccount(Account account) {
accountDao.updateAccount(account);
}
public void delAccount(Integer id) {
accountDao.delAccount(id);
}
}
package dao.impl;
import dao.AccountDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import pojo.Account;
import java.sql.SQLException;
import java.util.List;
public class AccountDaoImpl implements AccountDao {
private QueryRunner runner;
public void setRunner(QueryRunner runner) {
this.runner = runner;
}
public List<Account> findAll() {
try {
return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
} catch (Exception e) {
throw new RuntimeException();
}
}
public Account findAccountById(Integer id) {
try {
return runner.query("select * from account where id = ?",new BeanHandler<Account>(Account.class),id);
} catch (Exception e) {
throw new RuntimeException();
}
}
public void addAccount(Account account) {
try {
runner.update("insert into account(uid,money) values(?,?)",account.getUid(),account.getMoney());
} catch (Exception e) {
throw new RuntimeException();
}
}
public void updateAccount(Account account) {
try {
runner.update("update account set uid=?,money=? where id=?",account.getUid(),account.getMoney(),account.getId());
} catch (Exception e) {
throw new RuntimeException();
}
}
public void delAccount(Integer id) {
try {
runner.update("delete from account where id=?",id);
} catch (Exception e) {
throw new RuntimeException();
}
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="accountService" class="service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<bean id="accountDao" class="dao.impl.AccountDaoImpl">
<property name="runner" ref="runner"></property>
</bean>
<!--配置QueryRunner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<!--配置QueryRunner-->
<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/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
</beans>
5. 测试
查询所有信息:
根据id进行模糊查询:
增加信息:
修改信息:
删除信息: