“Spring:JdbcTemplate、事务”的版本间差异
跳到导航
跳到搜索
(→使用属性文件) |
|||
第124行: | 第124行: | ||
== jdbcTemplate == | == jdbcTemplate == | ||
JDBC 模板CRUD 的操作: | |||
* JdbcTemplate类使用“org.springframework.jdbc.core.'''RowMapper<T>'''”接口进行结果映射:将结果集ResultSet的行,映射到实际的结果对象。 | |||
<syntaxhighlight lang="java" highlight="5-6,11,26,32,38,41-45"> | |||
@RunWith(SpringJUnit4ClassRunner.class) | |||
@ContextConfiguration("classpath:applicationContext.xml") | |||
public class SpringDemo3 { | |||
= | @Resource(name="jdbcTemplate") | ||
private JdbcTemplate jdbcTemplate; | |||
=== | @Test | ||
// 插入操作 | |||
public void demo1() { | |||
jdbcTemplate.update("insert into account values (null,?,?)", "冠希",10000d); | |||
} | |||
@Test | |||
// 修改操作 | |||
public void demo2() { | |||
jdbcTemplate.update("update account set name=?,money =? where id = ?", "思雨",10000d,5); | |||
} | |||
@Test | |||
// 删除操作 | |||
public void demo3() { | |||
jdbcTemplate.update("delete from account where id = ?", 5); | |||
} | |||
@Test | |||
// 查询一条记录 | |||
public void demo4() { | |||
Account account = jdbcTemplate.queryForObject("select * from account where id = ?", new MyRowMapper(), 1); | |||
System.out.println(account); | |||
} | |||
@Test | |||
// 查询所有记录 | |||
public void demo5() { | |||
List<Account> list = jdbcTemplate.query("select * from account", new MyRowMapper()); | |||
for (Account account : list) { | |||
System.out.println(account); | |||
} | |||
} | |||
class MyRowMapper implements RowMapper<Account> { | |||
@Override | |||
public Account mapRow(ResultSet rs, int rowNum) throws SQLException { | |||
Account account = new Account(); | |||
account.setId(rs.getInt("id")); | |||
account.setName(rs.getString("name")); | |||
account.setMoney(rs.getDouble("money")); | |||
return account; | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
# 注入了 jdbcTemplate; | |||
# 使用 jdbcTemplate进入db操作; | |||
# 使用 RowMapper进行结果集映射; | |||
== 事务 == | == 事务 == |
2020年10月31日 (六) 19:17的版本
关于 =
spring对不同的持久化层技术都进行了封装:
jdbcTemplate 对JDBC进行了封装;
jar包
- 数据库驱动包;
- JDBC 包;
- 事务包
JDBC 模板的基本使用
@Test
public void demo1(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///spring_day03");
dataSource.setUsername("root");
dataSource.setPassword("123");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.update("insert into account values (null,?,?)", " 会希",10000d);
}
连接池
数据库连接池(Connection pooling):程序启动时建立足够的数据库连接,并将这些连接组成一个连接池,由程序动态地对池中的连接进行申请、使用、释放。
连接池的机制:
- 程序初始化时创建连接池;
- 使用时向连接池申请可用连接;
- 使用完毕,将连接返还给连接池;
- 程序退出时,断开所有连接,并释放资源;
其中:
- 最小连接数:连接池一直保持的数据库连接;
- 最大连接数:连接池能申请的最大连接数:
- 如果数据库连接请求超过最大次数,后到的请求将被加入等待队列;
Spring 内置连接池
“org.springframework.jdbc.datasource.DriverManagerDataSource”:
- 配置内置连接池:
<!-- 配置Spring 的内置连接池--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///spring_day02"/> <property name="username" value="root"/> <property name="password" value="123"/> </bean>
- 配置jdbcTemplate模板:
<!-- 配置JDBC 模板--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean>
- 编写测试类:
- 注入 jdbcTemplate;
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class SpringDemo2 { @Resource(name="jdbcTemplate") private JdbcTemplate jdbcTemplate; @Test public void demo1(){ jdbcTemplate.update("insert into account values (null,?,?)", "eijux",10000d); } }
DBCP 连接池
- 引入dbcp 连接池的jar 包:
- 配置连接池:
<!-- 配置DBCP 连接池--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///spring_day02"/> <property name="username" value="root"/> <property name="password" value="123"/> </bean>
c3p0 连接池
- 引入相应的jar 包:
- 配置连接池:
<!-- 配置C3P0 连接池--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql:///spring_day02"/> <property name="user" value="root"/> <property name="password" value="123"/> </bean>
使用属性文件
定义属性文件“jdbc.properties”:
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring_day02
jdbc.username=root
jdbc.password=123
引入外部的属性文件:(两种方式)
- 第一种方式:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"/> </bean>
- 第二种方式:
<context:property-placeholder location="classpath:jdbc.properties"/>
jdbcTemplate
JDBC 模板CRUD 的操作:
- JdbcTemplate类使用“org.springframework.jdbc.core.RowMapper<T>”接口进行结果映射:将结果集ResultSet的行,映射到实际的结果对象。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDemo3 {
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Test
// 插入操作
public void demo1() {
jdbcTemplate.update("insert into account values (null,?,?)", "冠希",10000d);
}
@Test
// 修改操作
public void demo2() {
jdbcTemplate.update("update account set name=?,money =? where id = ?", "思雨",10000d,5);
}
@Test
// 删除操作
public void demo3() {
jdbcTemplate.update("delete from account where id = ?", 5);
}
@Test
// 查询一条记录
public void demo4() {
Account account = jdbcTemplate.queryForObject("select * from account where id = ?", new MyRowMapper(), 1);
System.out.println(account);
}
@Test
// 查询所有记录
public void demo5() {
List<Account> list = jdbcTemplate.query("select * from account", new MyRowMapper());
for (Account account : list) {
System.out.println(account);
}
}
class MyRowMapper implements RowMapper<Account> {
@Override
public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
Account account = new Account();
account.setId(rs.getInt("id"));
account.setName(rs.getString("name"));
account.setMoney(rs.getDouble("money"));
return account;
}
}
}
- 注入了 jdbcTemplate;
- 使用 jdbcTemplate进入db操作;
- 使用 RowMapper进行结果集映射;