property name=password value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"123456"> /bean bean id=jdbcTemplate class。Spring框架的详细介绍( 六 )。" />

Spring框架的详细介绍( 六 )


<property name="username" value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"root">
<property name="password" value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"123456">
</bean>
<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 JdbcDemo2 {
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Test
public void demo2(){
jdbcTemplate.update("insert into account values (null,?,?)", "xiaolan",1000d);
}
}
使用开源数据库连接池
  1. 使用 DBCP 的配置:
<bean id="dataSource" class="org.Apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"com.mysql.jdbc.Driver">
<property name="url" value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"jdbc:mysql://192.168.66.128/spring4">
<property name="username" value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"root">
<property name="password" value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"123456">
  1. 使用 C3P0 的配置:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"com.mysql.jdbc.Driver">
<property name="jdbcUrl" value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"jdbc:mysql://192.168.66.128/spring4">
<property name="user" value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"root">
<property name="password" value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"123456">
</bean>
  1. 引入外部属性文件
首先建立外部属性文件:
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.66.128/spring4
jdbc.username=root
jdbc.password=123456
 
然后对属性文件进行配置:
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"${jdbc.driverClass}">
<property name="jdbcUrl" value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"${jdbc.url}">
<property name="user" value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"${jdbc.username}">
<property name="password" value=https://www.isolves.com/it/cxkf/kj/2020-03-03/"${jdbc.password}">
</bean>
CRUD操作insert, update, delete 语句都借助模板的 update 方法进行操作 。
public void demo(){
jdbcTemplate.update("insert into account values (null,?,?)", "xiaoda",1000d);
jdbcTemplate.update("update account set name=?,money=? where id=?", "xiaoda",1000d,2);
jdbcTemplate.update("delete from account where id=?", 6);
}
查询操作:
public void demo3(){
String name=jdbcTemplate.queryForObject("select name from account where id=?",String.class,5);
long count=jdbcTemplate.queryForObject("select count(*) from account",Long.class);
}
将返回的结果封装成为类:
public void demo4(){
Account account=jdbcTemplate.queryForObject("select * from account where id=?", new MyRowMapper(),5);
}
其中:
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;
}
}
Spring的事务管理事务事务是指逻辑上的一组操作,组成这组操作的各个单元,要么全部成功,要么全部失败 。
具有四个特性: