Spring JDBC SqlParameterSource

来源:互联网 发布:mac java9.0怎么卸载 编辑:程序博客网 时间:2024/05/24 01:47

 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);     } }