# 创建数据库
CREATE DATABASE spring;
# 使用数据库
USE spring;
# 创建表
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('Cat',1000);
INSERT INTO account(NAME,money) VALUES('Dog',1000);
INSERT INTO account(NAME,money) VALUES('Rat',1000);
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.0.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.0.2.RELEASEversion>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.6version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
dependencies>
src
main
test
package cn.water.JDBC;
import java.io.Serializable;
public class Account implements Serializable {
/* 成员变量 */
private Integer id;
private String name;
private Float money;
/* 构造函数 */
public Account() {
}
public Account(Integer id, String name, Float 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 Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
/* toString */
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
<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="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
bean>
<bean id="jdbc" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
bean>
beans>
package cn.water.JDBC;
import org.junit.Test;
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.datasource.DriverManagerDataSource;
import java.sql.*;
import java.util.List;
public class JDBCTest {
/** Connection对象和Statement对象 */
@Test
public void test01() throws Exception {
/* 1、注册数据库驱动 */
Class.forName("com.mysql.jdbc.Driver");
/* 2、获取Connection对象 */
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/spring", "root", "root");
/* 3、SQL语句 */
String sql = "SELECT * FROM account WHERE id = 1 ";
/* 4、获取Statement对象 */
Statement statement = connection.createStatement();
/* 5、执行SQL语句,并接收结果集 */
ResultSet resultSet = statement.executeQuery(sql);
/* 6、遍历 */
while (resultSet.next()) {
int id = resultSet.getInt(1);
String name = resultSet.getString(2);
float money = resultSet.getFloat(3);
System.out.println("Account:"+id+" "+name+" "+money);
}
}
/** Connection对象和PreparedStatement对象 */
@Test
public void test02() throws Exception {
/* 1、注册 数据库驱动 */
Class.forName("com.mysql.jdbc.Driver");
/* 2、获取 Connection对象 */
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/spring", "root", "root");
/* 3、SQL语句 */
String sql = "SELECT * FROM account WHERE id = ? ";
/* 4、获取 PreparedStatement对象 */
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,1);
/* 5、执行SQL语句,并接收结果集 */
ResultSet resultSet = preparedStatement.executeQuery();
/* 6、遍历 */
while (resultSet.next()) {
int id = resultSet.getInt(1);
String name = resultSet.getString(2);
float money = resultSet.getFloat(3);
System.out.println("Account:"+id+" "+name+" "+money);
}
}
/** Spring JDBC */
@Test
public void test03() {
/* 1、获取 DriverManagerDataSource对象 */
DriverManagerDataSource dataSource = new DriverManagerDataSource();
/* 2、设置 数据库连接参数 */
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/spring");
dataSource.setUsername("root");
dataSource.setPassword("root");
/* 3、获取 JdbcTemplate对象 */
JdbcTemplate jdbcTemplate = new JdbcTemplate();
/* 4、设值 JdbcTemplate对象 */
jdbcTemplate.setDataSource(dataSource);
/* 5、执行SQL语句,并接收结果集 */
List<Account> result = jdbcTemplate.query(
"SELECT * FROM account WHERE id = ?",
new BeanPropertyRowMapper<Account>(Account.class),
1
);
/* 6、遍历 */
for (Account account : result) {
System.out.println(account);
}
}
/** Spring JDBC + Spring IoC */
@Test
public void test04() {
/* 1、加载配置文件,初始化Bean对象 */
ApplicationContext app = new ClassPathXmlApplicationContext("JDBC/Beans.xml");
/* 2、获取Bean对象 */
JdbcTemplate jdbcTemplate = app.getBean("jdbc", JdbcTemplate.class);
/* 3、执行SQL语句,并接收结果集 */
List<Account> result = jdbcTemplate.query(
"SELECT * FROM account WHERE id = ?",
new BeanPropertyRowMapper<Account>(Account.class),
1
);
/* 5、遍历 */
for (Account account : result) {
System.out.println(account);
}
}
}
/* 1、注册数据库驱动 */
Class.forName("com.mysql.jdbc.Driver");
/* 2、获取Connection对象 */
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/spring", "root", "root");
/* 3、SQL语句 */
String sql = "SELECT * FROM account WHERE id = 1 ";
/* 4、获取Statement对象 */
Statement statement = connection.createStatement();
/* 5、执行SQL语句,并接收结果集 */
ResultSet resultSet = statement.executeQuery(sql);
/* 6、遍历 */
while (resultSet.next()) {
int id = resultSet.getInt(1);
String name = resultSet.getString(2);
float money = resultSet.getFloat(3);
System.out.println("Account:"+id+" "+name+" "+money);
}
/* 1、注册 数据库驱动 */
Class.forName("com.mysql.jdbc.Driver");
/* 2、获取 Connection对象 */
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/spring", "root", "root");
/* 3、SQL语句 */
String sql = "SELECT * FROM account WHERE id = ? ";
/* 4、获取 PreparedStatement对象 */
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,1);
/* 5、执行SQL语句,并接收结果集 */
ResultSet resultSet = preparedStatement.executeQuery();
/* 6、遍历 */
while (resultSet.next()) {
int id = resultSet.getInt(1);
String name = resultSet.getString(2);
float money = resultSet.getFloat(3);
System.out.println("Account:"+id+" "+name+" "+money);
}
public class Account implements Serializable {
/* 成员变量 */
private Integer id;
private String name;
private Float money;
/* 构造函数 */
/* 设值函数 */
/* toString */
}
/* 1、获取 DriverManagerDataSource对象 */
DriverManagerDataSource dataSource = new DriverManagerDataSource();
/* 2、设置 数据库连接参数 */
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/spring");
dataSource.setUsername("root");
dataSource.setPassword("root");
/* 3、获取 JdbcTemplate对象 */
JdbcTemplate jdbcTemplate = new JdbcTemplate();
/* 4、设值 JdbcTemplate对象 */
jdbcTemplate.setDataSource(dataSource);
/* 5、执行SQL语句,并接收结果集 */
List<Account> result = jdbcTemplate.query(
"SELECT * FROM account WHERE id = ?",
new BeanPropertyRowMapper<Account>(Account.class),
1
);
/* 6、遍历 */
for (Account account : result) {
System.out.println(account);
}
public class Account implements Serializable {
/* 成员变量 */
private Integer id;
private String name;
private Float money;
/* 构造函数 */
/* 设值函数 */
/* toString */
}
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
bean>
<bean id="jdbc" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
bean>
/* 1、加载配置文件,初始化Bean对象 */
ApplicationContext app = new ClassPathXmlApplicationContext("JDBC/Beans.xml");
/* 2、获取Bean对象 */
JdbcTemplate jdbcTemplate = app.getBean("jdbc", JdbcTemplate.class);
/* 3、执行SQL语句,并接收结果集 */
List<Account> result = jdbcTemplate.query(
"SELECT * FROM account WHERE id = ?",
new BeanPropertyRowMapper<Account>(Account.class),
1
);
/* 5、遍历 */
for (Account account : result) {
System.out.println(account);
}
src
main
test
package cn.water.JDBCCase.domain;
public class Account {
/* 成员变量 */
private Integer id;
private String name;
private Float money;
/* 构造函数 */
public Account() {
}
public Account(Integer id, String name, Float 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 Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
/* toString */
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
package cn.water.JDBCCase.dao;
import cn.water.JDBCCase.domain.Account;
import java.util.List;
public interface AccountDao {
/** 查询所有 */
List<Account> findAll();
/** 查询单个 */
Account findById(Integer id);
}
package cn.water.JDBCCase.dao;
import cn.water.JDBCCase.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
import java.util.List;
public class AccountDaoImp01 implements AccountDao{
/* 成员变量 */
private DataSource dataSource;
/* 设值函数 */
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
/** 查询所有 */
public List<Account> findAll() {
return new JdbcTemplate(dataSource).query(
"SELECT * FROM account ",
new BeanPropertyRowMapper<Account>(Account.class)
);
}
/** 查询单个 */
public Account findById(Integer id) {
return new JdbcTemplate(dataSource).queryForObject(
"SELECT * FROM account WHERE id = ? ",
new BeanPropertyRowMapper<Account>(Account.class),
id
);
}
}
package cn.water.JDBCCase.dao;
import cn.water.JDBCCase.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import javax.sql.DataSource;
import java.util.List;
public class AccountDaoImp02 implements AccountDao{
/* 成员变量 */
private JdbcTemplate template;
/* 设值函数 */
public void setTemplate(JdbcTemplate template) {
this.template = template;
}
/** 查询所有 */
public List<Account> findAll() {
return template.query(
"SELECT * FROM account ",
new BeanPropertyRowMapper<Account>(Account.class)
);
}
/** 查询单个 */
public Account findById(Integer id) {
return template.queryForObject(
"SELECT * FROM account WHERE id = ? ",
new BeanPropertyRowMapper<Account>(Account.class),
id
);
}
}
package cn.water.JDBCCase.dao;
import cn.water.JDBCCase.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import java.util.List;
public class AccountDaoImp03 extends JdbcDaoSupport implements AccountDao{
/** 查询所有 */
public List<Account> findAll() {
return super.getJdbcTemplate().query(
"SELECT * FROM account ",
new BeanPropertyRowMapper<Account>(Account.class)
);
}
/** 查询单个 */
public Account findById(Integer id) {
return super.getJdbcTemplate().queryForObject(
"SELECT * FROM account WHERE id = ? ",
new BeanPropertyRowMapper<Account>(Account.class),
id
);
}
}
<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="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
bean>
<bean id="jdbc" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
bean>
<bean id="dao01" class="cn.water.JDBCCase.dao.AccountDaoImp01">
<property name="dataSource" ref="dataSource"/>
bean>
<bean id="dao02" class="cn.water.JDBCCase.dao.AccountDaoImp02">
<property name="template" ref="jdbc"/>
bean>
<bean id="dao03" class="cn.water.JDBCCase.dao.AccountDaoImp03">
<property name="dataSource" ref="dataSource"/>
bean>
beans>
package cn.water.JDBCCase;
import cn.water.JDBCCase.dao.AccountDaoImp01;
import cn.water.JDBCCase.dao.AccountDaoImp02;
import cn.water.JDBCCase.domain.Account;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JDBCTest {
@Test
public void test01(){
ApplicationContext app = new ClassPathXmlApplicationContext("JDBCCase/Beans.xml");
AccountDaoImp01 dao = app.getBean("dao01", AccountDaoImp01.class);
for (Account account : dao.findAll()) {
System.out.println(account);
}
System.out.println(dao.findById(1));
}
@Test
public void test02(){
ApplicationContext app = new ClassPathXmlApplicationContext("JDBCCase/Beans.xml");
AccountDaoImp02 dao = app.getBean("dao02", AccountDaoImp02.class);
for (Account account : dao.findAll()) {
System.out.println(account);
}
System.out.println(dao.findById(1));
}
}
/* 成员变量 */
private DataSource dataSource;
/* 设值函数 */
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
/** 查询所有 */
public List<Account> findAll() {
return new JdbcTemplate(dataSource).query(
"SELECT * FROM account ",
new BeanPropertyRowMapper<Account>(Account.class)
);
}
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
bean>
<bean id="jdbc" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
bean>
<bean id="dao01" class="cn.water.JDBCCase.dao.AccountDaoImp01">
<property name="dataSource" ref="dataSource"/>
bean>
/* 成员变量 */
private JdbcTemplate template;
/* 设值函数 */
public void setTemplate(JdbcTemplate template) {
this.template = template;
}
/** 查询所有 */
public List<Account> findAll() {
return template.query(
"SELECT * FROM account ",
new BeanPropertyRowMapper<Account>(Account.class)
);
}
/** 查询单个 */
public Account findById(Integer id) {
return template.queryForObject(
"SELECT * FROM account WHERE id = ? ",
new BeanPropertyRowMapper<Account>(Account.class),
id
);
}
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
bean>
<bean id="jdbc" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
bean>
<bean id="dao02" class="cn.water.JDBCCase.dao.AccountDaoImp02">
<property name="template" ref="jdbc"/>
bean>
继承 JdbcDaoSupport类,并向其传递 DataSource,然后调用父类方法,获取Connection对象,最后执行操作数据库的方法。
public class AccountDaoImp03 extends JdbcDaoSupport implements AccountDao{
/** 查询所有 */
public List<Account> findAll() {
return super.getJdbcTemplate().query(
"SELECT * FROM account ",
new BeanPropertyRowMapper<Account>(Account.class)
);
}
/** 查询单个 */
public Account findById(Integer id) {
return super.getJdbcTemplate().queryForObject(
"SELECT * FROM account WHERE id = ? ",
new BeanPropertyRowMapper<Account>(Account.class),
id
);
}
}
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
bean>
<bean id="jdbc" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
bean>
<bean id="dao03" class="cn.water.JDBCCase.dao.AccountDaoImp03">
<property name="dataSource" ref="dataSource"/>