spring 提供用于操作JDBC工具类,类似:DBUtils。
依赖 连接池DataSource (数据源)
Spring 提供了一个强有力的模板类JdbcTemplate简化JDBC操作,DataSource,JdbcTemplate都可以以Bean的方式定义在想xml配置文件,JdbcTemplate创建只需注入一个DataSource。
应用程序Dao层只需要继承JdbcDaoSupport, 或者注入JdbcTemplate,便可以获取JdbcTemplate,JdbcTemplate是一个线程安全的类,多个Dao可以
启动mysql
使用sql语句创建数据库和数据表
create database spring_day02;
use spring_day02;
create table t_user(
id int primary key auto_increment,
username varchar(50),
password varchar(32)
);
不使用Ioc操作数据库
步骤:
1 创建User类
2 创建测试类**
3 在测试类中创建数据源BasicDataSource
4 加载驱动
dataSource.setDriverClassName(“com.mysql.jdbc.Driver”);
5 连接数据库
dataSource.setUrl(“jdbc:mysql://localhost:3306/spring_day02”);
6 设置用户名密码
dataSource.setUsername(“root”);
dataSource.setPassword("");
7 创建模板JdbcTemplate对象加载数据源
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
8 操作数据库
jdbcTemplate.update(“insert into user(username,password) values(?,?)”, “tom”,“998”);
使用Ioc和DI
spring 4.x提供了
BeanPropertyRowMapper,如果PO和数据库模型的字段完全对应(字段名字一样或者驼峰式与下划线式对应),如果使用JdbcTemplate则可以使用这个RowMapper作为PO和数据库的映射
步骤:
1 创建UserDao 依赖注入JdbcTemplate属性
2 创建update方法,用来更新User数据
String sql = “update t_user set username=?,password=? where id =?”;
//将传进来的user对象属性值取出来,并存入数组中
Object[] args = {user.getUsername(),user.getPassword(),user.getId()};
jdbcTemplate.update(sql, args);
3 spring配置文件中
< !-- 创建数据源 –>
< bean id=“dataSourceId” class=“org.apache.commons.dbcp.BasicDataSource”>
< property name=“driverClassName” value=“com.mysql.jdbc.Driver”>< /property>
< property name=“url” value=“jdbc:mysql://localhost:3306/spring_day02?useUnicode=true&characterEncoding=UTF-8”>< /property>
< property name=“username” value=“root”>< /property>
< property name=“password” value="">< /property>
< /bean>
< !-- 创建模板 ,需要注入数据源–>
< bean id=“jdbcTemplateId” class=“org.springframework.jdbc.core.JdbcTemplate”>
< property name=“dataSource” ref=“dataSourceId”>< /property>
< /bean>
< !-- 配置dao 依赖注入jdbcTemplate -->
< bean id=“userDaoId” class=“com.c_dbcp.UserDao”>
< property name=“jdbcTemplate” ref=“jdbcTemplateId”>< /property>
< /bean>
4 测试类中获取UserDao对象执行update方法
查询一个学生信息
public List selectOne(Integer id){
return jdbcTemplate.queryForList
(“select * from user where id = ?”, id);
}
public List selectAll(){
String sql = “select * from user”;
List list = jdbcTemplate.queryForList(sql);
return list;
}
JdbcTemplate+C3P0的使用
Spring配置文件
< !-- 创建数据源 c3p0–>
< bean id=“dataSourceId” class=“com.mchange.v2.c3p0.ComboPooledDataSource”>
< property name=“driverClass” value=“com.mysql.jdbc.Driver”>< /property>
< property name=“jdbcUrl” value=“jdbc:mysql://localhost:3306/spring_day02?useUnicode=true&characterEncoding=UTF-8”>< /property>
< property name=“user” value=“root”>< /property>
< property name=“password” value="">< /property>
< /bean>
< !-- 创建模板 ,需要注入数据源–>
< bean id=“jdbcTemplateId” class=“org.springframework.jdbc.core.JdbcTemplate”>
< property name=“dataSource” ref=“dataSourceId”>< /property>
< /bean>
< !-- 配置dao -->
< bean id=“userDaoId” class=“com.d_c3p0.UserDao”>
< property name=“jdbcTemplate” ref=“jdbcTemplateId”>< /property>
< /bean>
Dao层继承 JdbcDaoSupport自动获取模板
步骤:
1 创建UserDao,并继承JdbcDaoSupport
2 使用this.getJdbcTemplate()获取模板对象
3 创建查询所有学生信息的方法
public List findAll() {
return this.getJdbcTemplate().query(“select * from user”, BeanPropertyRowMapper.newInstance(User.class));
}
4 spring配置文件
< !-- 创建数据源 c3p0–>
< bean id=“dataSourceId” class=“com.mchange.v2.c3p0.ComboPooledDataSource”>
< property name=“driverClass” value=“com.mysql.jdbc.Driver”>< /property>
< property name=“jdbcUrl” value=“jdbc:mysql://localhost:3306/spring_day02”>< /property>
< property name=“user” value=“root”>< /property>
< property name=“password” value="">< /property>
< /bean>
< !-- 配置dao * dao 继承 JdbcDaoSupport,之后只需要注入数据源,底层将自动创建模板–>
< bean id=“userDaoId” class=“com.e_jdbcdaosupport.UserDao”>
< property name=“dataSource” ref=“dataSourceId”>< /property>
< /bean>
5 测试类
使用properties属性文件优化
创建jdbc.properties属性文件
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/spring_day02?useUnicode=true&characterEncoding=UTF-8
jdbc.user=root
jdbc.password=
spring配置文件
< context:property-placeholder location=“classpath:com /f_properties/jdbcInfo.properties”/>
< !-- 创建数据源 c3p0–>
< bean id=“dataSourceId” class=“com.mchange.v2.c3p0.ComboPooledDataSource”>
< property name=“driverClass” value="
j
d
b
c
.
d
r
i
v
e
r
C
l
a
s
s
"
>
<
/
p
r
o
p
e
r
t
y
>
<
p
r
o
p
e
r
t
y
n
a
m
e
=
"
j
d
b
c
U
r
l
"
v
a
l
u
e
=
"
{jdbc.driverClass}">< /property> < property name="jdbcUrl" value="
jdbc.driverClass"></property><propertyname="jdbcUrl"value="{jdbc.jdbcUrl}">< /property>
< property name=“user” value="
j
d
b
c
.
u
s
e
r
"
>
<
/
p
r
o
p
e
r
t
y
>
<
p
r
o
p
e
r
t
y
n
a
m
e
=
"
p
a
s
s
w
o
r
d
"
v
a
l
u
e
=
"
{jdbc.user}">< /property> < property name="password" value="
jdbc.user"></property><propertyname="password"value="{jdbc.password}">< /property>
< /bean>
< bean id=“userDaoId” class=“com.f_properties.UserDao”>
< property name=“dataSource” ref=“dataSourceId”>< /property>
< /bean>
在Dao层中,需要try catch,否则查询无结果会抛异常 ,抛EmptyResultDataAccessException异常
public User getById(int id) {
User object = null;
try {
object = this.getJdbcTemplate().queryForObject(“select * from user where id = ?”, BeanPropertyRowMapper.newInstance(User.class), id);
} catch (EmptyResultDataAccessException e) {
return null;
}
return object;