三层架构是指哪三层.docx
《三层架构是指哪三层.docx》由会员分享,可在线阅读,更多相关《三层架构是指哪三层.docx(14页珍藏版)》请在冰豆网上搜索。
三层架构是指哪三层
1.三层架构是指哪三层
界面(视图)层
业务层
数据访问(持久层)
2.为什么使用三层
职责划分清楚,各司其职,各层配合
例如:
发现sql语句写错了,sql语句的定义一定在dao层
3.上层如何将数据传递给下层
例如:
数据从界面如何传给业务
数据如何从业务传给dao
方法:
要将数据传给谁,就new谁的对象,然后用new出来的对象调用方法,数据作为方法参数传递
4.下层如何将数据传递给上层
下层通过返回值将数据传递给上层
5.各层中都写什么代码
5.1.界面层
界面层主要职责是输入和输出
5.2.业务层
编写控制业务流程的代码,通常是很多if语句来控制业务流程,是核心层
例如:
业务:
用银行卡取钱
业务流程
1:
判断卡是否是银行卡
2:
验证卡号和密码是否正确
3:
验证卡是否被冻结
4:
判断余额是否够用
5:
是否跨行
6:
是否跨地区
7:
开始取钱
5.3.数据访问层
5.3.1.Dao
1.拼写sql语句
2.为sql语句的参数准备值
3.发送sql和值到dbhelepr
Dao程序编写的模板
publicintsave(Useruser)throwsException{
Try{
拼写sql
准备值
调用dbhelpoer执行sql
}catcha(Exceptione){
异常处理,将异常抛出
}Finally{
Dbheleper.close()
}
}
5.3.2.dbHelper
执行sql语句
6.三层示例
6.1.需求
1:
实现添加商品
2:
商品的列表显示
6.2.准备开发环境
6.2.1.数据库环境
CREATEDATABASEthreelayer;
USEthreelayer;
CREATETABLEproduct
(
idINTAUTO_INCREMENTPRIMARYKEY,
productNameVARCHAR(30),
priceDOUBLE
);
6.2.2.Java环境
同一个项目中,每个开发人员的各个环境的版本必须一致
1.Jdk的版本:
1.8
2.Eclipse的版本:
Kepler
3.Jar包:
a)Mysql数据库的驱动jar
b)Junit的jar
6.2.3.创建java项目
项目命名为threelayer1126
6.2.4.分包
edu.xbmu.threelayer.view:
界面层
edu.xbmu.threelayer.service:
业务层
edu.xbmu.threelayer.dao:
数据访问层
edu.xbmu.threelayer.pojo:
实体类
6.2.5.准备DBHelper(其实你可以拷贝)
packageedu.xbmu.threelayer.dao;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.util.Properties;
/**
*该类的作用是执行sql语句
*该类必须通用,通用是指对所有的表的sql语句都能够执行
*/
publicclassDBHelper{
//定义属性
privateConnectionconnection=null;
privatePreparedStatementstatement=null;
privateResultSetrs=null;
privatestaticStringURL="jdbc:
mysql:
//localhost:
3306/threelayer";
privatestaticStringUSER="root";
privatestaticStringPASS="root";//硬编码
//静态块加载驱动,只加载一次
static{
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}
}
//加载数据库的配置文件
static{
try{
Filefile=newFile("c:
/db.properties");
FileInputStreaminStream=newFileInputStream(file);
//Properties是键值对的集合类,父接口是Map
Propertiesprop=newProperties();
prop.load(inStream);
URL=(String)prop.get("url");
USER=(String)prop.get("user");
PASS=(String)prop.get("pass");
}catch(IOExceptione){
System.out.println("数据库配置文件加载失败,请停止所有的操作,检查数据库配置文件");
}
}
/**
*初始化连接对象
*/
privatevoidinitConnection(){
try{
if(connection==null||connection.isClosed()==true){
connection=DriverManager.getConnection(URL,USER,PASS);
}
}catch(SQLExceptione){
e.printStackTrace();
}
}
//sql=insertintouser(username,userpass)values(?
?
)
//values=["admin","123"]
/**
*执行添加,删除,修改的语句
*@paramsql被执行的sql语句
*@paramvaluessql语句中占位符的值
*@return返回影响的行数
*@throwsSQLException
*/
publicintexecuteUpdate(Stringsql,Object...values)throwsSQLException{
//创建连接对象
initConnection();
//创建执行sql语句的对象
statement=connection.prepareStatement(sql);
//为sql语句占位符赋值
setParameter(values);
//执行sql语句
returnstatement.executeUpdate();
}
publicResultSetexecuteQuery(Stringsql,Object...values)throwsSQLException{
//连接数据库
initConnection();
//创建执行sql语句对象
statement=connection.prepareStatement(sql);
//为占位符赋值
setParameter(values);
rs=statement.executeQuery();
returnrs;
}
/**
*为占位符赋值
*/
privatevoidsetParameter(Object...values)throwsSQLException{
if(values!
=null&&values.length>0){
for(inti=0;istatement.setObject(i+1,values[i]);
}
}
}
/**
*关闭数据库
*/
publicvoidclose(){
//关闭库
if(rs!
=null){
try{
rs.close();
}catch(SQLExceptione){
e.printStackTrace();
}
rs=null;
}
if(statement!
=null){
try{
statement.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
if(connection!
=null){
try{
connection.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
}
}
6.3.需求1:
添加商品
6.3.1.开发实体类
6.3.2.开发界面层
在view包中创建表示界面的类,命名为ProductView
privatestaticvoidsave(){
//1:
界面输入数据
System.out.println("请输入商品名称");
StringproductName=in.next();
System.out.println("请输入商品价格");
doubleprice=in.nextDouble();
//2:
对数据进行验证
if(price<0){
System.out.println("商品价格不允许小于0");
System.out.println("商品添加失败");
return;//表示该方法停止运行,返回到调用者
}
//3:
将数据封装到实体对象中
ProductModelproductModel=newProductModel();
productModel.setProductName(productName);
productModel.setPrice(price);
//4:
将数据传递给业务层
ProductServiceproductService=newProductService();
inti=0;
try{
i=productService.save(productModel);
}catch(Exceptione){
System.out.println("保存商品出错"+e);
}
//5:
显示业务返回的结果
if(i>0){
System.out.println("商品添加成功");
}else{
System.out.println("商品添加失败");
}
}
6.3.3.开发业务层
publicclassProductService{
privateProductDaoproductDao=newProductDao();
//定义添加商品业务的方法
publicintsave(ProductModelproductModel)throwsException{
//保存商品没有业务流程,因此直接使用dao调用了save方法
returnproductDao.save(productModel);
}
}
6.3.4.开发dao层
publicclassProductDao{
privateDBHelperhelper=newDBHelper();
//添加商品的dao
publicintsave(ProductModelproductModel)throwsException{
intcount=0;
try{
//1:
拼写sql
Stringsql="INSERTINTOproduct(productName,price)VALUES(?
?
)";
//2:
准备值
Object[]values=newObject[]{productModel.getProductName(),productModel.getPrice()};
//3:
调用dbhelper执行
count=helper.executeUpdate(sql,values);
}catch(Exceptionex){
ex.printStackTrace();
throwex;
}finally{
helper.close();
}
returncount;
}
}
6.3.5.单元测试
publicclassProductTest{
ProductServiceproductService=null;
@Before
publicvoidtearDown(){
productService=newProductService();
}
@Test
publicvoidtestSave()throwsException{
ProductModelproductModel=newProductModel();
productModel.setProductName("农夫");
productModel.setPrice(3);
inti=productService.save(productModel);
Assert.assertEquals(1,i);
}
}
6.4.需求2:
查看所有的商品
6.4.1.开发dao
publicListfindAll()throwsException{
Listlist=newArrayList();
try{
//1:
拼写sql
Stringsql="select*fromproduct";
//2:
准备值
//3:
执行
ResultSetrs=helper.executeQuery(sql);
while(rs.next()){
ProductModelproductModel=newProductModel();
productModel.setId(rs.getInt("id"));
productModel.setProductName(rs.getString("productName"));
productModel.setPrice(rs.getDouble("price"));
list.add(productModel);
}
}catch(Exceptione){
e.printStackTrace();
throwe;
}finally{
helper.close();
}
returnlist;
}
6.4.2.开发service
publicListfindAll()throwsException{
returnproductDao.findAll();
}
6.4.3.开发视图
privatestaticvoidfindAll(){
//格式化货币
NumberFormatnf=NumberFormat.getCurrencyInstance(Locale.CHINA);
try{
ProductServiceproductService=newProductService();
Listlist=productService.findAll();
System.out.println("编号\t名称\t价格");
if(list!
=null&&list.size()>0){
for(inti=0;iSystem.out.print(list.get(i).getId());
System.out.print("\t");
System.out.print(list.get(i).getProductName());
System.out.print("\t");
System.out.println(nf.format(list.get(i).getPrice()));
}
}
}catch(Exceptione){
e.printStackTrace();
}
}
6.4.4.单元测试