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的极品,又想让他明天开始干活..... ”
- @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 {
-
- 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));
- }
- }
posted on 2008-11-11 13:34
SIMONE 阅读(2068)
评论(0) 编辑 收藏 所属分类:
JAVA