spring的 jdbcTemplate
jdbcTemplate是spring提供的dao层用来和数据库数据交互的技术
回顾dao层操作数据库数据的技术:
1 jdbc+c3p0 任何代码要想操作数据库的数据都得遵循jdbc规范
2 dbutils apache组织提供的对jdbc+c3p0的封装
3 hibernate 对jdbc+c3p0的封装
4 jdbctemplate spring对jdbc+c3p0的封装
5 hibernateTemplate spring对hibernate又封装一次
6 mybatis 对jdbc+c3p0的封装
7 SqlMapClientTemplate sping对mybatis又封装一次
和dbutils的区别
dbutils: apache
QueryRunner qr=new QueryRunner();
qr.setDataSource(连接池)
String sql="crud"
qr.update()
qr.query()
jdbctemplate: spring
jdbctemplate qr=new jdbctemplate();
qr.setDataSource(连接池)
String sql="crud"
qr.update()
qr.query()
jdbctemplate的开发步骤:
1 导包
spring-jdbc.jar
spring-tx.jar
2 对数据库的数据进行crud操作
项目结构
jdbc.properties文件。这里使用的是mysql8.0x版本的所以url这么写
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/atm?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
jdbc.username=root
jdbc.password=123
#jdbc.driver=com.mysql.jdbc.Driver
#jdbc.url=jdbc:oracle://localhost:3306/hibernate
#jdbc.username=root
#jdbc.password=1234
#jdbc.driver=com.mysql.jdbc.Driver
#jdbc.url=jdbc:db2://localhost:3306/hibernate
#jdbc.username=root
#jdbc.password=1234
bean.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- spring加载src下的properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!-- c3p0 -->
<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- jdbcTemplate -->
<bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="c3p0"></property>
</bean>
<!-- dao -->
<bean id="atmdao" class="com.it.daoimpl.AtmDaoImpl">
<property name="jdbctemplate" ref="jdbctemplate"></property>
</bean>
<!-- service -->
<bean id="atmservice" class="com.it.serviceimpl.AtmServiceImpl">
<property name="atmdao" ref="atmdao"></property>
</bean>
</beans>
JavaBean
package com.it.bean;
public class Atm {
private String username;
private double money;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
@Override
public String toString() {
return "Atm [username=" + username + ", money=" + money + "]";
}
}
测试类
package com.it.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.it.serviceimpl.AtmServiceImpl;
public class JdbcTempalteTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
AtmServiceImpl atmservice= (AtmServiceImpl) context.getBean("atmservice");
//查找所有
atmservice.findAll();
//根据名字查找
//atmservice.findByname();
//查询有多少条消息
//atmservice.findCount();
//增加
//atmservice.save();
//删除
//atmservice.delete();
//修改
//atmservice.update();
}
}
service接口
package com.it.service;
public interface AtmService {
public void save();
public void delete();
public void update();
public void findAll();
public void findByname();
public void findCount();
}
service实现类
package com.it.serviceimpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.it.dao.AtmDao;
import com.it.daoimpl.AtmDaoImpl;
import com.it.service.AtmService;
public class AtmServiceImpl implements AtmService {
private AtmDao atmdao;
public void setAtmdao(AtmDao atmdao) {
this.atmdao = atmdao;
}
@Override
public void save() {
atmdao.save();
}
@Override
public void delete() {
atmdao.delete();
}
@Override
public void update() {
atmdao.update();
}
@Override
public void findAll() {
atmdao.findAll();
}
@Override
public void findByname() {
atmdao.findByname();
}
@Override
public void findCount() {
atmdao.findCount();
}
}
dao接口
package com.it.dao;
public interface AtmDao {
void save();
void delete();
void update();
void findAll();
void findByname();
void findCount();
}
dao实现类
package com.it.daoimpl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import com.it.bean.Atm;
import com.it.dao.AtmDao;
public class AtmDaoImpl implements AtmDao {
private JdbcTemplate jdbctemplate;
public void setJdbctemplate(JdbcTemplate jdbctemplate) {
this.jdbctemplate = jdbctemplate;
}
@Override
public void save() {
String sql="insert into atm values (?,?)";
int i = jdbctemplate.update(sql,"tonnny",123456);
System.out.println("增加了"+i);
}
@Override
public void delete() {
String sql="delete from atm where username = ?";
int i = jdbctemplate.update(sql,"tonnny");
System.out.println("删除了"+i);
}
@Override
public void update() {
String sql="update atm set money=? where username=?";
int i = jdbctemplate.update(sql, 999,"rose");
System.out.println();
}
@Override
public void findAll() {
String sql="select * from atm";
List<Atm> list = jdbctemplate.query(sql, new BeanPropertyRowMapper(Atm.class));
for (Atm atm : list) {
System.out.println(atm);
}
}
@Override
public void findByname() {
String sql="select * from atm where username = ?";
Atm atm = jdbctemplate.queryForObject(sql, new BeanPropertyRowMapper<Atm>(Atm.class),"jack");
System.out.println(atm);
}
@Override
public void findCount() {
String sql="select count(*) from atm";
Integer integer = jdbctemplate.queryForObject(sql, Integer.class);
System.out.println(integer);
}
}
jdbctemplate在dao层有2种注入方式:
set方式注入 能使用注解
继承的方式 让jdbctemplate继承JdbcDaoSupport 不能使用注解
hibernateTempalte也有这2中方式