#mybatis。它是对jdbc的封装
#spring框架也提供了对jdbc的封装:JdbcTemplate。用于支持持久层的操作。
#JdbcTemplate是spring提供的一个模板类,它是对jdbc的封装。用于支持持久层的操作。它的特点是:简单、方便
/*创建账户表*/
create table account(
id int primary key auto_increment,
name varchar(40),
money float
)ENGINE=InnoDB character set utf8 collate utf8_general_ci;
/*初始化新增三个账户*/
insert into account(name,money) values('小明',1000);
insert into account(name,money) values('小花',1000);
insert into account(name,money) values('小王',1000);
package com.KkaBum.jdbc;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class JdbcTemplateDemo {
public static void main(String[] args) {
/**
* 需求:通过JdbcTemplate实现添加一条账户记录,到账户表account中
*/
//创建jdbcTemplate对象,内部封装了jdbc的实际操作
JdbcTemplate jdbcTemplate = new JdbcTemplate();
//创建连接池对象,并且交给jdbcTemplate对象
DriverManagerDataSource dataSource = new DriverManagerDataSource();
//设置数据源对象
dataSource.setUsername("root");
dataSource.setPassword("root");
dataSource.setUrl("jdbc:mysql://localhost:3306/KkaBum");
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
jdbcTemplate.setDataSource(dataSource);
//添加账户信息
String sql = "insert into account(name,money) values('kabum',20000)";
jdbcTemplate.update(sql);
}
}
<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.0modelVersion>
<groupId>com.KkaBumgroupId>
<artifactId>spring02_jdbcTemplateartifactId>
<version>1.0.0version>
<properties>
<spring.version>5.0.6.RELEASEspring.version>
<mysql.version>5.1.30mysql.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
dependency>
dependencies>
project>
applicationContext.xml文件放在resources目录下
<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="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource">property>
bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="root">property>
<property name="password" value="root">property>
<property name="url" value="jdbc:mysql://localhost:3306/KkaBum">property>
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
bean>
beans>
package com.KkaBum.jdbc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class JdbcTemplateDemo {
public static void main(String[] args) {
/**
* 需求:通过JdbcTemplate实现添加一条账户记录,到账户表account中
*/
//加载spring配置文件,创建spring IOC容器
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
// 从spring IOC容器中,获取jdbcTemplate
JdbcTemplate jdbcTemplate = (JdbcTemplate) ac.getBean("jdbcTemplate");
// 添加用户
String sql = "insert into account(name,money) values('小敏',999)";
jdbcTemplate.update(sql);
}
}
#需求:
1.新增一个账户
2.根据用户id修改用户
3.根据用户id删除用户
4.查询用户(查询一行、查询多行)
5.查询一行一列
package com.KkaBum.pojo;
public class Account {
private Integer id;
private String name;
private double money;
public Account() {
}
public Account(Integer id, String name, double money) {
this.id = id;
this.name = name;
this.money = 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 +
'}';
}
}
package com.KkaBum.jdbc;
import com.KkaBum.pojo.Account;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class JdbcCRUD {
public static void main(String[] args) {
/*
#需求:
1.新增一个账户
2.根据用户id修改用户
3.根据用户id删除用户
4.查询用户(查询一行、查询多行)
5.查询一行一列
*/
//加载spring配置文件,创建spring IOC容器
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
// 从spring IOC容器中,获取jdbcTemplate
JdbcTemplate jdbcTemplate = (JdbcTemplate) ac.getBean("jdbcTemplate");
}
}
//需求1:新增账户
String sql1 = "insert into account values(null,?,?)";
jdbcTemplate.update(sql1,"柳岩",2000);
//需求2:根据用户id修改用户
String sql2 = "update account set money = ? where id = ?";
jdbcTemplate.update(sql2,9999,5);
//需求3:根据用户id删除用户
String sql3 = "delete from account where id = ?";
dbcTemplate.update(sql3,1);
//需求4.1:查询用户(查询一行)
String sql4 = "select * from account where id = ?";
List<Account> accountList = jdbcTemplate.query(sql4, new RowMapper<Account>() {
//每有一行数据,当前方法都会执行一次
@Override
public Account mapRow(ResultSet rs, int i) throws SQLException {
//获取每一条的数据
int id = rs.getInt("id");
String name = rs.getString("name");
double money = rs.getDouble("money");
//封装到实体类中取
Account account = new Account();
account.setId(id);
account.setName(name);
account.setMoney(money);
return account;
}
}, 1);
for (Account account : accountList) {
System.out.println("account = " + account);
}
优化:RowMapper用于对结果集进行封装,但是代码比较多,spring给我们提供了自动映射,spring提供的自动映射的实现类是BeanPropertyRowMapper,
使用BeanPropertyRowMapper前提条件: 保证实体的属性名和列名一样。
String sql4 = "select * from account where id = ?";
List<Account> accountList = jdbcTemplate.query(sql4, new BeanPropertyRowMapper<>(Account.class),1);
for (Account account : accountList) {
System.out.println("account = " + account);
}
String sql5 = "select * from account";
List<Account> accountList = jdbcTemplate.query(sql5, new RowMapper<Account>() {
@Override
public Account mapRow(ResultSet rs, int i) throws SQLException {
int id = rs.getInt("id");
String name = rs.getString("name");
double money = rs.getDouble("money");
Account account = new Account();
account.setId(id);
account.setName(name);
account.setMoney(money);
return account;
}
});
for (Account account : accountList) {
System.out.println("account = " + account);
}
优化:
//需求5:查询一行一列
String sql6 = "select count(*) from account";
Integer count = jdbcTemplate.queryForObject(sql6, Integer.class);
System.out.println("count = " + count);
导入依赖
<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.0modelVersion>
<groupId>com.KkaBumgroupId>
<artifactId>spring02_jdbcTemplate_crudartifactId>
<version>1.0.0version>
<properties>
<spring.version>5.0.6.RELEASEspring.version>
<mysql.version>5.1.30mysql.version>
<c3p0.version>0.9.1.2c3p0.version>
<druid.version>1.0.29druid.version>
<dbcp.version>1.4dbcp.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
dependency>
<dependency>
<groupId>c3p0groupId>
<artifactId>c3p0artifactId>
<version>${c3p0.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>${druid.version}version>
dependency>
<dependency>
<groupId>commons-dbcpgroupId>
<artifactId>commons-dbcpartifactId>
<version>${dbcp.version}version>
dependency>
dependencies>
project>
<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="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dbcpDataSource">property>
bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="root">property>
<property name="password" value="root">property>
<property name="url" value="jdbc:mysql://localhost:3306/KkaBum">property>
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
bean>
<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root">property>
<property name="password" value="root">property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/KkaBum">property>
<property name="driverClass" value="com.mysql.jdbc.Driver">property>
bean>
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="root">property>
<property name="password" value="root">property>
<property name="url" value="jdbc:mysql://localhost:3306/KkaBum">property>
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
bean>
<bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="root">property>
<property name="password" value="root">property>
<property name="url" value="jdbc:mysql://localhost:3306/KkaBum">property>
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
bean>
beans>
# 作用:在测试中自动创建spring容器,并且自动使用注解赋值
步骤:
1.@RunWith 来替换spring的运行器。可以帮我们自动创建spring容器
2.@ContextConfiguration 来告知spring的运行器 配置文件或者配置类的位置
依赖
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>5.0.6.RELEASEversion>
dependency>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test{
...
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class) //导入配置类
public class Test{
...
}