今天主要是三层架构的一个进阶,我们要把三层构架运用到我们原先的新闻系统中去,所以今天主要是代码, 大家看下在新闻系统中是如何运用三层构架。
目录
1.文件放置位置
【命名规则】
com.biz -> Biz接口
com.biz.impl ->Biz的接口实现类
com.dao -> Dao接口
com.dao.impl -> Dao接口的实现类
2.三层构造创建
在JavaWeb 11中我们在运用三层构造时,是先写biz接口和biz的实现类(业务逻辑层),但是在我们正常运用三层构造时,应该先把dao接口&dao接口的实现类(数据访问层)先写好,把在要用的方法,都在接口中先写好,列如:增,删,改,查.......,然后在数据访问层有的方法,在业务逻辑层都要实现。
【调用顺序】
表示层->业务逻辑层->数据库访问层----- |
|
表示层<-业务逻辑层<-数据库访问层------|
表示层:在JavaWeb中表示层为JSP文件,就像登录界面,新闻首页
业务逻辑层: biz接口和biz实现类
数据库访问层 :dao接口和dao的实现类
3.接口的作用
在我们运用三层构造时,业务逻辑层和数据访问层都有接口,实现类会实现我们的接口,而当实现类实现了该接口,那么接口中有的方法你也要具备,因为实现类实现了该接口,如果不写接口中的方法,那么无法运行,起到约束的一个作用。
4.实操代码
1.dao接口&dao的实现类(数据访问层)
dao接口代码如下:
package com.yjx.dao;
import java.util.List;
import com.yjx.pojo.News;
import com.yjx.pojo.User;
/**
* 数据访问层接口
* @author zjjt
*
*/
public interface IDao {
//页数的方法
List<News> selectLsit(String name,int page);
//根据用户名和密码查询数据
User select(User u);
//删除新闻
int del(int id);
//有几页
int select(String name);
}
dao实现类代码如下:
package com.yjx.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.yjx.dao.IDao;
import com.yjx.pojo.News;
import com.yjx.pojo.User;
import ulit.DBHeper;
/**
* 存放数据访问层的实体类
* @author zjjt
*
*/
public class DaoImpl implements IDao{
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
/**
*
* @param name 标题名字
* @param page 页数
* @return
*/
public List<News> selectLsit(String name,int page){
List<News> list=new ArrayList<News>();
int row=5;//条数,因为每一页五条数据
//初始
int begin=1+(page-1)*5;
//末尾
int last=page*row;
try {
con=DBHeper.getCon();
ps=con.prepareStatement("select * from(select a.*,rownum myr from jw05_news a where news_title like ?)b where myr between ? and ?");
ps.setString(1,"%"+name+"%");
ps.setInt(2, begin);
ps.setInt(3, last);
rs=ps.executeQuery();
while(rs.next()) {
News news=new News();
news.setNews_id(rs.getInt(1));
news.setNews_title(rs.getString(2));
news.setNews_topic(rs.getInt(3));
news.setNews_author(rs.getString(4));
news.setNews_publisher(rs.getString(5));
news.setNews_contet(rs.getString(6));
news.setNews_cover(rs.getString(7));
news.setNews_count(rs.getInt(8));
list.add(news);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHeper.getColse(con, ps, rs);
}
return list;
}
/**
* 查找用户
*/
public User select(User u) {
try {
con=DBHeper.getCon();
ps=con.prepareStatement("select * from jw05_user where uname=? and upwd=?");
ps.setString(1,u.getUsername());
ps.setString(2,u.getUserpwd());
rs=ps.executeQuery();
while(rs.next()) {
User user=new User();
user.setUserid(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setUserpwd(rs.getString(3));
return user;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHeper.getColse(con, ps, rs);
}
return null;
}
/**
* 删除新闻
*/
public int del(int id) {
try {
con=DBHeper.getCon();
ps=con.prepareStatement("delete from jw05_news where news_id=?");
ps.setInt(1, id);
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHeper.getColse(con, ps, rs);
}
return 0;
}
/**
* 算出页数
*/
public int select(String name) {
//页数=总的新闻条数/条数
int row=5;//条数
int count=0;//总的条数
try {
con=DBHeper.getCon();
ps=con.prepareStatement("select count(1) from jw05_news where news_title like ?");
ps.setString(1,"%"+name+"%");
rs=ps.executeQuery();
if(rs.ne