Spring的JDBC模板

使用开源的数据库连接池

配置都写在applicationContext.xml中

  • DBCP的使用
    1. 引入jar包
    2. 配置DBCP连接池
<!-- 配置DBCP连接池=============================== -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql:///spring4_day03"/>
		<property name="username" value="root"/>
		<property name="password" value="abc"/>
	</bean>
	<!-- 配置Spring的JDBC的模板========================= -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>
  • C3P0的使用
    1. 引入C3P0的jar包
    2. 配置C3P0连接池
<!-- 配置C3P0连接池=============================== -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"/>
		<property name="jdbcUrl" value="jdbc:mysql:///spring4_day03"/>
		<property name="user" value="root"/>
		<property name="password" value="abc"/>
	</bean>
	
	<!-- 配置Spring的JDBC的模板========================= -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>

抽取到属性文件

  • 定义一个属性文件
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring4_day03
jdbc.username=root
jdbc.password=abc
  • 在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"/>
  • 引入属性文件的值
<!-- 配置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>

使用JDBC的模板完成CRUD的操作

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcDemo2 {
	@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 = ?", "何巨涛",2000d,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>{

		@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;
		}
		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值