Spring的JDBC的模板的使用
Spring的JDBC的模板:
Spring是EE开发的一站式的框架,有EE开发的每层的解决方案。Spring对持久层也提供了解决方案:ORM模块和JDBC的模板。
Spring提供了很多的模板用于简化开发:
JDBC模板使用的入门
创建项目引入jar包
引入基本开发包:
数据库驱动
Spring的JDBC模板的jar包
创建数据库和表
create database springjdbc;
use springjdbc;
create table account(
id int primary key auto_increment,
name varchar(20),
money double
);
使用JDBC的模板保存数据
public class JdbcDemo1 {
public void demo1(){
//创建连接池
DriverManagerDataSource dataSource =new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///springjdbc");
dataSource.setUsername("root");
dataSource.setPassword("root");
//创建jdbc模板
JdbcTemplate jdbcTemplate =new JdbcTemplate(dataSource);
jdbcTemplate.update("insert into account values (null,?,?)", "liyao",10000d);
}
public static void main(String[] args) {
JdbcDemo1 jdbcDemo1 = new JdbcDemo1();
jdbcDemo1.demo1();
}
}
将连接池和模板交给Spring管理
Spring的配置文件
<!--配置Spring的内置连接池-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--属性注入-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///springjdbc"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!--配置Spring的JDBC的模板-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class demo1Test {
@Resource(name = "jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Test
public void demo2(){
jdbcTemplate.update("insert into account values (null,?,?)","not","222");
}
}
使用开源的数据库连接池:
DBCP的使用
引入jar包
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.7.0</version>
</dependency>
配置DBCP连接池
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///springjdbc"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!--配置Spring的JDBC的模板-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
配置完成直接执行上方测试类
C3P0的使用
引入jar包
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
配置C3P0连接池
注意:C3P0属性name名称与前面两个不一致,有改动
<!--配置C3P0连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///springjdbc"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!--配置Spring的JDBC的模板-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
配置完成直接执行上方测试类
抽取配置到属性文件
定义一个属性文件
在spring的配置文件中引入属性文件
第一种
<!--第一种方式通过bean标签引入的(很少)-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="Location" value="classpath:jdbc.properties"/>
</bean>
第二种
<!--第二种方式通过context标签引入-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
引入属性文件后,需要修改连接池配置改成动态获取
<!--配置C3P0连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
使用Spring的JDBC模板查询相关
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class demo1Test {
@Resource(name = "jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Test
/**
* 保存操作
*/
public void demo1(){
jdbcTemplate.update("insert into account values (null,?,?)","李四","222");
}
/**
* 修改操作
*/
@Test
public void demo2(){
jdbcTemplate.update("update account set name = ? ,money = ? where id = ?", "是否",222,6);
}
// 删除操作
@Test
public void demo3(){
jdbcTemplate.update("delete from account where id = ?", 6);
}
@Test
// 查询操作:
public void demo4(){
String name = jdbcTemplate.queryForObject("select name from account where id = ?", String.class, 5);
System.out.println(name);
}
@Test
// 统计查询
public void demo5(){
Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
System.out.println(count);
}
@Test
// 封装到一个对象中
public void demo6(){
Account account = jdbcTemplate.queryForObject("select * from account where id = ?", new MyRowMapper(), 5);
System.out.println(account);
}
@Test
// 查询多条记录
public void demo7(){
List<Account> list = jdbcTemplate.query("select * from account", new MyRowMapper());
for (Account account : list) {
System.out.println(account);
}
}
class MyRowMapper implements RowMapper<Account> {
public Account mapRow(ResultSet rs, int i) throws SQLException {
Account account =new Account();
account.setId(rs.getInt("id"));
account.setName(rs.getString("name"));
account.setMoney(rs.getDouble("money"));
return account;
}
}
}

浙公网安备 33010602011771号