<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>
- 测试文件:
@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);
}
}
使用开源数据库连接池
- 使用 DBCP 的配置:
<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">
- 使用 C3P0 的配置:
<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>
- 引入外部属性文件
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的事务管理事务事务是指逻辑上的一组操作,组成这组操作的各个单元,要么全部成功,要么全部失败 。
具有四个特性:
- 原子性:事务不可分
- 一致性:事务执行前后数据完整性保持一致
- 隔离性:一个事务的执行不应该受到其他事务干扰
- 持久性:一旦事务结束,数据就持久化到数据库
推荐阅读
- 抖音客服的工作靠谱么 抖音售后客服是干嘛的
- 淘宝店铺的规则主要有哪一些 开淘宝店的规则是什么
- 淘宝不按时发货的惩罚 淘宝店铺未按照约定时间发货有赔偿吗
- 推荐几个开发必备的JSON工具
- 福鼎大白茶,福鼎大白茶的优势
- 福鼎大毫茶,福鼎大白与福鼎大毫茶树的生长条件介绍
- 提高搜索引擎对网站信用度的方法
- 升级win10系统必须要做的5件事,装机、重装系统必知
- 与程序员相关的CPU缓存知识
- 福建绿茶,福建的绿茶种类