通用basedao怎么写

来源:趣味经验馆 2.28W
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中的注入才不会出错

热门标签