1.JDBC模板
Spring是EE开发的一站式的框架,有EE开发的每层的解决方案。
Spring对持久层也提供了解决方案:ORM模块和JDBC的模板。
Spring提供了很多的模板用于简化开发,如JDBC、Springmvc
2.JDBC模板入门
create table account(
id int primary key auto_increment ,
name varchar ( 20 ) ,
money double
) ;
public class SpringJdbcTest {
@Test
public void test ( ) {
DriverManagerDataSource source = new DriverManagerDataSource ( ) ;
source. setDriverClassName ( "com.mysql.jdbc.Driver" ) ;
source. setUrl ( "jdbc:mysql:///spring" ) ;
source. setUsername ( "root" ) ;
source. setPassword ( "123" ) ;
JdbcTemplate jdbcTemplate = new JdbcTemplate ( source) ;
jdbcTemplate. update ( "insert into account value(null,?,?)" , "zs" , 1000d ) ;
}
}
3.Spring管理JDBC
<?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: aop= " http://www.springframework.org/schema/aop"
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" >
< bean id = " source" class = " org.springframework.jdbc.datasource.DriverManagerDataSource" >
< property name = " driverClassName" value = " com.mysql.jdbc.Driver" />
< property name = " url" value = " jdbc:mysql:///spring" />
< property name = " username" value = " root" />
< property name = " password" value = " 123" />
</ bean>
< bean id = " jdbcTemplate" class = " org.springframework.jdbc.core.JdbcTemplate" >
< property name = " dataSource" ref = " source" />
</ bean>
</ beans>
@RunWith ( SpringJUnit4ClassRunner. class )
@ContextConfiguration ( "classpath:applicationContext.xml" )
public class SpringJdbcTest2 {
@Resource ( name = "jdbcTemplate" )
private JdbcTemplate jdbcTemplate;
@Test
public void test ( ) {
jdbcTemplate. update ( "insert into account value(null,?,?)" , "ls" , 10000d ) ;
}
}
4.使用第三方连接池(只需要更改配置文件就行)
<?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: aop= " http://www.springframework.org/schema/aop"
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" >
< bean id = " dbcp" class = " org.apache.commons.dbcp.BasicDataSource" >
< property name = " driverClassName" value = " com.mysql.jdbc.Driver" />
< property name = " url" value = " jdbc:mysql:///spring" />
< property name = " username" value = " root" />
< property name = " password" value = " 123" />
</ bean>
< bean id = " jdbcTemplate" class = " org.springframework.jdbc.core.JdbcTemplate" >
< property name = " dataSource" ref = " dbcp" />
</ bean>
</ beans>
<?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: aop= " http://www.springframework.org/schema/aop"
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" >
< bean id = " c3p0" class = " com.mchange.v2.c3p0.ComboPooledDataSource" >
< property name = " driverClass" value = " com.mysql.jdbc.Driver" />
< property name = " jdbcUrl" value = " jdbc:mysql:///spring" />
< property name = " user" value = " root" />
< property name = " password" value = " 123" />
</ bean>
< bean id = " jdbcTemplate" class = " org.springframework.jdbc.core.JdbcTemplate" >
< property name = " dataSource" ref = " c3p0" />
</ bean>
</ beans>
<?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: aop= " http://www.springframework.org/schema/aop"
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" >
< bean id = " druid" class = " com.alibaba.druid.pool.DruidDataSource" >
< property name = " driverClassName" value = " com.mysql.jdbc.Driver" />
< property name = " url" value = " jdbc:mysql:///spring" />
< property name = " username" value = " root" />
< property name = " password" value = " 123" />
</ bean>
< bean id = " jdbcTemplate" class = " org.springframework.jdbc.core.JdbcTemplate" >
< property name = " dataSource" ref = " c3p0" />
</ bean>
</ beans>
5.加载属性文件(bean方式)
driverClassName= com. mysql. jdbc. Driver
url= jdbc: mysql: /
username= root
password= 123
<?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: aop= " http://www.springframework.org/schema/aop"
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" >
< bean class = " org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
< property name = " location" value = " jdbc.properties" />
</ bean>
< bean id = " druid" class = " com.alibaba.druid.pool.DruidDataSource" >
< property name = " driverClassName" value = " ${driverClassName}" />
< property name = " url" value = " ${url}" />
< property name = " username" value = " ${username}" />
< property name = " password" value = " ${password}" />
</ bean>
< bean id = " jdbcTemplate" class = " org.springframework.jdbc.core.JdbcTemplate" >
< property name = " dataSource" ref = " druid" />
</ bean>
</ beans>
6.加载属性文件(context方式)
driverClassName= com. mysql. jdbc. Driver
url= jdbc: mysql: /
username= root
password= 123
<?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: aop= " http://www.springframework.org/schema/aop"
xmlns: context= " http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd" >
< context: property-placeholder location = " classpath:jdbc.properties" />
< bean id = " druid" class = " com.alibaba.druid.pool.DruidDataSource" >
< property name = " driverClassName" value = " ${jdbc.driverClassName}" />
< property name = " url" value = " ${jdbc.url}" />
< property name = " username" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ bean>
< bean id = " jdbcTemplate" class = " org.springframework.jdbc.core.JdbcTemplate" >
< property name = " dataSource" ref = " druid" />
</ bean>
</ beans>
7.JDBC模板的CRUD操作
@Test
public void test ( ) {
String name = jdbcTemplate. queryForObject ( "select name from account where id=?" , String. class , 1 ) ;
System. out. println ( name) ;
}
新建domain类
import lombok. Getter;
import lombok. Setter;
@Getter @Setter
public class Account {
private Integer id;
private String name;
private Double money;
@Override
public String toString ( ) {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}' ;
}
}
新建结果集处理器类
public class MyRowMap implements RowMapper < Account> {
@Override
public Account mapRow ( ResultSet resultSet, int i) throws SQLException {
Account account = new Account ( ) ;
account. setId ( resultSet. getInt ( "id" ) ) ;
account. setName ( resultSet. getString ( "name" ) ) ;
account. setMoney ( resultSet. getDouble ( "money" ) ) ;
return account;
}
}
运行测试类
@Test
public void test ( ) {
Account account = jdbcTemplate. queryForObject ( "select * from account where id=?" , new MyRowMap ( ) , 1 ) ;
System. out. println ( account) ;
}
domain类、结果集处理类不变
测试类
@Test
public void test ( ) {
List< Account> query = jdbcTemplate. query ( "select * from account" , new MyRowMap ( ) ) ;
for ( Account account: query) {
System. out. println ( account) ;
}
}