普通Dao操作通用步驟
1.寫SQL語句
2.獲取連接
3.創(chuàng)建PreparedStatement
4.執(zhí)行sql語句
a)更新
b)查詢
5.關閉/異常
BaseDao的實現(xiàn)
BaseDao是自己寫的通用的dao父類,自己寫的所有的dao都繼承這個父類dao
通用方法
更新類別的:
update
,delete
,insert
-
查詢類別的
代碼中用到元數(shù)據(jù)方法簡書地址:數(shù)據(jù)庫之元數(shù)據(jù)——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();
//創(chuàng)建執(zhí)行任務
pstmt = conn.prepareStatement(sql);
//參數(shù)元數(shù)據(jù): 得到占位符參數(shù)的個數(shù)
int count = pstmt.getParameterMetaData().getParameterCount();
//判斷是否有條件
if (paramsValue != null && paramsValue.length > 0) {
//循環(huán)給參數(shù)賦值
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 <T> List<T> query (String sql, Object[] paramsValue , Class<T> tClass){
List <T> list = new ArrayList<T>();
//獲取連接
conn = ConnUtil.getConnextion();
try {
//創(chuàng)建對象
pstmt = conn.prepareStatement(sql);
int count = pstmt.getParameterMetaData().getParameterCount();
if(paramsValue !=null && paramsValue.length> 0 ){
for(int i=0;i<paramsValue.length;i++){
pstmt.setObject(i+1,paramsValue[i]);
}
}
rs = pstmt.executeQuery();
//拿到結果集元數(shù)據(jù)
ResultSetMetaData rsmd = rs.getMetaData();
//獲取列的個數(shù)
int culumnCount =rsmd.getColumnCount();
T t;
while(rs.next()){
t = tClass.newInstance();
for (int i = 0; i<culumnCount;i++){
String coulumnName =rsmd.getColumnName(i+1);
Object value = rs.getObject(coulumnName);
BeanUtils.copyProperty(t,coulumnName,value);
}
list.add(t);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
DAO層: 下來我們在寫Dao層的代碼時就特別簡單
下面示例:
- 查詢數(shù)據(jù)
- 查找數(shù)據(jù)按照id
- 插入數(shù)據(jù)
- 更新數(shù)據(jù)
- 刪除數(shù)據(jù)
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> TronClasstype() {
String sql = " SELECT * FROM eu_tronclass ; ";
List <TronClasstype> 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 <TronClasstype> 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);
}
}