JDBC是Spring数据集成/访问的重要模块
4.1Spring JDBC
Spring 的 JDBC模块负责数据库资源管理和错误处理,简化开发人员对数据库的操作。
4.1.1Spring JdbcTemplate 的解析
JdbcTemplate类是Spring JdBC的核心类,该类继承抽象类JdbcAccessor,实现JdbcOperations接口。
抽象类JdbcAccessor中,包含访问数据库的公共属性,主要用于数据库的连接和转译工作
JdbcOperations接口定义了JdbcTemplate类中可以操作的集合包括增删改查。
4.1.2Spring JDBC的配置
Spring JDBC模块主要由4个包组成,分别是core(核心包),daraSource(数据源包),object(对象包),support(支持包)
jdbc配置的模板
需要在xml文件中定义三个bean实例,分别是datasource,jdbcTemplate和需要注入类的bean,分别负责数据源配置,将数据源注入到 jdbctemplate,然后将其注入具体的类。
"org.springframework.jdbc.datasource.DriverManagerDataSource">
class="org.springframework.jdbc.core.JdbcTemplate">
4.2Spring JdbcTemplate 的常用方法
4.2.1execute()
这个方法可以执行sql语句,首先执行一个创建表的操作,先引入相应的jar包,然后根据配置文件的模板修改配置文件,这里使用mysql数据库,并且已经创建了spring数据库,这里的配置文件也做出了一定的修改,这里没有使用实体注入类,而且连接数据库的url和数据库驱动由于采用模板的值会报错错误,也根据网上的教程做出了修改。
"org.springframework.jdbc.datasource.DriverManagerDataSource">
class="org.springframework.jdbc.core.JdbcTemplate">
编写创建表的测试类
获取jdbcTemplate实例,调用execute方法,将创建表的sql语句作为参数填入。
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jdTemplate = (JdbcTemplate)applicationContext.getBean("jdbcTemplate");
String sql = "CREATE TABLE `account` (\n" +
" `id` int(11) NOT NULL,\n" +
" `username` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,\n" +
" `balance` double(255, 0) DEFAULT NULL,\n" +
" PRIMARY KEY (`id`) USING BTREE\n" +
") ";
jdTemplate.execute(sql);
System.out.println("创建成功 ");
}
运行结果如图
4.2.2update()
update方法可以实现增删改操作。下面分别定义用户实体类,操作接口和操作类。
定义实体类,实现接口
package com.itheima.jdbc;
public class Account {
private Integer 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 + "]";
}
}
接口及其实现类
public interface AccountDao {
public int addAccount(Account account);
public int deleteAccount(int id);
public int updateAccount(Account account);
}
public class AccountDaoImpl implements AccountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate= jdbcTemplate;
}
public int addAccount(Account account) {
String sql = "insert into account(username,balance) value(?,?)";
Object[] obj = new Object[]{
account.getUsername(),
account.getBalance()
};
int num = this.jdbcTemplate.update(sql,obj);
return num;
}
public int deleteAccount(int id) {
// TODO Auto-generated method stub
String sql = "delete from account where id = ?";
int num = this.jdbcTemplate.update(sql,id);
return num;
}
public int updateAccount(Account account) {
// TODO Auto-generated method stub
String sql = "update account set username = ?,balance = ? where id = ?";
Object[] params = new Object[]{
account.getUsername(),
account.getBalance(),
account.getId()
//"大魔王",12.5,1
};
int num = this.jdbcTemplate.update(sql,params);
return num;
}
}
测试方法,这里采用了junit4测试
package com.itheima.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 {
//@Test
// public void mainTest( ) {
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// JdbcTemplate jdTemplate = (JdbcTemplate)applicationContext.getBean("jdbcTemplate");
// String sql = "CREATE TABLE `account` (\n" +
// " `id` int(11) auto_increment NOT NULL,\n" +
// " `username` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,\n" +
// " `balance` double(255, 0) DEFAULT NULL,\n" +
// " PRIMARY KEY (`id`) USING BTREE\n" +
// ") ";
// jdTemplate.execute(sql);
// System.out.println("创建成功 ");
// }
//@Test
// public void addAcc( ) {
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// AccountDao accountDao = (AccountDao)applicationContext.getBean("accountDao");
// Account account1 = new Account();
// account1.setId(15);
// account1.setUsername("张伟隆");
// account1.setBalance(100.0);
// int num = accountDao.addAccount(account1);
// if (num>0) {
// System.out.println("添加成功 "+num);
//}
// else
// System.out.println("插入失败");
// }
//@Test
//public void updateacc(){
//ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// AccountDao accountDao = (AccountDao)applicationContext.getBean("accountDao");
// Account account2 = new Account();
// account2.setId(1);
// account2.setUsername("小魔王");
// account2.setBalance(50.0);
// int num = accountDao.updateAccount(account2);
// if (num>0){
// System.out.println("更新成功");
//}else {
//System.out.println("失败");
//}
//}
@Test
public void deleteAcc(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountDao accountDao = (AccountDao)applicationContext.getBean("accountDao");
int num = accountDao.deleteAccount(1);
if (num>0){
System.out.println("删除成功");
}else {
System.out.println("失败");
}
}
}
4.2.3query()
该方法用于查询,这里定义两个方法,一个用于根据id查询,一个查询所有
首先在接口中定义方法,然后在UserDaoImpl中给出具体实现
public Account findByAccount(int id);
public List findAll();
//实现
@Override
public Account findByAccount(int id) {
// TODO Auto-generated method stub
String sql = "select * from account where id = ?";
org.springframework.jdbc.core.RowMapper rowMapper = new BeanPropertyRowMapper(Account.class);
return this.jdbcTemplate.queryForObject(sql, rowMapper,id);
}
@Override
public List findAll() {
// TODO Auto-generated method stub
String sql = "select * from account";
org.springframework.jdbc.core.RowMapper rowMapper = new BeanPropertyRowMapper(Account.class);
return this.jdbcTemplate.query(sql, rowMapper);
}
编写测试结果