java mysql dao_Java DAO 模式
哈哈哈哈哈。。。呃~參考文章
DAO 模式
DAO (DataAccessobjects 數(shù)據(jù)存取對象)是指位于業(yè)務(wù)邏輯和持久化數(shù)據(jù)之間實現(xiàn)對持久化數(shù)據(jù)的訪問。通俗來講,就是將數(shù)據(jù)庫操作都封裝起來。
對外提供相應(yīng)的接口
在面向?qū)ο笤O(shè)計過程中,有一些"套路”用于解決特定問題稱為模式。
DAO 模式提供了訪問關(guān)系型數(shù)據(jù)庫系統(tǒng)所需操作的接口,將數(shù)據(jù)訪問和業(yè)務(wù)邏輯分離對上層提供面向?qū)ο蟮臄?shù)據(jù)訪問接口。
從以上 DAO 模式使用可以看出,DAO 模式的優(yōu)勢就在于它實現(xiàn)了兩次隔離。
1、隔離了數(shù)據(jù)訪問代碼和業(yè)務(wù)邏輯代碼。業(yè)務(wù)邏輯代碼直接調(diào)用DAO方法即可,完全感覺不到數(shù)據(jù)庫表的存在。分工明確,數(shù)據(jù)訪問層代碼變化不影響業(yè)務(wù)邏輯代碼,這符合單一職能原則,降低了藕合性,提高了可復(fù)用性。
2、隔離了不同數(shù)據(jù)庫實現(xiàn)。采用面向接口編程,如果底層數(shù)據(jù)庫變化,如由 MySQL 變成 Oracle 只要增加 DAO 接口的新實現(xiàn)類即可,原有 MySQ 實現(xiàn)不用修改。這符合 "開-閉" 原則。該原則降低了代碼的藕合性,提高了代碼擴(kuò)展性和系統(tǒng)的可移植性。
一個典型的DAO 模式主要由以下幾部分組成。
1、DAO接口: 把對數(shù)據(jù)庫的所有操作定義成抽象方法,可以提供多種實現(xiàn)。
2、DAO 實現(xiàn)類: 針對不同數(shù)據(jù)庫給出DAO接口定義方法的具體實現(xiàn)。
3、實體類:用于存放與傳輸對象數(shù)據(jù)。
4、數(shù)據(jù)庫連接和關(guān)閉工具類: 避免了數(shù)據(jù)庫連接和關(guān)閉代碼的重復(fù)使用,方便修改。
DAO 接口:
public interface PetDao {
/**
* 查詢所有寵物
*/
List findAllPets() throws Exception;
}
DAO 實現(xiàn)類:
public class PetDaoImpl extends BaseDao implements PetDao {
/**
* 查詢所有寵物
*/
public List findAllPets() throws Exception {
Connection conn=BaseDao.getConnection();
String sql="select * from pet";
PreparedStatement stmt= conn.prepareStatement(sql);
ResultSet rs=????stmt.executeQuery();
List petList=new ArrayList();
while(rs.next()) {
Pet pet=new Pet(
rs.getInt("id"),
rs.getInt("owner_id"),
rs.getInt("store_id"),
rs.getString("name"),
rs.getString("type_name"),
rs.getInt("health"),
rs.getInt("love"),
rs.getDate("birthday")
);
petList.add(pet);
}
BaseDao.closeAll(conn, stmt, rs);
return petList;
}
}
寵物實體類(里面get/set方法就不列出了)
public class Pet {
private Integer id;
private Integer ownerId;????//主人ID
private Integer storeId;????//商店ID
private String name;????//姓名
private String typeName;????//類型
private int health;????//健康值
private int love;????//愛心值
private Date birthday;????//生日
}
連接數(shù)據(jù)庫
public class BaseDao {
private static String driver="com.mysql.jdbc.Driver";
private static String url="jdbc:mysql://127.0.0.1:3306/epet";
private static String user="root";
private static String password="root";
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
public static void closeAll(Connection conn,Statement stmt,ResultSet rs) throws SQLException {
if(rs!=null) {
rs.close();
}
if(stmt!=null) {
stmt.close();
}
if(conn!=null) {
conn.close();
}
}
public int executeSQL(String preparedSql, Object[] param) throws ClassNotFoundException {
Connection conn = null;
PreparedStatement pstmt = null;
/* 處理SQL,執(zhí)行SQL */
try {
conn = getConnection(); // 得到數(shù)據(jù)庫連接
pstmt = conn.prepareStatement(preparedSql); // 得到PreparedStatement對象
if (param != null) {
for (int i = 0; i < param.length; i++) {
pstmt.setObject(i + 1, param[i]); // 為預(yù)編譯sql設(shè)置參數(shù)
}
}
ResultSet num = pstmt.executeQuery(); // 執(zhí)行SQL語句
} catch (SQLException e) {
e.printStackTrace(); // 處理SQLException異常
} finally {
try {
BaseDao.closeAll(conn, pstmt, null);
} catch (SQLException e) {
e.printStackTrace();
}
}
return 0;
}
}
總結(jié)
以上是生活随笔為你收集整理的java mysql dao_Java DAO 模式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php mysql读取数据_PHP My
- 下一篇: mysql 插入记录慢_mysql:in