20.Spring-JdbcTemplate-连接池&JDBC模板&Dao配置到spring容器

QQ群:Java资料共享群 59174518
在这里插入图片描述

ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:c="http://www.springframework.org/schema/c"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

	<!-- 1.将连接池放到Spring容器里面 -->
	<bean name="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="jdbc:mysql:///jdbc"></property>
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="user" value="root"></property>
		<property name="password" value="root"></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="vc.helloworld.SpringJDBC.UserDaoImpl">
		<property name="jt" ref="jdbcTemplate"></property>
	</bean>
</beans>

测试类

package vc.helloworld.SpringJDBC;

import java.beans.PropertyVetoException;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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 vc.helloworld.SpringJDBC.bean.User;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:vc/helloworld/SpringJDBC/ApplicationContextJdbcTemplate.xml")
public class SpringJdbcTemplate {
	@Resource(name = "userDao")
	private UserDao db;

	@Test
	public void fun13() throws PropertyVetoException {
		// 1.准备连接池
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setDriverClass("com.mysql.jdbc.Driver");
		dataSource.setJdbcUrl("jdbc:mysql:///jdbc");
		dataSource.setUser("root");
		dataSource.setPassword("root");
		// 2.创建SpringJDBC模板
		JdbcTemplate jt = new JdbcTemplate();
		jt.setDataSource(dataSource);

		String sql = "insert into t_user values(null,'Aaron')";

		jt.update(sql);
	}

	@Test
	public void fun14() {
		User u = new User();
		u.setName("Aaron");
		db.save(u);

	}
}

UserDaoImpl

package vc.helloworld.SpringJDBC;

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 vc.helloworld.SpringJDBC.bean.User;

//使用SpringJdbcTemplate模板实现CRUD
public class UserDaoImpl implements UserDao {
	// 生命一个模板,让Spring管理
	private JdbcTemplate jt;

	public void save(User u) {

		String sql = "insert into t_user values(null,?)";
		// u.getName是这个方法里面的形式参数的User
		jt.update(sql, u.getName());
	}

	public void delete(Integer id) {
		String sql = "delete from t_user where id = ?";
		// u.getName是这个方法里面的形式参数的User
		jt.update(sql, id);

	}

	public void update(User u) {
		String sql = "update  t_user set name = ? where id = ?";

		jt.update(sql, u.getName(), u.getId());
	}

	public User getById(Integer id) {
		String sql = "select * from t_user  where id = ?";

		return jt.queryForObject(sql, new RowMapper<User>() {
			// ResultSet这个东西手动实现的话,就会自动遍历结果集
			public User mapRow(ResultSet rs, int arg1) throws SQLException {
				User u = new User();
				u.setId(rs.getInt("id"));
				u.setName(rs.getString("name"));
				return null;
			}
		}, id);
	}

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

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

	// 生成jdbc模板的set方法,等等可以让Spring使用
	public void setJt(JdbcTemplate jt) {
		this.jt = jt;
	}

}

UserDao

package vc.helloworld.SpringJDBC;

import java.util.List;

import vc.helloworld.SpringJDBC.bean.User;

public interface UserDao {
	// 增
	void save(User u);

	// 删
	void delete(Integer id);

	// 该
	void update(User u);

	// 查
	User getById(Integer id);

	// 查
	int getTotalCount();

	// 查
	List<User> getAll();
}

猜你喜欢

转载自blog.csdn.net/redeagles/article/details/82828643