Spring NamedParameterJdbcTemplate完成数据的增删改查

1、Spring配置文件



        
        
        
        
        
        
        
        	
        
        
        
           	
            
                ${jdbc.driver}
            
            
            
                ${jdbc.url}
            
            
            
                ${jdbc.username}
            
            
            
                ${jdbc.password}
            
                 
              
                
              
               
              
                
              
                 
             
                
              
                
             
                
               
          



2、持久类操作数据库代码

package com.spring.data;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;


@Repository
public class JdbcSpitterDao {
	
	@Resource
	private NamedParameterJdbcTemplate jdbcTemplate;
	//插入数据的sql
	private static final String ADD_SQL="insert into tbl_c3p0(name,address,age) values(:name,:address,:age)";
	//根据id获取user对象
	private static final String QUERY_SQL="select id,name,address,age from tbl_c3p0 where id=:id";
	//修改user对象
	private static final String UPDATE_SQL="update tbl_c3p0 set name=:name,address=:address,age=:age where id=:id";
	//根据id删除user对象
	private static final String DELETE_SQL="delete from tbl_c3p0 where id=:id";
	/**
	 * 插入数据
	 */
	public void add(User user){
	    Map paramMap = new HashMap<>();
	    paramMap.put("name", user.getName());
	    paramMap.put("address", user.getAddress());
	    paramMap.put("age", user.getAge());
		jdbcTemplate.update(ADD_SQL, paramMap);
	}
	/**
	 * 获取User对象
	 */
	public User queryById(int id){
	    Map paramMap = new HashMap<>();
	    paramMap.put("id", id);
	    RowMapper rm = ParameterizedBeanPropertyRowMapper.newInstance(User.class);
            return jdbcTemplate.queryForObject(QUERY_SQL, paramMap, rm);
	}
	/**
	 * 根据User对象进行查找
	 */
	public User queryByUser(User user){
		SqlParameterSource ps = new BeanPropertySqlParameterSource(user);
		RowMapper rm = ParameterizedBeanPropertyRowMapper.newInstance(User.class);
		return jdbcTemplate.queryForObject(QUERY_SQL, ps, rm);
	}
	/**
	 * 根据id修改user对象
	 */
	public void update(User user){
		SqlParameterSource ps=new BeanPropertySqlParameterSource(user);
		jdbcTemplate.update(UPDATE_SQL, ps);
	}
	/**
	 * 根据id删除user对象
	 */
	public void delete(int id){
		Map paramMap = new HashMap<>();
	    paramMap.put("id", id);
		jdbcTemplate.update(DELETE_SQL, paramMap);
	}
}

3、注意事项

在queryById方法中如果这样写:
public User queryById(int id){
		Map paramMap = new HashMap<>();
	    paramMap.put("id", id);
		return jdbcTemplate.queryForObject(QUERY_SQL, paramMap, User.class);
	}
就会报:
Spring NamedParameterJdbcTemplate完成数据的增删改查_第1张图片

这样的问题


你可能感兴趣的:(spring)