1.完成Account类中的定义私有变量(id,userName,balance)
package com.spring.jdbc.JdbcTemplate;public class Account {//定义私有变量private Integer id; //账户IDprivate String userName; //账户名称private Double balance; //账户余额//定义set和get方法完成依赖注入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;}//输出函数public String toString() {return "id="+id + ",userName="+userName+",balance="+balance;}}
2.完成AccountManage接口类,定义接口(插入add、更新update、查询单个query、查询所有queryAll、删除delete)
// com.spring.jdbc.JdbcTemplate;
package com.spring.jdbc.JdbcTemplate;public interface AccountManage {//定义接口//插入账号记录public void addAccount(Account account);//更新账号记录public void updateAccount(Account account);//查询单个账户记录public void queryAccountById(Integer id);//查询所有账户记录public void queryAllAccount();//删除账号记录public void deleteAccount(Integer id);}
3.完成AccountManageImpl接口实现类
package com.spring.jdbc.JdbcTemplate;import java.util.List;import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;//接口实现类
public class AccountManageImpl implements AccountManage {//定义JdbcTemplate属性以及相关的set方法private JdbcTemplate jdbcTemplate;public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}@Overridepublic void addAccount(Account account) {//定义一个SQL语句,实现插入一条数据String sql="insert into accounts(id,userName,balance) value(?,?,?)";//定义数组存放SQL语句中的参数Object[] obj = new Object[] {account.getId(),account.getUserName(),account.getBalance()};//执行SQL语句,返回执行添加操作的记录条数int num = jdbcTemplate.update(sql,obj);if(num >0) {System.out.println("成功插入了"+num+"条转账记录");System.out.println(account);}else {System.out.println("执行插入账号记录失败");}}@Overridepublic void queryAccountById(Integer id) {//定义一个SQL语句,实现根据账户ID进行查询String sql="select * from accounts where id=?";//创建一个BeanProPertRowMapper对象RowMapper<Account> rowMapper;rowMapper = new BeanPropertyRowMapper<Account>(Account.class);//执行Sql查询语句,并通过rowMapper类返回Account类对象Account account = jdbcTemplate.queryForObject(sql,rowMapper,id);System.out.println("成功查询了1个用户记录");System.out.println(account);}@Overridepublic void queryAllAccount() {//定义SQL查询语句,实现查询所有的账号记录String sql="select * from accounts";//创建一个BeanProPertRowMapper对象RowMapper<Account> rowMapper;rowMapper = new BeanPropertyRowMapper<Account>(Account.class);//执行Sql查询语句,并通过rowMapper类返回Account类对象List<Account>accounts = jdbcTemplate.query(sql,rowMapper);System.out.println("成功查询了所有的用户记录");//循环输出每个记录for(Account account:accounts) {System.out.println(account);}}@Overridepublic void updateAccount(Account account) {//定义SQL查询语句,实现更新账号id余额String sql="update accounts set balance=? where id=?";//定义数组用来存放SQL语句中的参数Object[] params = new Object[] {account.getBalance(),account.getId()};//执行SQL更新语句,返回执行更新操作的记录条数int num = jdbcTemplate.update(sql,params);if(num>0) {System.out.println("数据更新成功"+num+"条账号记录");}else {System.out.println("执行更新账号记录失败");}}@Overridepublic void deleteAccount(Integer id) {//定义SQL查询语句,实现更新账号id余额String sql="delete from accounts where id=?";//执行SQL删除语句,返回执行删除操作的记录条数int num = jdbcTemplate.update(sql,id);if(num>0) {System.out.println("数据成功删除"+num+"条账号记录");}else {System.out.println("执行删除账号记录失败");}}}
4.完成jdbcTemplateTest程序测试类,完成创建数据表,查询、插入、更新、删除等操作。
package com.spring.jdbc.JdbcTemplate;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;public class JdbcTemplateTest {public static void main(String[] args) {//加载jdbcTemplate.xml文件ApplicationContext context;context = new ClassPathXmlApplicationContext("jdbcTemplate.xml");//获取bean实例jdbcTemplateJdbcTemplate jdbcTemplate = (JdbcTemplate)context.getBean("jdbcTemplate");//1、使用execute()方法执行sql创建表语句,创建账户表accountsjdbcTemplate.execute("create table if not exists accounts(id int,userName varchar(50),balance double)");System.out.println("************************************");System.out.println(" 账户表 accounts 创建成功!");System.out.println("************************************");//2、获取Bean实例accountManageAccountManage accountManage;accountManage = (AccountManage)context.getBean("accountManage");//3、创建Account类对象accountAccount account = new Account();//插入数据:给属性赋值,插入两条数据System.out.println("====== I N S E R T =====");account.setId(20240702);account.setUserName("黄桑");account.setBalance(1000.0);accountManage.addAccount(account);System.out.println("====== S E L E C T =====");//查询单条数据:id为20240702的账户记录accountManage.queryAccountById(20240702);//查询所有数据: 所有账户记录accountManage.queryAllAccount();System.out.println("====== U P D A T E =====");//更新一条数据:id为20240702的账户名字改为小张account.setId(20240702);account.setBalance(1000.0);accountManage.updateAccount(account);//查询所有数据: 所有账户记录accountManage.queryAllAccount();System.out.println("====== D E L E T E =====");//删除账户ID为20240702的账户记录accountManage.deleteAccount(20240702);//查询所有数据: 所有账户记录accountManage.queryAllAccount();//关闭容器((AbstractApplicationContext) context).close();}}
5.jdbcTemple.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--配置数据源--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><!--数据库驱动--><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/><!--数据库url--><property name="url" value="jdbc:mysql://localhost:3306/db_spring"/><!--数据库用户名--><property name="username" value="root"/><!--数据库密码--><property name="password" value="123456"/></bean><!--配置JDBC模板--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"/></bean><!--配置注入类--><bean id="accountManage" class="com.spring.jdbc.JdbcTemplate.AccountManageImpl"><property name="jdbcTemplate" ref="jdbcTemplate"/></bean>
</beans>
运行说明
将项目文件夹下的resource目录中的jdbcTemple.xml拷贝到bin目录下
手动创建数据库db_springCREATE DATABASE db_spring;