Dao 작업 추출 - BaseDao 캡슐화

5486 단어
일반 Dao 작업 일반 절차
1. SQL 문 쓰기
2. 연결 가져오기
3. PreparedStatement 만들기
4. sql 문장 실행하기
a) 업데이트 b) 질의
5. 닫기/예외
BaseDao의 구현
BaseDao는 자신이 쓴 통용적인dao 부류입니다. 자신이 쓴 모든dao는 이 부류dao를 계승합니다.
통용 방법
  • 업데이트 카테고리: update, delete, insert
  • 쿼리 범주의
  • 코드에 사용된 메타데이터 메타데이터 메타데이터 주소: 데이터베이스의 메타데이터 - DatabaseMetaData
    package com.eu.dss.dao;
    
    import com.eu.dss.util.ConnUtil;
    import java.util.List;
    import org.apache.commons.beanutils.BeanUtils;
    import java.sql.*;
    import java.util.ArrayList;
    
    /**
     * Created by     on 2017/5/18.
     */
    public class BaseDao {
        private Connection conn;
        private PreparedStatement pstmt;
        private ResultSet rs;
    
        /**
         *        
         *    sql  (update/insert/delete)
         * paramsValue  sql          (       ,  null)
         */
        public void update(String sql, Object[] paramsValue) {
            try {
                //    
                conn = ConnUtil.getConnextion();
                //      
                pstmt = conn.prepareStatement(sql);
                //     :           
                int count = pstmt.getParameterMetaData().getParameterCount();
                //       
                if (paramsValue != null && paramsValue.length > 0) {
                    //       
                    for (int i = 0; i < count; i++) {
                        pstmt.setObject(i + 1, paramsValue[i]);
                    }
                }
                pstmt.executeUpdate();
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                ConnUtil.close(null, pstmt, conn);
    
            }
        }
    
        /**
         *       
         */
        public  List query (String sql, Object[] paramsValue , Class tClass){
            List  list = new ArrayList();
            //    
            conn = ConnUtil.getConnextion();
            try {
                //    
                pstmt = conn.prepareStatement(sql);
                int count = pstmt.getParameterMetaData().getParameterCount();
                if(paramsValue !=null && paramsValue.length> 0 ){
                    for(int i=0;i

    DAO 계층: 우리가 Dao 계층의 코드를 쓸 때 매우 간단합니다.
    다음 예는 다음과 같습니다.
  • 조회 데이터
  • 데이터 찾기 id
  • 데이터 삽입
  • 업데이트 데이터
  • 데이터 삭제
  • package com.eu.dss.dao.impl;
    
    import com.eu.dss.dao.BaseDao;
    import com.eu.dss.dao.ITronClassDao;
    import com.eu.dss.entity.TronClasstype;
    import net.sf.json.JSONArray;
    
    import java.util.List;
    
    /**
     * Created by     on 2017/5/23.
     */
    public class TronClassDao extends BaseDao implements ITronClassDao  {
    
        public List TronClasstype() {
            String sql = " SELECT * FROM eu_tronclass ; ";
            List  list = super.query(sql,null,TronClasstype.class);
            JSONArray jsonArray = JSONArray.fromObject(list);
            System.out.println("bbbb"+jsonArray);
            return list;
        }
    
        public TronClasstype findByid(int id) {
            String sql = " SELECT * FROM eu_tronclass where id=? ; ";
            List  list = super.query(sql,new Object[id],TronClasstype.class);
            return (list!=null && list.size()>0) ? list.get(0) : null;
        }
    
        public void save(TronClasstype tronClassType) {
            String sql = " INSERT INTO eu_tronclass (year,tron_month,eu_rj,eu_xin,eu_rw,eu_ts,eu_xiu,eu_gz,eu_kuai,eu_ad,eu_wc,eu_wu,eu_jr) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?); ";
            Object[] paramsValue = {tronClassType.getYear(),tronClassType.getTron_month(),
                    tronClassType.getEu_rj(),   tronClassType.getEu_xin(),
                    tronClassType.getEu_rw(),   tronClassType.getEu_ts(),
                    tronClassType.getEu_xiu(),  tronClassType.getEu_gz(),
                    tronClassType.getEu_kuai(), tronClassType.getEu_ad(),
                    tronClassType.getEu_wc(),   tronClassType.getEu_wu(),
                    tronClassType.getEu_jr()};
            super.update(sql,paramsValue);
        }
    
        public void update(TronClasstype tronClassType ) {
            String sql = " UPDATE eu_tronclass SET year = ?,tron_month =?,eu_rj =?," +
                    "eu_xin =?,eu_rw=?,eu_ts=?,eu_xiu=?,eu_gz=?,eu_kuai=?," +
                    "eu_ad=?,eu_wc=?,eu_wu=?,eu_jr=? where id=?";
            Object[] paramsValue = {tronClassType.getYear(),tronClassType.getTron_month(),
                    tronClassType.getEu_rj(),   tronClassType.getEu_xin(),
                    tronClassType.getEu_rw(),   tronClassType.getEu_ts(),
                    tronClassType.getEu_xiu(),  tronClassType.getEu_gz(),
                    tronClassType.getEu_kuai(), tronClassType.getEu_ad(),
                    tronClassType.getEu_wc(),   tronClassType.getEu_wu(),
                    tronClassType.getEu_jr(),   tronClassType.getId()};
            super.update(sql,paramsValue);
        }
        public void delete(int id) {
            String sql = " delete from eu_tronclass where id =? ";
            Object[] paramsValue = {id};
            super.update(sql,paramsValue);
        }
    }
    

    문장 문집:JavaEE--학습 노트

    좋은 웹페이지 즐겨찾기