• Java学习笔记——三层架构


    Layer:

    UI层:  user interface 用户接口层

    Biz层:   service business login layer 业务逻辑层

    DAO层:   Date Access Object 数据访问层

    1.建立三层架构

    UI层(对应包ui):这里就是一个简单的测试类

    Biz层(对应包service):包括实体类的service层接口IGradeService和其实现类(impl包下)GradeServiceimpl

    DAO层(对应包dao):BaseDAO工具类和实体类的dao层接口IGradeDAO和其实现类(impl包下)GradeDAOimpl

    BaseDAO代码:

     1 package cn.happy.dao;
     2 
     3 import java.sql.Connection;
     4 import java.sql.DriverManager;
     5 import java.sql.PreparedStatement;
     6 import java.sql.ResultSet;
     7 
     8 /**
     9  * database Data Access Object
    10  * 数据库访问工具类
    11  * Created by yanshaochen on 17-7-25.
    12  */
    13 public class BaseDAO {
    14     static final String URL="jdbc:mysql://127.0.0.1:3306/t14";
    15     static final String DRIVER="com.mysql.jdbc.Driver";
    16     static final String USR="root";
    17     static final String PASSWORD="root";
    18     private Connection con=null;
    19     private PreparedStatement ps=null;
    20     private ResultSet rs=null;
    21     //getConnection
    22     public Connection getConnection() throws Exception {
    23         Class.forName(DRIVER);
    24         if(con==null||con.isClosed()){
    25             con= DriverManager.getConnection(URL,USR,PASSWORD);
    26         }
    27         return con;
    28     }
    29     //close rs,ps,con
    30     public void closeResources() throws Exception {
    31         if(rs!=null)
    32             rs.close();
    33         if(ps!=null)
    34             ps.close();
    35         if(con!=null)
    36             con.close();
    37     }
    38     //executeUpdate
    39     public int executeUpdate(String sql,Object... objs) throws Exception {
    40         int count=0;
    41         getConnection();
    42         ps=con.prepareStatement(sql);
    43         if(objs!=null){
    44             for (int i=0;i<objs.length;i++){
    45                 ps.setObject(i+1,objs[i]);
    46             }
    47         }48         count=ps.executeUpdate();
    49         return count;
    50     }
    51     //executeQuery
    52     public ResultSet executeQuery(String sql,Object... objs) throws Exception {
    53         getConnection();
    54         ps=con.prepareStatement(sql);
    55         if(objs!=null){
    56             for (int i=0;i<objs.length;i++){
    57                 ps.setObject(i+1,objs[i]);
    58             }
    59         }60         rs = ps.executeQuery();
    61         return rs;
    62     }
    63 }

    实体类代码:

     1 package cn.happy.entity;
     2 
     3 /**
     4  * Created by yanshaochen on 17-7-25.
     5  */
     6 public class Grade {
     7     private String gradeName;
     8 
     9     public String getGrade() {
    10         return gradeName;
    11     }
    12 
    13     public void setGrade(String gradeName) {
    14         this.gradeName = gradeName;
    15     }
    16 }

    Biz层接口代码:

     1 package cn.happy.service;
     2 
     3 import cn.happy.entity.Grade;
     4 
     5 /**
     6  * Created by yanshaochen on 17-7-25.
     7  */
     8 public interface IGradeService {
     9     boolean addGrade(Grade grade) throws Exception;
    10 }

    实现类代码(与dao层联系):

     1 package cn.happy.service.impl;
     2 
     3 import cn.happy.dao.IGradeDAO;
     4 import cn.happy.dao.impl.GradeDAOImpl;
     5 import cn.happy.entity.Grade;
     6 import cn.happy.service.IGradeService;
     7 
     8 /**
     9  * Created by yanshaochen on 17-7-25.
    10  */
    11 public class GradeServiceImpl implements IGradeService {
    12     IGradeDAO dao=new GradeDAOImpl();
    13     @Override
    14     public boolean addGrade(Grade grade) throws Exception {
    15         return dao.addGrade(grade);
    16     }
    17 }

    DAO层接口代码:

     1 package cn.happy.dao;
     2 
     3 import cn.happy.entity.Grade;
     4 
     5 /**
     6  * Created by yanshaochen on 17-7-25.
     7  */
     8 public interface IGradeDAO {
     9     boolean addGrade(Grade grade) throws Exception;
    10 }

    实现类代码:

     1 package cn.happy.dao.impl;
     2 
     3 import cn.happy.dao.BaseDAO;
     4 import cn.happy.dao.IGradeDAO;
     5 import cn.happy.entity.Grade;
     6 
     7 /**
     8  * Created by yanshaochen on 17-7-25.
     9  */
    10 public class GradeDAOImpl extends BaseDAO implements IGradeDAO{
    11     @Override
    12     public boolean addGrade(Grade grade) throws Exception {
    13         boolean flag=false;
    14         String sql="insert into grade(gradeName)values(?);";
    15         int count = executeUpdate(sql,grade.getGrade());
    16         if(count>0){
    17             flag=true;
    18         }
    19         return flag;
    20     }
    21 }

    测试类代码:

     1 package cn.happy.ui;
     2 
     3 import cn.happy.entity.Grade;
     4 import cn.happy.service.IGradeService;
     5 import cn.happy.service.impl.GradeServiceImpl;
     6 
     7 /**
     8  * Created by yanshaochen on 17-7-25.
     9  */
    10 public class MyMain {
    11     public static void main(String[] args) throws Exception {
    12         Grade grade = new Grade();
    13         grade.setGrade("T21");
    14         IGradeService igs=new GradeServiceImpl();
    15         boolean isSuccess = igs.addGrade(grade);
    16         if(isSuccess){
    17             System.out.println("添加成功!");
    18         }else {
    19             System.out.println("添加失败!");
    20         }
    21     }
    22 }
  • 相关阅读:
    用Eclipse+MyEclipse开发struts的一个经典的实例(转)
    TOMCAT配置虚拟目录
    翻动100万级的数据(自定义的MSSQL分页查询过程)
    MyEclipse Hibernate 快速入门中文版
    微软提供的数据访问组件SqlHelper
    Java内存管理(一、内存分配)
    使用commonlogging与log4j打印日志,发现版本冲突
    Java内存管理(二、Java垃圾回收)
    初探java内存机制_堆和栈
    关于单CPU,多CPU上的原子操作
  • 原文地址:https://www.cnblogs.com/tomasman/p/7235814.html
Copyright © 2020-2023  润新知