Spring JDBC SqlParameterSource

 BeanPropertySqlParameterSource,MapSqlParameterSource提供了对SqlParameterSource的实现,

允许使用javabean或者Map来实现对命名参数传递。

@Component 
public class UserInfoDaoJdbcImpl extends SimpleJdbcDaoSupport implements UserInfoDao { 

    public int deleteEntity(UserInfo t) throws DataAccessException { 
        if (null != t.getId()) { 
            return super.getSimpleJdbcTemplate().update("delete from user_info where id=:id", t.getId()); 
        } else if (null != t.getMap().get("ids")) { 
            return super.getSimpleJdbcTemplate().update( 
                    "delete from user_info where id in (" + t.getMap().get("ids") + ")", t.getId()); 
        } else { 
            return -1; 
        } 
    } 
    public Integer insertEntity(UserInfo t) throws DataAccessException { 
        String sql = "insert into user_info (user_name, password, birthday, age) values (:user_name, :password, :birthday, :age)"; 
        return super.getSimpleJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(t)); 
    } 
    public UserInfo selectEntity(UserInfo t) throws DataAccessException { 
        List<String> sqls = new ArrayList<String>(); 
        sqls.add("select * from user_info where 1=1"); 
        if (null != t.getId()) { 
            sqls.add("and id=:id"); 
        } 
        if (null != t.getUser_name()) { 
            sqls.add("and login_name=:login_name"); 
        } 
        if (null != t.getPassword()) { 
            sqls.add("and password=:password"); 
        } 
        String sql = StringUtils.join(sqls, " "); 
        List<UserInfo> userInfoList = super.getSimpleJdbcTemplate().query(sql, 
                ParameterizedBeanPropertyRowMapper.newInstance(UserInfo.class), new BeanPropertySqlParameterSource(t)); 
        int listSize = userInfoList.size(); 
        if (1 == listSize) { 
            return userInfoList.get(0); 
        } else { 
            return null; 
        } 
    } 
    public Long selectEntityCount(UserInfo t) throws DataAccessException { 
        List<String> sqls = new ArrayList<String>(); 
        sqls.add("select count(*) from user_info where 1=1"); 
        if (null != t.getId()) { 
            sqls.add("and id=:id"); 
        } 
        if (null != t.getUser_name()) { 
            sqls.add("and login_name=:login_name"); 
        } 
        if (null != t.getPassword()) { 
            sqls.add("and password=:password"); 
        } 
        String sql = StringUtils.join(sqls, " "); 
        return super.getSimpleJdbcTemplate().queryForLong(sql, new BeanPropertySqlParameterSource(t)); 
    } 

    public List<UserInfo> selectEntityList(UserInfo t) throws DataAccessException { 
        List<String> sqls = new ArrayList<String>(); 
        sqls.add("select * from user_info where 1=1"); 
        if (null != t.getId()) { 
            sqls.add("and id=:id"); 
        } 
        if (null != t.getUser_name()) { 
            sqls.add("and login_name=:login_name"); 
        } 
        if (null != t.getPassword()) { 
            sqls.add("and password=:password"); 
        } 
        String sql = StringUtils.join(sqls, " "); 
        return super.getSimpleJdbcTemplate().query(sql, ParameterizedBeanPropertyRowMapper.newInstance(UserInfo.class), 
                new BeanPropertySqlParameterSource(t)); 
    } 
    public List<UserInfo> selectEntityPaginatedList(UserInfo t) throws DataAccessException { 
        // TODO Auto-generated method stub 
        return null; 
    } 

    public int updateEntity(UserInfo t) throws DataAccessException { 
        List<String> sqls = new ArrayList<String>(); 
        if (null != t.getAge()) { 
            sqls.add(" age=:age"); 
        } 
        if (null != t.getBirthday()) { 
            sqls.add(" birthday=:birthday"); 
        } 

        String sql = StringUtils.join(new String[] { "update user_info set ", StringUtils.join(sqls.toArray(), ","), 
                " where id=:id" }); 

        return super.getSimpleJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(t)); 
    } 

} 
 
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

public class FormJdbcDao2  extends NamedParameterJdbcDaoSupport{
  public void addForm(final Form form){
   final String sql  = "insert into tb_user(userName) values(:userName)";
   //BeanPropertySqlParameterSource该类实现了一个JavaBean的对象,封装成一个参数源,
   //以便通过JavaBean属性名和SQL语句中的命名参数匹配方式绑定参数
   SqlParameterSource sps  = new BeanPropertySqlParameterSource(form);
   getNamedParameterJdbcTemplate().update(sql, sps);
   
  }
  public void addForm1(final Form form){
   final String sql  = "insert into tb_user(userName) values(:userName)";
   //MapSqlParameterSource该类内部通过一个Map存储参数,可以通过addValue(String paramName,Object value)或者
   //addValues(Map values)添加参数。如果表记录没有对应的领域对象,用户可以直接通过使用MapSqlParameterSource的方式进行参数绑定。
   MapSqlParameterSource  sps  = new MapSqlParameterSource().addValue("userName", form.getUserName());
   getNamedParameterJdbcTemplate().update(sql, sps);
   
  }
 
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值