Spring-整合JDBC

整合JDBC

一、spring提供了很多模板整合Dao技术


二、spring中提供了一个可以操作数据库的对象,对象封装了jdbc技术

1、JDBCTemplate --> JDBC模板对象(与DBUtils中的QueryRunner非常相似)

@Test
public void fun1() throws Exception{
	//0 准备连接池
	ComboPooledDataSource dataSource = new ComboPooledDataSource();
	dataSource.setDriverClass("com.mysql.jdbc.Driver");
	dataSource.setJdbcUrl("jdbc:mysql:///hibernate_32");
	dataSource.setUser("root");
	dataSource.setPassword("1234");
	//1 创建JDBC模板对象
	JdbcTemplate jt = new JdbcTemplate();
	jt.setDataSource(dataSource);
	//2 书写sql,并执行
	String sql = "insert into t_user values(null,'rose') ";
	jt.update(sql);
}

三、步骤

1、导包

4+2、spring-test、spring-aop、junit4类库
c3p0连接池、JDBC驱动、spring-jdbc、spring-tx事务

2、准备数据库

3、写Dao

3.1 增删改

@Override
public void save(User u) {
	String sql = "insert into t_user values(null,?) ";
	super.getJdbcTemplate().update(sql, u.getName());
}
@Override
public void delete(Integer id) {
	String sql = "delete from t_user where id = ? ";
	super.getJdbcTemplate().update(sql,id);
}
@Override
public void update(User u) {
	String sql = "update  t_user set name = ? where id=? ";
	super.getJdbcTemplate().update(sql, u.getName(),u.getId());
}

3.2 查询单个对象

@Override
public User getById(Integer id) {
	String sql = "select * from t_user where id = ? ";
	return super.getJdbcTemplate().queryForObject(sql,new RowMapper<User>(){
	@Override
	public User mapRow(ResultSet rs, int arg1) throws SQLException {
		User u = new User();
		u.setId(rs.getInt("id"));
		u.setName(rs.getString("name"));
		return u;
	}}, id);
}

3.3 查询值类型

@Override
public int getTotalCount() {
	String sql = "select count(*) from t_user  ";
	Integer count = super.getJdbcTemplate().queryForObject(sql, Integer.class);
	return count;
}

3.4 查询list集合类型

@Override
public List<User> getAll() {
	String sql = "select * from t_user  ";
	List<User> list = super.getJdbcTemplate().query(sql, new RowMapper<User>(){
		@Override
	    public User mapRow(ResultSet rs, int arg1) throws SQLException {
		User u = new User();
		u.setId(rs.getInt("id"));
		u.setName(rs.getString("name"));
		return u;
	    }});
	return list;
}

4、spring配置

4.1 依赖关系

进阶内容:

1、JDBCDaoSupport

public class UserDaoImpl extends JdbcDaoSupport implements UserDao 

<!-- 3.将UserDao放入spring容器 -->
<bean name="userDao" class="cn.shop.a_jdbctemplate.UserDaoImpl" >
	<!-- <property name="jt" ref="jdbcTemplate" ></property> -->
	<property name="dataSource" ref="dataSource" ></property>
</bean>


2、读取外部的Properties配置(注意加前缀)

db.properties:

jdbc.jdbcUrl=jdbc:mysql:///hibernate_32
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=1234

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">

<!-- 指定spring读取db.properties配置 -->
<context:property-placeholder location="classpath:db.properties"  />

<!-- 1.将连接池放入spring容器 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
	<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
	<property name="driverClass" value="${jdbc.driverClass}" ></property>
	<property name="user" value="${jdbc.user}" ></property>
	<property name="password" value="${jdbc.password}" ></property>
</bean>


<!-- 2.将JDBCTemplate放入spring容器 -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
	<property name="dataSource" ref="dataSource" ></property>
</bean>

<!-- 3.将UserDao放入spring容器 -->
<bean name="userDao" class="cn.shop.a_jdbctemplate.UserDaoImpl" >
	<!-- <property name="jt" ref="jdbcTemplate" ></property> -->
	<property name="dataSource" ref="dataSource" ></property>
</bean>
	
</beans>

5、测试

//演示JDBC模板
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
	@Resource(name="userDao")
	private UserDao ud;
	
	@Test
	public void fun1() throws Exception{
		//0 准备连接池
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setDriverClass("com.mysql.jdbc.Driver");
		dataSource.setJdbcUrl("jdbc:mysql:///hibernate");
		dataSource.setUser("root");
		dataSource.setPassword("1234");
		//1 创建JDBC模板对象
		JdbcTemplate jt = new JdbcTemplate();
		jt.setDataSource(dataSource);
		//2 书写sql,并执行
		String sql = "insert into t_user values(null,'rose') ";
	        jt.update(sql);
	}
	
	@Test
	public void fun2() throws Exception{
		User u = new User();
		u.setName("tom");
		ud.save(u);
	}
	@Test
	public void fun3() throws Exception{
		User u = new User();
		u.setId(2);
		u.setName("jack");
		ud.update(u);
	}
	
	@Test
	public void fun4() throws Exception{
		ud.delete(2);
	}
	
	@Test
	public void fun5() throws Exception{
		System.out.println(ud.getTotalCount());
	}
	
	@Test
	public void fun6() throws Exception{
		System.out.println(ud.getById(1));
	}
	
	@Test
	public void fun7() throws Exception{
		System.out.println(ud.getAll());
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值