Java學習筆記——三層架構

Layer:java

UI層:  user interface 用戶接口層mysql

Biz層:   service business login layer 業務邏輯層sql

DAO層:   Date Access Object 數據訪問層數據庫

1.創建三層架構架構

UI層(對應包ui):這裏就是一個簡單的測試類app

Biz層(對應包service):包括實體類的service層接口IGradeService和其實現類(impl包下)GradeServiceimplide

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 }

實體類代碼:ui

 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 }
相關文章
相關標籤/搜索