JdbcTemplate的基本用法
JdbcTemplate原始代码
publicclassJdbcTemplateDemo1 {
publicstaticvoidmain(String[] args) {
//准备数据源:spring的内置数据源
DriverManagerDataSourceds=newDriverManagerDataSource();
//com.mysql.cj.jdbc.Driver
ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/eesy?serverTimezone=GMT%2B8");
ds.setUsername("ggbond");
ds.setPassword("password");
//1.创建JdbcTemplate对象
JdbcTemplatejt=newJdbcTemplate();
//2.给jt设置数据源
jt.setDataSource(ds);
//3.执行操作
jt.execute("insert into account(name,money) values ('ddd',1000)");
}
}
使用AOP对代码解耦
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="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">
<!-- 配置JdbcTemplate-->
<beanid="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate">
<propertyname="dataSource"ref="dataSource"></property>
</bean>
<!-- 配置数据源-->
<beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName"value="com.mysql.cj.jdbc.Driver"></property>
<propertyname="url"value="jdbc:mysql://localhost:3306/eesy?serverTimezone=GMT%2B8"></property>
<propertyname="username"value="ggbond"></property>
<propertyname="password"value="password"></property>
</bean>
</beans>
publicclassJdbcTemplateDemo2 {
publicstaticvoidmain(String[] args) {
//1.获取容器
ApplicationContextac=newClassPathXmlApplicationContext("bean.xml");
//2.获取对象
JdbcTemplatejt= (JdbcTemplate)ac.getBean("jdbcTemplate");
//3.执行操作
jt.execute("insert into account(name,money) values ('eee',1000)");
}
}
JdbcTemplate的CRUD操作
publicclassJdbcTemplateDemo3 {
publicstaticvoidmain(String[] args) {
//1.获取容器
ApplicationContextac=newClassPathXmlApplicationContext("bean.xml");
//2.获取对象
JdbcTemplatejt= (JdbcTemplate)ac.getBean("jdbcTemplate");
//增加(保存)
jt.update("insert into account(name,money) values (?,?)", "fff",1000);
//删除
jt.update("delete from account where id=?", 10);
//更改(更新)
jt.update("update account set name=?,money=? where id=?", "test",4567,11);
//查询所有
List<Account>list=jt.query("select * from account where money=?", newBeanPropertyRowMapper<>(Account.class), 1000);
for (Accountaccount: list) {
System.out.println(account);
}
//查询一个
List<Account>accounts=jt.query("select * from account where id=?", newBeanPropertyRowMapper<>(Account.class), 11);
System.out.println(accounts.isEmpty()?"没有内容":accounts.get(0));
//查询返回一行一列(使用聚合函数,但不使用group by子句)
Longcount=jt.queryForObject("select count(*) from account where money=?", Long.class, 1000);
System.out.println(count);
}
}
JdbcTemplate在DAO中的使用
/**
* 账户的持久层实现类
*/
publicclassAccountDaoImplimplementsIAccountDao {
//通过set方法注入JdbcTemplate对象
privateJdbcTemplatejt;
publicvoidsetJt(JdbcTemplatejt) {
this.jt=jt;
}
/**
* 根据id查找,可能为0或1
* @param accountId
* @return
*/
@Override
publicAccountfindAccountById(IntegeraccountId) {
List<Account>list=jt.query("select * from account where id=?", newBeanPropertyRowMapper<>(Account.class), accountId);
returnlist.isEmpty()?null:list.get(0);
}
/**
* 根据name查找,可能为0或1或多个
* @param accountName
* @return
*/
@Override
publicAccountfindAccountByName(IntegeraccountName) {
List<Account>list=jt.query("select * from account where name=?", newBeanPropertyRowMapper<>(Account.class), accountName);
if (list.isEmpty()){//0
returnnull;
}elseif (list.size()>1){//多个
thrownewRuntimeException("结果集不唯一");
}
returnlist.get(0);//1
}
/**
* 更新账户
* @param account
*/
@Override
publicvoidupdateAccount(Accountaccount) {
jt.update("update account set name=?,money=? where id=?", account.getName(),account.getMoney(),account.getId());
}
}
dao中通过set方法注入JdbcTemplate对象的重复代码,spring封装在JdbcDaoSupport类中,只要继承即可