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); } }
- Spring JDBC SqlParameterSource
- spring NamedParameterJdbcTemplate 和 SqlParameterSource 参数绑定使用
- Spring JDBC
- spring+JDBC
- Spring JDBC
- spring + jdbc
- Spring JDBC
- spring+jdbc
- spring+jdbc
- Spring JDBC
- Spring JDBC
- Spring JDBC
- Spring JDBC
- spring+jdbc
- Spring JDBC
- spring+jdbc
- spring jdbc
- Spring JDBC
- 图说JS viewer 研究总结
- 纠结的apache httpclient与swt browser共享cookies
- 如何下载和离线安装Chrome的CRX扩展文件包
- pclint9配置说明
- "CREATE TABLE AS SELECT"以及"INSERT INTO AS SELECT"语句的优化
- Spring JDBC SqlParameterSource
- android文件上传到服务器
- ASP中Response.End之后代码不运行!含验证代码
- 映 射 ALT 键
- Quartz管理类
- 对EditText状态进行监听
- jquery 分页控件实现代码
- 算法入门竞赛经典第七章暴力求解法7.1.1
- C++栈和堆原理介绍