Spring Jdbc 의 강 함 을 증명 합 니 다.

9570 단어 자바springjdbc
Spring 에 대해 조금 알 고 있 는 단계 에 있 었 습 니 다.최근 에 회사 에서 프레임 워 크 를 보 았 습 니 다.spring 으로 만 들 었 습 니 다.아주 강하 게 썼 습 니 다!그 중에서 삭제 수정 은 주로 namedParameterJdbcTemplate 와 Bean PropertySql ParameterSource 두 가지 유형 을 사용 합 니 다!
 
  Spring Jdbc   
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 {
        // 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));
    }
}


  Spring NamedParameterJdbcTemplate  DAO    

2009-06-30 12:42:24|    :      |     
NamedParameterJdbcTemplate       JdbcTemplate,  JdbcTemplate     NamedParameterJdbcTemplate   ,NamedParameterJdbcTemplate   JdbcTemplate              。
public Object queryForObject(String sql, Map paramMap, RowMapper rowMapper)
public Object queryForObject(String sql, SqlParameterSource paramSource, RowMapper rowMapper)
       SqlParameterSource       MapSqlParameterSource
        BeanPropertySqlParameterSource
public int update(String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder)        。 
    :
/**
 *  、NamedParameterJdbcTemplate       , :x   ?
 * NamedParameterJdbcTemplate      map  SqlParameterSource
 *  map    :x,x       ,              map  ,   key :     ,value     
 *  SqlParameterSource            ,  :x  x              
 
 *  、  KeyHolder keyholder=new GeneratedKeyHolder();
   namedjdbctemp.update(sql, ps,keyholder);             
 */
public class StuDaoImple implements StuDaointer{ 
    private NamedParameterJdbcTemplate namedjdbctemp;
 
    public StuDaoImple(){
       namedjdbctemp=new NamedParameterJdbcTemplate(SQLConnUtil.getDataSource());
    }// SQLConnUtil JDBC   ,             
    /*
     *      NamedParameterJdbcTemplate    :
     * 1,           。
     * 2,              
     */
    public void addStu(Stu stu) {
       String sql="insert into stu values(:sname,:ssex,:sbrith)";
       //:        stu      
       SqlParameterSource ps=new BeanPropertySqlParameterSource(stu);
       KeyHolder keyholder=new GeneratedKeyHolder();
       namedjdbctemp.update(sql, ps,keyholder);
       //  KeyHolder               
       int m=keyholder.getKey().intValue();
       System.out.println(m);
       //Map map=keyholder.getKeys();//            
       //keyholder.getKeyList();//            ,          
    }
    public int count() {
       String sql="select count(*) from stu";
       //    NamedParameterJdbcTemplate  JdbcTemplate
       int m=namedjdbctemp.getJdbcOperations().queryForInt(sql);
       return m;
    }
 
    /*
     *      NamedParameterJdbcTemplate     :
     *     
     */
    public void delStu(int sid) {
       String sql="delete stu where s_id=:id";
       Map map=new HashMap();
       map.put("id", sid);
       namedjdbctemp.update(sql, map);
    }
    public List getAllStu() {
       String sql="select s_id as sid,s_name as sname,s_sex as ssex,s_brith as sbrith from stu";
       List list=namedjdbctemp.getJdbcOperations().query(sql,new BeanPropertyRowMapper(Stu.class));
       return list;
    }
    public List getAllStu(Stu stu) {
       String sql="select s_id as sid,s_name as sname,s_sex as ssex,s_brith as sbrith from stu where s_id=:sid";
       SqlParameterSource ps=new BeanPropertySqlParameterSource(stu);
       return namedjdbctemp.query(sql, ps, new BeanPropertyRowMapper(Stu.class));
    }
    public Stu getOneStu(Stu stu) {
       String sql="select s_id as sid,s_name as sname,s_sex as ssex,s_brith as sbrith from stu where s_id=:sid";
       SqlParameterSource ps=new BeanPropertySqlParameterSource(stu);
       return (Stu)namedjdbctemp.queryForObject(sql, ps, new BeanPropertyRowMapper(Stu.class));
    }
    public String getStuName(Stu stu) {
       String sql="select s_name as sname from stu where s_name=:sname";
       SqlParameterSource ps=new BeanPropertySqlParameterSource(stu);
       return (String)namedjdbctemp.queryForObject(sql, ps, String.class);
    }
    public void updStu(Stu stu) {
       String sql="update stu set s_name=:sname,s_sex=:ssex,s_brith=:sbrith where s_id=:sid";
       SqlParameterSource ps=new BeanPropertySqlParameterSource(stu);
       namedjdbctemp.update(sql, ps);
    }
}

좋은 웹페이지 즐겨찾기