简述Spring JDBC框架
1,JDBC 框架概述
因为JDBC的操作过程比较烦索,并且操作步骤大多相对固定,所以很多框架都对其进行了封装与优化。其中著名的Spring框架也对JDBC做了简单封装。
Spring框架提供了一个JdbcTemplate类简化JDBC的开发,它的特点是:简单、方便,这个技术俗称Spring JDBC技术。
Spring JDBC理解为Spring框架对持久层JDBC操作进行了封装与优化,化繁琐为简单实用,即解决持久层的技术问题。
在使用普通的 JDBC 数据库时,就会很麻烦的写不必要的代码来处理异常,打开和关闭数据库连接等。但 Spring JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQL 语句,处理异常,处理事务,到最后关闭连接。
所以当从数据库中获取数据时,你所做的是定义连接参数,指定要执行的 SQL 语句,每次迭代完成所需的工作。
2,Spring JDBC 示例
想要理解带有 jdbc 模板类的 Spring JDBC 框架的相关概念,让我们编写一个简单的示例,来实现简单的查询操作。
这是我的项目结构
(1)建立一个数据库表 我的表数据如下:
(2)创建实体类
package cn.tb.entity;
public class City {
private int cid;
private String cname;
private int pid;
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public City(int cid, String cname, int pid) {
this.cid = cid;
this.cname = cname;
this.pid = pid;
}
public City() {
}
}
(3)创建接口
package cn.tb.dao;
import cn.tb.entity.City;
import java.util.List;
public interface CityDao {
public List<City> findCnameAll(); //查询所有城市
(4)接口实现类 这里用到了JdbcTemplate 类
JdbcTemplate 类
Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中。
JdbcTemplate主要提供以下五类方法:
execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;
update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句;
query方法及queryForXXX方法:用于执行查询相关语句;
call方法:用于执行存储过程、函数相关语句。
package cn.tb.dao.impl;
import cn.tb.dao.CityDao;
import cn.tb.entity.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository
public class CityImpl implements CityDao{
//JdbcTemplate 类
@Autowired
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<City> findCnameAll() {
return jdbcTemplate.query("select * from city",new RowMapper(){
@Nullable
@Override
public City mapRow(ResultSet rs, int i) throws SQLException {
City city = new City();
city.setCid(rs.getInt(1));
city.setCname(rs.getString(2));
city.setPid(rs.getInt(3));
return city;
}
});
}
(5)service
package cn.tb.service;
import cn.tb.dao.CityDao;
import cn.tb.entity.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CityService {
@Autowired
private CityDao cityDao;
public CityDao getCityDao() {
return cityDao;
}
public void setCityDao(CityDao cityDao) {
this.cityDao = cityDao;
}
public List<City> findCnameAll(){
return cityDao.findCnameAll();
}
public void update(City cid){
cityDao.update(cid);
}
}
(6)配置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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
">
<!--自动扫描上下文包 自动扫描cn.tb下的所有包-->
<context:component-scan base-package="cn.tb.*" />
<!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!--加载mysql驱动-->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<!--获取数据库连接 jqueryajaxdemo 数据库名-->
<property name="url" value="jdbc:mysql://localhost:3306/jqueryajaxdemo?serverTimezone=UTC" />
<!--获取数据库账号-->
<property name="username" value="root" />
<!--获取数据库密码-->
<property name="password" value="root" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<!--事务管理-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txadvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="services" expression="execution(* cn.tb.dao.impl.*.*(..))" />
<aop:advisor advice-ref="txadvice" pointcut-ref="services" />
</aop:config>
</beans>
(7)测试
package cn.tb.test;
import cn.tb.entity.City;
import cn.tb.service.CityService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Test {
public static void main(String[] args) {
ApplicationContext alc = new ClassPathXmlApplicationContext("Spring.xml");
CityService ct = alc.getBean("cityService", CityService.class);
List<City> list = ct.findCnameAll();
for (City c : list) {
System.out.println(c.getCname());
}
}
}
(8)测试结果