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 Object10 * 数据库访问工具类11 * Created by yanshaochen on 17-7-25.12 */
13 public classBaseDAO {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 publicConnection getConnection() throws Exception {23 Class.forName(DRIVER);24 if(con==null||con.isClosed()){25 con=DriverManager.getConnection(URL,USR,PASSWORD);26 }27 returncon;28 }29 //close rs,ps,con
30 public voidcloseResources() 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 intexecuteUpdate(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
52 publicResultSet executeQuery(String sql,Object... objs) throws Exception {53 getConnection();54 ps=con.prepareStatement(sql);55 if(objs!=null){56 for (int i=0;i
实体类代码:
1 package cn.happy.entity;2
3 /**4 * Created by yanshaochen on 17-7-25.5 */
6 public classGrade {7 privateString gradeName;8
9 publicString getGrade() {10 returngradeName;11 }12
13 public voidsetGrade(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 interfaceIGradeService {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 classGradeServiceImpl implements IGradeService {12 IGradeDAO dao=newGradeDAOImpl();13 @Override14 publicboolean addGrade(Grade grade) throws Exception {15 returndao.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 interfaceIGradeDAO {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 classGradeDAOImpl extends BaseDAO implements IGradeDAO{11 @Override12 publicboolean 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 returnflag;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 classMyMain {11 public static voidmain(String[] args) throws Exception {12 Grade grade = newGrade();13 grade.setGrade("T21");14 IGradeService igs=newGradeServiceImpl();15 boolean isSuccess =igs.addGrade(grade);16 if(isSuccess){17 System.out.println("添加成功!");18 }else{19 System.out.println("添加失败!");20 }21 }22 }