首页 技术 正文
技术 2022年11月18日
0 收藏 827 点赞 2,620 浏览 4228 个字

什么是DAO模式:

DAO(Data Access Object Pattern)用于将低层的数据操作API与上层的业务逻辑层分离,其主要涉及以下几个部分:

1.Data Access Object Interface

定义了在model object上的标准操作接口。

2.Data Access Object concrete class

实现了1中的接口,负责从database或者xml等中操作数据。

3.Model Object or Value Object

简单的POJO对象。

一.  DAO全程是Data Access Object,是J2EE核心模式之一,主要用于上层应用程序与持久化机制之间的中间层,对于底层的数据库持久化,其各部分关系答题如下:

 DAO模式

1、使用JDBC的API访问数据库

连接、SQL语句执行、结果

java.sql.Driver:各个数据库厂商需要实现该接口,驱动的标记

java.sql.Connection:封装和数据库的连接

java.sql.Statement:封装需要执行的SQL语句

java.sql.ResultSet:封装查询的结果集

java.sql.PreparedStatement接口

2、JDBC编程步骤

step1——加载驱动

step2——获取连接对象

step3——执行SQL语句

step4——处理结果集

step5——关闭资源

3.DAO的架构

DAO模式

实现接口:

示例:

public class NewsDAOImpl  implements INewsDAO{
BaseDao dao = new BaseDao();
@Override
public List<News> findAll() throws Exception {
List<News> list=new ArrayList<News>();
String sql="select * from news";
ResultSet rs=dao.executeQuery(sql);
if (rs!=null) {
while (rs.next()) {
News grade=new News();
grade.setId(rs.getInt(1));
grade.setName(rs.getString(2));
grade.setAuthor(rs.getString(3));
grade.setCreateTime(rs.getDate(4));
grade.setContent(rs.getString(5));
list.add(grade);
}
}
return list;
}}

工具类:

public class BaseDao {
private static final String driver="com.mysql.jdbc.Driver";
private static final String url="jdbc:mysql:///news2230";
private static final String username="cai";
private static final String password="root"; private Connection con;
private PreparedStatement ps;
private ResultSet rSet; public Connection getConnection() throws Exception {
Class.forName(driver);
if (con==null||con.isClosed()) {
con=DriverManager.getConnection(url,username,password);
}
return con;
} //增加 修改 删除
public int executeUpdate(String sql,Object...objects) throws Exception {
getConnection();
ps=con.prepareStatement(sql);
for (int i = 0; i < objects.length; i++) {
ps.setObject(i+1, objects[i]);
}
int count=ps.executeUpdate();
return count; } //查询
public ResultSet executeQuery(String sql,Object...objects) throws Exception{
getConnection();
ps=con.prepareStatement(sql);
for (int i = 0; i < objects.length; i++) {
ps.setObject(i+1, objects[i]);
}
rSet = ps.executeQuery();
return rSet; } //释放资源
public void closeResource(){
try {
if (rSet!=null) {
rSet.close();
}
if (ps!=null) {
ps.close();
}
if (con!=null) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
} }
}

接口:

public interface INewsDAO {
public List<News> findAll() throws Exception;
}

实现用户操作相关的类:

public class News {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
private int id; //编号
private String name; //姓名
private Date createTime;//时间
private String content;
public String author;
}

连接dao层和test层的关系:

实现接口

public class NewsServiceImpl implements INewsService {   
INewsDAO aa=new NewsDAOImpl();
@Override
public List<News> findAll() throws Exception { return aa.findAll();
}}

定义接口:

public interface INewsService {
public List<News> findAll() throws Exception;
}

mian方法:

    public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
NewManager manager=new NewManager();
manager.toHtml();
}}

io流:

    public void writeFile(String filePath,String str) throws Exception{
OutputStreamWriter oStreamWriter = new OutputStreamWriter(new FileOutputStream(filePath), "GBK");
oStreamWriter.write(str);
oStreamWriter.close();
} public String readerFile(String filePath) throws Exception{ InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), "GBK");
char[] ch = new char[1024];
int data = 0;
String str = null;
StringBuffer sb = new StringBuffer();
while ((data = isr.read(ch)) != -1) {
str = new String(ch, 0, data);
sb.append(str);
}
System.out.println(sb.toString());
isr.close();
return str;
}}

调用IO实现 html:

  public void toHtml() throws Exception{
FileIo fileio= new FileIo();
String templatestr=fileio.readerFile("F:\\JDBC_CMS\\src\\news.template");
INewsService service=new NewsServiceImpl();
List<News> newlist=service.findAll();
for (int i = 0; i < newlist.size(); i++) {
News news=newlist.get(i);
String rep=new String();
rep=templatestr;
rep=rep.replace("{title}", news.getName());
rep=rep.replace("{author}", news.getAuthor());
rep=rep.replace("{createTime}", news.getCreateTime().toString());
rep=rep.replace("{content}", news.getContent()); String fil="F:\\哈哈哈+"+i+".html";
fileio.writeFile(fil, rep);
}
}}

实现结果:

DAO模式

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919