網站首頁 學習教育 IT科技 金融知識 旅遊規劃 生活小知識 家鄉美食 養生小知識 健身運動 美容百科 遊戲知識 綜合知識
當前位置:趣知科普吧 > 綜合知識 > 

通用basedao怎麼寫

欄目: 綜合知識 / 發佈於: / 人氣:2.24W
1.java中網站所用到的baseDao怎麼寫呢

/** * 數據庫連接 * * @author Administrator * */public class BaseDao { /** 連接對象 */ protected Connection con; /** 預編譯 */ protected PreparedStatement ps; /** 結果集 */ protected ResultSet rs; /** 資源檔案對象 */ private static Properties pro = new Properties(); /** * 靜態代碼塊,此塊在第一次新建類對象前優先加載在類模板中,只執行一次並且返回的pro靜態屬性一直儲存直到程序關閉 * */ static { /** 得到檔案的字節流 */ InputStream in = BaseDao.class.getResourceAsStream("/txt/dao.txt"); try { pro.load(in); } catch (IOException e) { e.printStackTrace(); } } /** * 連接數據庫,獲取Connection對象 * * @throws * 沒有找到類檔案 * @throws SQLException * 數據庫訪問異常 已測試透過 */ protected void setConnection() { try { Class.forName(pro.getProperty("driver")); this.con = DriverManager.getConnection(pro.getProperty("url"), pro .getProperty("userName"), pro.getProperty("pwd")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 關閉數據庫連接 * * @throws SQLException * 數據庫異常 * */ protected void close() { try { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (con != null) { con.close(); } } catch (Exception e) { e.printStackTrace(); } }} 上面這個是一個BaseDao這個只是起創建連接的作用,dao繼承他就行了 下面這個是一個查詢方法..只是給你做個例子示範.. 你如果不懂 你說清楚你的表的需求這些 我給你寫好查詢的方法.. public List findName(String ename) throws Exception { List all = new ArrayList(); String sql = "SELECT * FROM emp WHERE ename LIKE ? "; this.pstmt = this.conn.prepareStatement(sql); this.pstmt.setString(1, "%" + ename + "%"); ResultSet rs = this.pstmt.executeQuery(); while (rs.next()) { Emp emp = new Emp(); emp.setEmpno(rs.getInt(1)); emp.setEname(rs.getString(2)); emp.setJob(rs.getString(3)); emp.setHiredate(rs.getDate(4)); emp.setSal(rs.getFloat(5)); emp.setComm(rs.getFloat(6)); emp.setMgr(rs.getInt(7)); all.add(emp); } return all; }。

通用basedao怎麼寫
2.java中網站所用到的baseDao怎麼寫呢

/** * 數據庫連接 * * @author Administrator * */public class BaseDao { /** 連接對象 */ protected Connection con; /** 預編譯 */ protected PreparedStatement ps; /** 結果集 */ protected ResultSet rs; /** 資源檔案對象 */ private static Properties pro = new Properties(); /** * 靜態代碼塊,此塊在第一次新建類對象前優先加載在類模板中,只執行一次並且返回的pro靜態屬性一直儲存直到程序關閉 * */ static { /** 得到檔案的字節流 */ InputStream in = BaseDao.class.getResourceAsStream("/txt/dao.txt"); try { pro.load(in); } catch (IOException e) { e.printStackTrace(); } } /** * 連接數據庫,獲取Connection對象 * * @throws * 沒有找到類檔案 * @throws SQLException * 數據庫訪問異常 已測試透過 */ protected void setConnection() { try { Class.forName(pro.getProperty("driver")); this.con = DriverManager.getConnection(pro.getProperty("url"), pro .getProperty("userName"), pro.getProperty("pwd")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 關閉數據庫連接 * * @throws SQLException * 數據庫異常 * */ protected void close() { try { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (con != null) { con.close(); } } catch (Exception e) { e.printStackTrace(); } }} 上面這個是一個BaseDao這個只是起創建連接的作用,dao繼承他就行了 下面這個是一個查詢方法..只是給你做個例子示範.. 你如果不懂 你說清楚你的表的需求這些 我給你寫好查詢的方法.. public List findName(String ename) throws Exception { List all = new ArrayList(); String sql = "SELECT * FROM emp WHERE ename LIKE ? "; this.pstmt = this.conn.prepareStatement(sql); this.pstmt.setString(1, "%" + ename + "%"); ResultSet rs = this.pstmt.executeQuery(); while (rs.next()) { Emp emp = new Emp(); emp.setEmpno(rs.getInt(1)); emp.setEname(rs.getString(2)); emp.setJob(rs.getString(3)); emp.setHiredate(rs.getDate(4)); emp.setSal(rs.getFloat(5)); emp.setComm(rs.getFloat(6)); emp.setMgr(rs.getInt(7)); all.add(emp); } return all; }。

3.Java中的BaseDao怎麼用

你好,我寫的BaseDao:

package dao;

import java.sql.*;

/**

*

* @author Administrator

*數據庫連接

*/

public class BaseDao {

//連接字元串

public String driver="oracle.jdbc.driver.OracleDriver";//數據庫驅動

public String url="jdbc:oracle:thin:@localhost:1521:hfaccp";//建立到給定數據庫 URL 的連接。

public String username="system";//數據庫用戶

public String password="system";//數據庫密碼

//聲明接口

public Connection con;

public PreparedStatement pstmt;

public ResultSet rs;

//獲得數據庫連接

public Connection getConnection()

{

try {

Class.forName(driver);

con=DriverManager.getConnection(url,username,password);

} catch ( e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

return con;

}

//釋放數據庫資源

public void CloseAll()

{

if(rs!=null)

{

try {

rs.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if(pstmt!=null)

{

try {

pstmt.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if(con!=null)

{

try {

con.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

4.求大神

public interface BaseDAO { /** * 儲存一個對象 * * @param o * @return */ public Serializable save(T o); /** * 刪除一個對象 * * @param o */ public void delete(T o); /** * 更新一個對象 * * @param o */ public void update(T o); /** * 儲存或更新對象 * * @param o */ public void saveOrUpdate(T o); /** * 查詢 * * @param hql * @return */ public List find(String hql); /** * 查詢集合 * * @param hql * @param param * @return */ public List find(String hql, Object[] param); /** * 查詢集合 * * @param hql * @param param * @return */ public List find(String hql, List param); /** * 查詢集合(帶分頁) * * @param hql * @param param * @param page * 查詢第幾頁 * @param rows * 每頁顯示幾條記錄 * @return */ public List find(String hql, Object[] param, Integer page, Integer rows); /** * 查詢集合(帶分頁) * * @param hql * @param param * @param page * @param rows * @return */ public List find(String hql, List param, Integer page, Integer rows); /** * 獲得一個對象 * * @param c * 對象類型 * @param id * @return Object */ public T get(Class c, Serializable id); /** * 獲得一個對象 * * @param hql * @param param * @return Object */ public T get(String hql, Object[] param); /** * 獲得一個對象 * * @param hql * @param param * @return */ public T get(String hql, List param); /** * select count(*) from 類 * * @param hql * @return */ public Long count(String hql); /** * select count(*) from 類 * * @param hql * @param param * @return */ public Long count(String hql, Object[] param); /** * select count(*) from 類 * * @param hql * @param param * @return */ public Long count(String hql, List param); /** * 執行HQL語句 * * @param hql * @return 響應數目 */ public Integer executeHql(String hql); /** * 執行HQL語句 * * @param hql * @param param * @return 響應數目 */ public Integer executeHql(String hql, Object[] param); /** * 執行HQL語句 * * @param hql * @param param * @return */ public Integer executeHql(String hql, List param); }。

5.spring mvc spring hibernate basedao該怎麼寫怎麼用

你在繼承類的時候,可以繼承它所有的公用方法和屬性,而這些類的屬性有兩種注入方式,一種是顯式注入,一種是隱式注入。隱式注入就是你繼承的類的屬性上面有類似於 @Autowired之類的註解,你在Spring中直接可以進行注入,顯式注入是你繼承的類的屬性上面沒有類似於注入相關的註解,所以只有從新重載你繼承類的某個屬性的set方法來進行添加註入的註解來進行注入。

就想你繼承了HibernateDaoSupport 類,就要顯式重載HibernateDaoSupport中的方法

這樣你在配置spring中的注入纔不會出錯

Tags:basedao 通用