posts - 495,comments - 227,trackbacks - 0

Spring 不仅仅是一个IoC container。

其提供的Spring Jdbc提供了一层对jdbc很薄的封装,功能却是异常强大。

 

不说多了,直接贴代码

 

1、Template模式的使用,使我们只关心,sql, 输入参数,输出映射。

2、new BeanPropertySqlParameterSource(t) 直接把bean转成输入参数。

3、ParameterizedBeanPropertyRowMapper.newInstance(UserInfo.class)直接将bean转成输出映射。

4、一个dao根据输入参数不同,动态生成多条sql语句。很有一点ibatis的味道。

5、学习曲线低,会sql和jdbc的可以直接写。适合“你请到了只会写jsp和sql的极品,又想让他明天开始干活..... ”

  1. @Component
  2. public class UserInfoDaoJdbcImpl extends SimpleJdbcDaoSupport implements UserInfoDao {
  3.     public int deleteEntity(UserInfo t) throws DataAccessException {
  4.         if (null != t.getId()) {
  5.             return super.getSimpleJdbcTemplate().update("delete from user_info where id=:id", t.getId());
  6.         } else if (null != t.getMap().get("ids")) {
  7.             return super.getSimpleJdbcTemplate().update(
  8.                     "delete from user_info where id in (" + t.getMap().get("ids") + ")", t.getId());
  9.         } else {
  10.             return -1;
  11.         }
  12.     }
  13.     public Integer insertEntity(UserInfo t) throws DataAccessException {
  14.         String sql = "insert into user_info (user_name, password, birthday, age) values (:user_name, :password, :birthday, :age)";
  15.         return super.getSimpleJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(t));
  16.     }
  17.     public UserInfo selectEntity(UserInfo t) throws DataAccessException {
  18.         List<String> sqls = new ArrayList<String>();
  19.         sqls.add("select * from user_info where 1=1");
  20.         if (null != t.getId()) {
  21.             sqls.add("and id=:id");
  22.         }
  23.         if (null != t.getUser_name()) {
  24.             sqls.add("and login_name=:login_name");
  25.         }
  26.         if (null != t.getPassword()) {
  27.             sqls.add("and password=:password");
  28.         }
  29.         String sql = StringUtils.join(sqls, " ");
  30.         List<UserInfo> userInfoList = super.getSimpleJdbcTemplate().query(sql,
  31.                 ParameterizedBeanPropertyRowMapper.newInstance(UserInfo.class), new BeanPropertySqlParameterSource(t));
  32.         int listSize = userInfoList.size();
  33.         if (1 == listSize) {
  34.             return userInfoList.get(0);
  35.         } else {
  36.             return null;
  37.         }
  38.     }
  39.     public Long selectEntityCount(UserInfo t) throws DataAccessException {
  40.         List<String> sqls = new ArrayList<String>();
  41.         sqls.add("select count(*) from user_info where 1=1");
  42.         if (null != t.getId()) {
  43.             sqls.add("and id=:id");
  44.         }
  45.         if (null != t.getUser_name()) {
  46.             sqls.add("and login_name=:login_name");
  47.         }
  48.         if (null != t.getPassword()) {
  49.             sqls.add("and password=:password");
  50.         }
  51.         String sql = StringUtils.join(sqls, " ");
  52.         return super.getSimpleJdbcTemplate().queryForLong(sql, new BeanPropertySqlParameterSource(t));
  53.     }
  54.     public List<UserInfo> selectEntityList(UserInfo t) throws DataAccessException {
  55.         List<String> sqls = new ArrayList<String>();
  56.         sqls.add("select * from user_info where 1=1");
  57.         if (null != t.getId()) {
  58.             sqls.add("and id=:id");
  59.         }
  60.         if (null != t.getUser_name()) {
  61.             sqls.add("and login_name=:login_name");
  62.         }
  63.         if (null != t.getPassword()) {
  64.             sqls.add("and password=:password");
  65.         }
  66.         String sql = StringUtils.join(sqls, " ");
  67.         return super.getSimpleJdbcTemplate().query(sql, ParameterizedBeanPropertyRowMapper.newInstance(UserInfo.class),
  68.                 new BeanPropertySqlParameterSource(t));
  69.     }
  70.     public List<UserInfo> selectEntityPaginatedList(UserInfo t) throws DataAccessException {
  71.         // TODO Auto-generated method stub
  72.         return null;
  73.     }
  74.     public int updateEntity(UserInfo t) throws DataAccessException {
  75.         List<String> sqls = new ArrayList<String>();
  76.         if (null != t.getAge()) {
  77.             sqls.add(" age=:age");
  78.         }
  79.         if (null != t.getBirthday()) {
  80.             sqls.add(" birthday=:birthday");
  81.         }
  82.         String sql = StringUtils.join(new String[] { "update user_info set ", StringUtils.join(sqls.toArray(), ","),
  83.                 " where id=:id" });
  84.         return super.getSimpleJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(t));
  85.     }
  86. }

posted on 2008-11-11 13:34 SIMONE 阅读(2068) 评论(0)  编辑  收藏 所属分类: JAVA

只有注册用户登录后才能发表评论。


网站导航: