DAO模式
什么是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核心模式之一,主要用于上层应用程序与持久化机制之间的中间层,对于底层的数据库持久化,其各部分关系答题如下:
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的架构
实现接口:
示例:
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); } } }
实现结果: