08Spring整合JDBC

JDBC模板对象

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

		JDBCTemplateJDBC模板对象

与DBUtils的QueryRunner非常相似

1.准备工作
(1)导包

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

在这里插入图片描述
(2)准备数据库

连接池jdbc模板、Dao配置到spring容器中

配置文件:

<?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 ">

<!-- 1.将连接池放入spring容器  -->
	<bean name = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="jdbcUrl" value = "jdbc:mysql:///spring_day3?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=UTC&amp;allowPublicKeyRetrieval=true"></property>
	<property name="driverClass" value = "com.mysql.cj.jdbc.Driver"></property>
	<property name="user" value = "root"></property>
	<property name="password" value = "jzb19981128"></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.itcast.a_jdbc.userDaoImpl">
	<property name="jt" ref = "jdbcTemplate"></property>
	
	</bean>
</beans>

userDaoImpl.java

package cn.itcast.a_jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import cn.itcast.bean.User;

	//使用jdbc模板实现增删改查

public class userDaoImpl implements userDao {

	private JdbcTemplate jt;
	@Override
	public void save(User u) {
		String sql = "insert into t_user values(null,?)";
		jt.update(sql, u.getName());

	}

	@Override
	public void delete(Integer id) {
		String sql = "delete from t_user where id = ?";
		jt.update(sql, id);

	}

	@Override
	public void update(User u) {
		String sql = "update t_user set name = ? where id = ?";
		jt.update(sql, u.getName(), u.getId());

	}

	@Override
	public User getById(Integer id) {
		String sql = "select * from t_user where id = ?";
		
		return jt.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);
		
	}

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

		return count;
	}

	@Override
	public List<User> getAll() {
		String sql = "select * from t_user";
		List<User> list = jt.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;
	}

	public JdbcTemplate getJt() {
		return jt;
	}

	public void setJt(JdbcTemplate jt) {
		this.jt = jt;
	}
	

}


测试:

package cn.itcast.a_jdbc;
import javax.annotation.Resource;
import java.lang.Exception;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.mchange.v2.c3p0.ComboPooledDataSource;

import cn.itcast.bean.User;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")

public class Demo {
	@Resource(name = "userDao")
	private userDao ud;
	@Test
	public void fun1() throws Exception{
		
		//准备连接池
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
		dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/spring_day3?useSSL=false&serverTimezone=GMT" +"&allowPublicKeyRetrieval=true");
		dataSource.setUser("root");
		dataSource.setPassword("jzb19981128");
		
		//1.创建JDBC模板对象
		JdbcTemplate jt = new JdbcTemplate();
		
		jt.setDataSource(dataSource);
		
		//2.书写sql语句,并执行
		String sql = "insert into t_user values(null,'bob')";
		
		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(2));
		
	}
	@Test
	public void fun7() throws Exception{
		System.out.println(ud.getAll());
		
	}
}

在这里插入图片描述

spring整合jdbc扩展—JDBCDaoSupport

读取properties配置

applicationContext

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

<!-- 1.将连接池放入spring容器  -->
	<bean id = "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>

db.properties

jdbc.jdbcUrl=jdbc:mysql:///spring_day3?useSSL=false&sererTimezone=GMT&allowPublicKeyRetrieval=true
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.user=root
jdbc.password=jzb19981128

猜你喜欢

转载自blog.csdn.net/weixin_43801116/article/details/106818775
今日推荐