Spring中使用JdbcTemplate
软件152班杨锦宏
Spring中使用JdbcTemplate、JdbcDaoSupport和NamedParameterJdbcTemplate来操作数据库,但是JdbcTemplate最常用,最易用。
jdbc.properties:
user=root password=123 driverClass=com.mysql.jdbc.Driver jdbcUrl=jdbc\:mysql\:///spring?encoding\=UFT-8 initPoolSize=5 maxPoolSize=20
ApplicationContext.xml中导入配置文件和配置dataSouce:
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="initialPoolSize" value="${initPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
</bean>
test0410.java(属性和数据库中的字段对应):
package spring.jdbc;
public class test0410 {
private Integer uuid;
private String name;
private Integer age;
public Integer getUuid() {
return uuid;
}
public void setUuid(Integer uuid) {
this.uuid = uuid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "test0410 [uuid=" + uuid + ", name=" + name + ", age=" + age + "]";
}
}
test0410Dao.java:
package spring.jdbc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
@Repository
public class test0410Dao {
@Autowired
private JdbcTemplate jdbcTemplate;
public test0410 get(Integer id){
String sql="select id uuid,name,age from test0410 where id=?";
RowMapper<test0410> rowMapper=new BeanPropertyRowMapper<test0410>(test0410.class);
test0410 test10410=jdbcTemplate.queryForObject(sql,rowMapper,1);
return test10410;
}
}
ApplicationContext.xml中配置自动扫描和JdbcTemplate:
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="spring.jdbc"></context:component-scan>
<!-- 配置spring 的JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
测试类:
package spring.jdbc.test;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import spring.jdbc.test0410;
import spring.jdbc.test0410Dao;
public class jdbcTest {
private ApplicationContext ctx=null;
private JdbcTemplate jdbcTemplate;
{
ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
jdbcTemplate= (JdbcTemplate) ctx.getBean("jdbcTemplate");
}
@Test
public void testDao(){
test0410Dao test04101=(test0410Dao) ctx.getBean("test0410Dao");
System.out.println(test04101.get(1));
}
/**
* 获取单个列的值 或做统计查询
* 使用queryForObject(String sql, Class<Long> requiredType)
*/
@Test
public void testQueryForObject2(){
String sql="select count(id) from test0410";
long count=jdbcTemplate.queryForObject(sql, Long.class);
System.out.println(count);
}
/**
* 查到实体类的集合
* 注意调用的不是queryForList方法
*/
@Test
public void testQueryForList(){
String sql="select id uuid,name,age from test0410 where id>?";
RowMapper<test0410> teMapper=new BeanPropertyRowMapper<test0410>(test0410.class);
List<test0410> test0410s=jdbcTemplate.query(sql, teMapper,2);
System.out.println(test0410s);
}
/**
* 从数据库中获取一条记录,实际得到一个对象
* 注意:不是调用queryForObject(String sql, Class<test0410> requiredType, Object... args)方法
* 而需要调用queryForObject(String sql, RowMapper<test0410> rowMapper, Object... args)
* 其中的rowMapper指定如何去映射结果集 的行,常用的实现类为BeanPropertyRowMapper
*
* 使用Sql中的列的别名和类的属性名映射,例如:id uuid
*
* 不支持级联属性
*/
@Test
public void testQueryForObject(){
String sql="select id uuid,name,age from test0410 where id=?";
RowMapper<test0410> rowMapper=new BeanPropertyRowMapper<test0410>(test0410.class);
test0410 test10410=jdbcTemplate.queryForObject(sql,rowMapper,1);
System.out.println(test10410);
}
/**
* 执行批量更新:update ,insert ,delete
* 最后一个参数是一个Object[]的List集合。
*/
@Test
public void testBatchUpdate(){
String sql="insert into test0410(id,name,age) values (?,?,?)";
List<Object[]> batchArgs=new ArrayList<Object[]>();
batchArgs.add(new Object[]{2,"aaa",23});
batchArgs.add(new Object[]{3,"bbb",24});
batchArgs.add(new Object[]{4,"ccc",25});
jdbcTemplate.batchUpdate(sql, batchArgs);
}
/**
* 执行update ,insert ,delete
*/
@Test
public void testUptate(){
String sql="update test0410 set name=? where id=? ";
jdbcTemplate.update(sql,"lyj",1);
}
@Test
public void TestDateSource() throws SQLException{
DataSource dataSource= (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource.getConnection());
}
}