该篇博客主要阐述关于Spring中jdbcTemplate,jdbcTemplate大大方便了jdbc的一些重复性操作
其中就包含了该篇博客要阐述的jdbcTemplate
Spring将数据访问过程中固定的和可变的部分明确划分为两个不同的类
Spring对数据库的操作在jdbc上面做了深层次的封装,使用Spring的注入功能,可以把DataSource(数据库连接池)注册到jdbcTemplate中
主要是不要忘了c3p0连接池的jar和数据库的jar
基本信息配置
- jdbc.user=root
- jdbc.password=1111
- jdbc.driverClass=com.mysql.jdbc.Driver
- jdbc.jdbcUrl=jdbc:mysql:///sms
其他信息配置
initialPoolSize:初始化连接数量
minPoolSize:最小连接数量
maxPoolSize:最大连接数量
acquireIncrement: 当连接池用完之后一次性获取的连接数量
idleConnectionTestPeriod:根据一定的时间间隔检查连接池的连接数量 单位为秒
maxIdleTime:最大空闲时间 单位为秒
maxStatements:最大的maxStatements连接数量
maxStatementsPerConnection:最大语句缓存
db.properties
jdbc.user=root
jdbc.password=1111
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///sms
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
package com.linjie.jdbctemplate;
/**
* @author LinJie E-mail:[email protected]
* @version 创建时间:2018年5月11日 下午4:42:55
* 测试
*/
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class testJDBC {
private JdbcTemplate jdbcTemplate;
private ApplicationContext context = null;
//初始化连接池
{
context = new ClassPathXmlApplicationContext("applicationContext.xml");
jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");
}
//测试是否连接数据库
@Test
public void testIsConnect() throws SQLException {
DataSource dataSource = context.getBean(DataSource.class);
System.out.println("连接成功"+dataSource.getConnection());
}
}
结果(连接成功)
既然已经可以连接了,那就进行一些常规操作吧
+----+------+----------+--------+------+-------+
| id | num | password | name | age | score |
+----+------+----------+--------+------+-------+
| 1 | 0001 | 111 | pink | 21 | 99 |
| 2 | 11 | 11 | ee | 12 | 55 |
| 3 | 0004 | 123 | linjie | 20 | 99 |
| 4 | 11 | 11 | ww | 11 | 11 |
| 5 | 007 | 123 | pojp | 23 | 99 |
| 6 | 008 | 123 | trtp | 23 | 99 |
| 7 | 009 | 123 | QQQp | 23 | 99 |
| 8 | 007 | 123 | pojp | 23 | 99 |
| 9 | 008 | 123 | trtp | 23 | 99 |
| 10 | 009 | 123 | QQQp | 23 | 99 |
| 11 | 007 | 123 | pojp | 23 | 99 |
| 12 | 008 | 123 | trtp | 23 | 99 |
| 13 | 009 | 123 | QQQp | 23 | 99 |
+----+------+----------+--------+------+-------+
//修改指定num的name
@Test
public void testUpdate() {
String sql = "update student set name = ? where num = ?";
jdbcTemplate.update(sql, "pink",0001);
System.out.println("success");
}
/*
*批量更新:批量 insert,update,delete
*/
@Test
public void testBatechUpdate() {
String sql = "insert into student(num,password,name,age,score) value(?,?,?,?,?)";
List
package com.linjie.jdbctemplate;
/**
* @author LinJie E-mail:[email protected]
* @version 创建时间:2018年5月10日 下午3:34:59
*/
public class stu {
private String num;
private String name;
private Integer age;
/**
* @return the num
*/
public String getNum() {
return num;
}
/**
* @param num the num to set
*/
public void setNum(String num) {
this.num = num;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public Integer getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(Integer age) {
this.age = age;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "stu [num=" + num + ", name=" + name + ", age=" + age + "]";
}
}
//初始化连接池代码块
{
context = new ClassPathXmlApplicationContext("applicationContext.xml");
jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");
studao = (stuDao) context.getBean("stuDao");
}
/*
*从数据库中获取一条记录,实际是得到对应的一个对象
*RowMapper:行的映射
*Spring 2.5 提供了一个便利的RowMapper实现-----BeanPropertyRowMapper
*它可自动将一行数据映射到指定类的实例中 它首先将这个类实例化,然后通过名称匹配的方式,映射到属性中去。
*字段 bean属性
*USER_NAME --> userName
*USER_ID --> userId
*/
@Test
public void testQueryForObject() {
String sql = "select num,name,age from student where id = ?";
RowMapper rowMapper = new BeanPropertyRowMapper(stu.class);
stu s = jdbcTemplate.queryForObject(sql, rowMapper,5);//最后一个参数为id值
System.out.println(s);
}
/*
*查询实体类的集合
*/
@Test
public void testQueryForList() {
String sql = "select num,name,age from student where id > ?";
RowMapper rowMapper = new BeanPropertyRowMapper(stu.class);
List s = jdbcTemplate.query(sql, rowMapper,0);//最后一个参数为id值
System.out.println(s);
}
/**
* 获取单个列的值,或统计
*/
@Test
public void testQueryObjectqqq() {
String sql = "select count(name) from student";
Long count = jdbcTemplate.queryForObject(sql, Long.class);
System.out.println(count);
}
stuDao.java(注意:stu类引用上文的)
package com.linjie.jdbctemplate;
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;
/**
* @author LinJie E-mail:[email protected]
* @version 创建时间:2018年5月10日 下午7:24:51
*/
@Repository("stuDao")
public class stuDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public stu get(Integer id) {
String sql = "select num,name,age from student where id = ?";
RowMapper rowMapper = new BeanPropertyRowMapper(stu.class);
stu s = jdbcTemplate.queryForObject(sql, rowMapper,id);
return s;
}
}
db.properties
jdbc.user=root
jdbc.password=1111
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///sms
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
applicationContext.xml
test测试类
package com.linjie.jdbctemplate;
/**
* @author LinJie E-mail:[email protected]
* @version 创建时间:2018年5月10日 上午8:49:38
* 测试
*/
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class JDBCTest {
private ApplicationContext context= null;
private JdbcTemplate jdbcTemplate;
private stuDao studao;
//初始化连接池代码块
{
context = new ClassPathXmlApplicationContext("applicationContext.xml");
jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");
studao = (stuDao) context.getBean("stuDao");
}
@Test
public void testsss() {
System.out.println(studao.get(1));
}
}
结果
《Spring IN ACTION》
数据库连接池作用及c3p0详解