1.3.1创建数据库,并导入jar包
1.3.2创建配置文件
<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-4.3.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">property>
<property name="url"
value="jdbc:mysql://localhost:3306/spring?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true">property>
<property name="username" value="root">property>
<property name="password" value="12345678">property>
bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource">property>
bean>
beans>
1.3.3创建测试类
使用单元测试
这里测试execute()方法
package com.lin.jdbc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class JdbcTemplateTest {
/**
* 使用 execute()方法建表
*/
/*
* public static void main(String[] args) { //加载配置文件
*
* ApplicationContext applicationContext =new
* ClassPathXmlApplicationContext("applicationContext.xml");
*
* //获取jdbcTemplate实例 JdbcTemplate jdbcTemplate=(JdbcTemplate)
* applicationContext.getBean("jdbcTemplate");
*
* //使用execute()方法执行SQL语句,创建用户账户管理表account
*
* jdbcTemplate.execute("create table accoutn("+
* "id int primary key auto_increment,"+ "usrname varchar(50),"+
* "balance double)"); System.out.println("账户表创建成功!"); }
*/
@Test
public void mainTest() {
//加载配置文件
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");
//获取jdbcTemplate实例
JdbcTemplate jdbcTemplate=(JdbcTemplate) applicationContext.getBean("jdbcTemplate");
//使用execute()方法执行SQL语句,创建用户账户管理表account
jdbcTemplate.execute("create table account("+
"id int primary key auto_increment,"+
"usrname varchar(50),"+
"balance double)");
System.out.println("账户表创建成功!");
}
}
package com.lin.jdbc;
public class Account {
private Integer id;// 账户id;
private String username;// 用户名
private Double balance;// 账户余额
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Double getBalance() {
return balance;
}
public void setBalance(Double balance) {
this.balance = balance;
}
@Override
public String toString() {
return "Account [id=" + id + ", username=" + username + ", balance=" + balance + "]";
}
}
创建Dao类,及其实现方法
package com.lin.jdbc.dao;
import com.lin.jdbc.Account;
public interface AccountDao {
//添加
public int addAccount(Account account);
//更新
public int updateAccount(Account account);
//删除
public int deleteAccount(int id);
}
package com.lin.jdbc.dao.Impl;
import org.springframework.jdbc.core.JdbcTemplate;
import com.lin.jdbc.Account;
import com.lin.jdbc.dao.AccountDao;
public class AccountDaoImpl implements AccountDao {
// 声明一个JdbcTmplate属性及其Setter方法
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
// 添加账户
@Override
public int addAccount(Account account) {
// 定义SQL
String sql = "insert into account(username,balance) value(?,?)";
// 定义数组来存放SQL语句中的参数
Object[] obj = new Object[] { account.getUsername(), account.getBalance(), };
// 执行添加操作,返回受SQL语句影响的条数
int num = this.jdbcTemplate.update(sql, obj);
return num;
}
// 更新账户
@Override
public int updateAccount(Account account) {
// 定义SQL
String sql = "update account set username=? , balance=? where id=?";
// 定义数组来存放SQL语句中的参数
Object[] obj = new Object[] { account.getUsername(), account.getBalance(), account.getId()};
// 执行更新操作,返回受SQL语句影响的条数
int num = this.jdbcTemplate.update(sql,obj);
return num;
}
// 删除账户
@Override
public int deleteAccount(int id) {
// 定义SQL
String sql = "delete from account where id=?";
// 执行更新操作,返回受SQL语句影响的条数
int num=this.jdbcTemplate.update(sql,id);
return num;
}
}
在上面的配置文件中加入accountDao的Bean
<bean id="accountDao" class="com.lin.jdbc.dao.Impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate">property>
bean>
使用单元测试
package com.lin.jdbc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import com.lin.jdbc.dao.AccountDao;
public class JdbcTemplateTest {
/**
* 使用 execute()方法建表
*/
/*
* public static void main(String[] args) { //加载配置文件
*
* ApplicationContext applicationContext =new
* ClassPathXmlApplicationContext("applicationContext.xml");
*
* //获取jdbcTemplate实例 JdbcTemplate jdbcTemplate=(JdbcTemplate)
* applicationContext.getBean("jdbcTemplate");
*
* //使用execute()方法执行SQL语句,创建用户账户管理表account
*
* jdbcTemplate.execute("create table accoutn("+
* "id int primary key auto_increment,"+ "usrname varchar(50),"+
* "balance double)"); System.out.println("账户表创建成功!"); }
*/
@Test
public void mainTest() {
//加载配置文件
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");
//获取jdbcTemplate实例
JdbcTemplate jdbcTemplate=(JdbcTemplate) applicationContext.getBean("jdbcTemplate");
//使用execute()方法执行SQL语句,创建用户账户管理表account
jdbcTemplate.execute("create table account("+
"id int primary key auto_increment,"+
"username varchar(50),"+
"balance double)");
System.out.println("账户表创建成功!");
}
@Test
public void addAccountTest() {
//加载配置文件
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取AccountDao实例
AccountDao accountDao=(AccountDao) applicationContext.getBean("accountDao");
//创建Account对象,并向Account对象中添加数据
Account account=new Account();
account.setUsername("tom");
account.setBalance(4000.00);
//执行addAccount()方法,并获取返回结果
int num=accountDao.addAccount(account);
if(num>0) {
System.out.println("成功插入"+num+"条数据!");
}else {
System.out.println("插入操作执行失败");
}
}
@Test
public void updateAccountTest() {
//加载配置文件
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取AccountDao实例
AccountDao accountDao=(AccountDao) applicationContext.getBean("accountDao");
//创建Account对象,并向Account对象中添加数据
Account account=new Account();
account.setId(1);
account.setUsername("tom");
account.setBalance(3000.00);
//执行updateAccount()方法,并获取返回结果
int num=accountDao.updateAccount(account);
if(num>0) {
System.out.println("成功修改"+num+"条数据!");
}else {
System.out.println("插入操作执行失败");
}
}
@Test
public void deleteAccountTest() {
//加载配置文件
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取AccountDao实例
AccountDao accountDao=(AccountDao) applicationContext.getBean("accountDao");
//创建Account对象,并向Account对象中添加数据
Account account=new Account();
int num=accountDao.deleteAccount(2);
if(num>0) {
System.out.println("成功删除"+num+"条数据!");
}else {
System.out.println("插入操作执行失败");
}
}
}
//通过id查询
public Account findAccountById(int id);
//查询所有账户
public List<Account> findAllAccount();
// 通过id查询账户数据信息
@Override
public Account findAccountById(int id) {
// 定义SQL
String sql = "select * from account where id=?";
// 创建一个新的BeanPropertyRowMapper对象
RowMapper<Account> rowMapper = new BeanPropertyRowMapper<Account>(Account.class);
// 将id绑定到SQL语句中,通过RowMapper返回一个Object类型的单行记录
return this.jdbcTemplate.queryForObject(sql, rowMapper, id);
}
//查询所有账户信息
@Override
public List<Account> findAllAccount() {
// 定义SQL
String sql = "select * from account";
// 创建一个新的BeanPropertyRowMapper对象
RowMapper<Account> rowMapper = new BeanPropertyRowMapper<Account>(Account.class);
//执行静态查询的sql,通过RowMapper返回结果
return this.jdbcTemplate.query(sql, rowMapper);
}
2.测试类
@Test
public void findAccountByIdTest() {
// 加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取AccountDao实例
AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
// 创建Account对象,并向Account对象中添加数据
Account account = new Account();
account=accountDao.findAccountById(1);
System.out.println(account);
}
@Test
public void findAllAccountTest() {
// 加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取AccountDao实例
AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
// 创建Account对象,并向Account对象中添加数据
List<Account> account= accountDao.findAllAccount();
for (Account act : account) {
System.out.println(act);
}
}