1. 程式人生 > >Spring整合jdbc-連線池&JDBC模板&Dao配置到spring容器_

Spring整合jdbc-連線池&JDBC模板&Dao配置到spring容器_

Spring整合jdbc-連線池&JDBC模板&Dao配置到spring容器_
一、導包
二、建立資料庫
三、寫好類
3.1 使用者類

package cn.itheima;

public class User {
	private Integer id;
	private String name;
	public Integer getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + "]";
	}
	
}

3.2 介面類

package cn.itheima;

import java.util.List;

public interface UserDao{
	//增
	void save(User u);
	//刪
	void delete(Integer id);
	//改
	void update(User u);
	//查
	User getByid(Integer id);
	//查總數
	int getTotalCount();
	//查
	List<User> getAll();
}

3.3 繼承類

package cn.itheima;

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

import javax.annotation.Resource;

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

public class UserImpl implements UserDao {
	
	private JdbcTemplate jt;//這個有xml配置,Spring自動提供
	
	
	public void setJt(JdbcTemplate jt) {
		this.jt = 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 arg0, int arg1) throws SQLException {
				User u = new User();
				u.setId(arg0.getInt("id"));
				u.setName(arg0.getString("name"));
				return u;
			}}, id);
		}
	//查
	@Override
	public int getTotalCount() {
		String sql = "select count(*) from t_user";
		Integer queryForObject = jt.queryForObject(sql, Integer.class);
		return queryForObject;
	}
	//查  找全部的資訊
	@Override
	public List<User> getAll() {
		String sql = "select * from t_user";
		List<User> query = jt.query(sql, new RowMapper<User>() {

			@Override
			public User mapRow(ResultSet arg0, int arg1) throws SQLException {
				User u = new User();
				u.setId(arg0.getInt("id"));
				u.setName(arg0.getString("name"));
				return u;
			}});
		return query;
	}

}

四、配置xml檔案

<?xml version="1.0" encoding="UTF-8"?>
<beans 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 " xmlns:context="http://www.springframework.org/schema/context" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<!-- 指定spring讀取db.properties配置 -->
<!-- 1.將連線池放入到Spring容器中 -->	
	 <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql:///student"></property>
		<property name="user" value="root"></property>
		<property name="password" value="123456"></property>
	</bean>  
	<!-- 2 將JDBCTemplate放入spring容器  -->
	<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean> 
	<!-- 3.userDao放入容器中去 -->
	 <bean name="userDao" class="cn.itheima.UserImpl">
		<property name="jt" ref="jdbcTemplate"></property>
		
	</bean> 
	
</beans>

五、測試類

package Demo;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.itheima.User;
import cn.itheima.UserDao;
import cn.itheima.UserImpl;
//1.建立容器
@RunWith(SpringJUnit4ClassRunner.class)
//2.載入xml檔案
@ContextConfiguration("classpath:application.xml")
public class Demo1 {
	@Resource(name="userDao")
	private UserDao ue;
	@Test
	public void fun1() {
		User us = new User();
		us.setName("tom");
		ue.save(us);
	}
	
	@Test//刪除
	public void fun2() throws Exception{
		/*User u = new User();
		u.getId()*/
		ue.delete(2);
		
	}
	@Test//改
	public void fun3() throws Exception{
		User u = new User();
		u.setId(6);
		u.setName("jack");
		ue.update(u);
		
	}
	@Test//查
	public void fun4() throws Exception{
		System.out.println(ue.getByid(6));
		
	}
	@Test//查詢全部
	public void fun5() throws Exception{
		System.out.println(ue.getAll());
		
	}
	@Test//查總數
	public void fun6() throws Exception{
		System.out.println(ue.getTotalCount());
		
	}
	
}