Spring对JDBC访问数据库进行了封装,建立了JDBCTemplate,而且对早期版本的JDBCTemplate进行了进一步优化完善,现在的这个封装非常简单,和MyBatis原理基本一致,且更加容易调试。
下面举个例子来说明JDBCTemplate的具体使用。
pom.xml添加依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.25</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
核心是spring-jdbc这一节
applicationContext.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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byName" default-lazy-init="true">
<context:annotation-config />
<context:component-scan base-package="com.critc"></context:component-scan>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- 注入jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
这一节比较简单,就是注入JdbcTemplate
Staff.java
public class Staff {
private int id;
private String name;//
//set get忽略
}
简单实体
StaffDao.java
@Repository
public class StaffDao {
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* 新增
*
* @param staff
*/
public void add(Staff staff) {
String sql = "insert into staff(name) values(:name)";
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(staff);
namedParameterJdbcTemplate.update(sql, paramSource);
}
/**
* 修改
*
* @param staff
*/
public void update(Staff staff) {
String sql = "update staff set name=:name where id=:id";
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(staff);
namedParameterJdbcTemplate.update(sql, paramSource);
}
public void delete(int id) {
String sql = "delete from staff where id=?";
Object[] params = new Object[]{id};
jdbcTemplate.update(sql, params);
}
/**
* 列表
*
* @return
*/
public List<Staff> list() {
return jdbcTemplate.query("select * from staff", new BeanPropertyRowMapper<>(Staff.class));
}
/**
* 根据id获取对象
*
* @param id
* @return
*/
public Staff get(int id) {
String sql = "select * from staff where id=?";
Object[] params = new Object[]{id};
List<Staff> list = jdbcTemplate.query(sql, params, new BeanPropertyRowMapper<>(Staff.class));
if (list.size() > 0)
return list.get(0);
else
return null;
}
}
这是增删改查的例子,JDBCTemplate的使用有两种方式,一个是直接用JDBCTemplate,参数使用Object[]数组传入进去,sql里面利用?作为占位符,这个适合参数比较少的情况,比如删除、按id查找,一般就一两个参数,比较合适。
第二种方式是新建一个NamedJDBCTemplate,传入一个VO对象,sql里面的参数采用:Id这种方式,适合新增、修改这种参数较多,和参数不固定,比如动态查询sql这种情况。
TestDao.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:applicationContext.xml"})
public class TestDao {
@Autowired
private StaffDao staffDao;
@Test
public void testAdd() {
Staff staff = new Staff();
staff.setName("JDBCTemplate");
staffDao.add(staff);
}
@Test
@Rollback(false)
public void testList() {
System.out.println(staffDao.list().size());
}
}
这是一个简单的测试小例子。
JDBCTemplate相对于Hibernate和MyBatis来讲,非常简单,涉及的接口非常容易掌握,而且是原生sql,如果sql出错,定位很准。推荐使用,尤其是新手。
网友评论