Spring JdbcTemplate完成数据的增删改查

1、spring配置及讲解



        
        
        
        
        
        
        
        	
        
        
        
           	
            
                ${jdbc.driver}
            
            
            
                ${jdbc.url}
            
            
            
                ${jdbc.username}
            
            
            
                ${jdbc.password}
            
                 
              
                
              
               
              
                
              
                 
             
                
              
                
             
                
               
          
需要注意的是如果checkoutTimeout配置的时间过短会报下面的错误
Spring JdbcTemplate完成数据的增删改查_第1张图片

我的默认配置
#MySql database setting
jdbc.driver=com.mysql.jdbc.Driver
#localtest
jdbc.url=jdbc:mysql://127.0.0.1:3306/test

jdbc.username=root
jdbc.password=root


#c3p0 settings
c3p0.maxPoolSize=15
c3p0.minPoolSize=10
c3p0.initialPoolSize=3
c3p0.maxIdleTime=60
c3p0.checkoutTimeout=500
c3p0.acquireIncrement=3
c3p0.idleConnectionTestPeriod=60
c3p0.maxStatements=100

2、对应的操作bean

package com.spring.data;

public class User {
	private int id;
	private String name;
	private String address;
	private int age;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", address=" + address + ", age=" + age + "]";
	}
	
}	

3、持久成操作数据库完成数据的增删改查

package com.spring.data;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class JdbcSpitterDao {
	
	@Resource
	private JdbcTemplate jdbcTemplate;
	//插入数据的sql
	private static final String ADD_SQL="insert into tbl_c3p0(name,address,age) values(?,?,?)";
	//根据id获取user对象
	private static final String QUERY_SQL="select id,name,address,age from tbl_c3p0 where id=?";
	//修改user对象
	private static final String UPDATE_SQL="update tbl_c3p0 set name=?,address=?,age=? where id=?";
	//根据id删除user对象
	private static final String DELETE_SQL="delete from tbl_c3p0 where id=?";
	/**
	 * 插入数据
	 */
	public void add(User user){
		jdbcTemplate.update(ADD_SQL,user.getName(),user.getAddress(),user.getAge());
	}
	/**
	 * 获取User对象
	 */
	public User query(int id){
		return jdbcTemplate.queryForObject(QUERY_SQL, new ParameterizedRowMapper() {
			public User mapRow(ResultSet rs,int rowNum) throws SQLException{
				User user = new User();
				user.setId(rs.getInt(1));
				user.setName(rs.getString(2));
				user.setAddress(rs.getString(3));
				user.setAge(rs.getInt(4));
				return user;
			}
		},id);
	}
	/**
	 * 根据id修改user对象
	 */
	public void update(User user){
		jdbcTemplate.update(UPDATE_SQL, user.getName(),user.getAddress(),user.getAge(),user.getId());
	}
	/**
	 * 根据id删除user对象
	 */
	public void delete(int id){
		jdbcTemplate.update(DELETE_SQL, id);
	}
}

4、整个过程测试入口

public class Test {
	public static void main(String[] args) {
		GenericXmlApplicationContext context = new GenericXmlApplicationContext();
		context.load("com/spring/data/applicationContext.xml");
		context.refresh();
		JdbcSpitterDao dao = (JdbcSpitterDao)context.getBean("jdbcSpitterDao");
		/*User user = new User();
		user.setId(1);
		user.setName("fanjunkai");
		user.setAddress("hennapuyang");
		user.setAge(18);
		dao.add(user);*/
		
		/*User u1 = dao.query(2);
		System.out.println(u1.toString());
		User u2 = new User();
		u2.setAge(10);
		u2.setName("xiayali");
		u2.setAddress("zhumadian");
		u2.setId(2);
		dao.update(u2);*/
		dao.delete(1);
	}
}

整个过程都是经过测试能够完整通过的






你可能感兴趣的:(spring)