JDBC之在分层结构中实现业务
JDBC之在分層結(jié)構(gòu)中實(shí)現(xiàn)業(yè)務(wù)
以上篇文章為基礎(chǔ)完成以下操作
一、添加驅(qū)動(dòng)
?在src下創(chuàng)建文件夾lib
?將MySQL數(shù)據(jù)庫(kù)驅(qū)動(dòng)包放入lib文件夾并右鍵點(diǎn)擊build path
二、創(chuàng)建jdbc.properties文件
driver = com.mysql.jdbc.Driver jdbcUrl = jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8 username = root userpassword = mysql
三、模型層(用于存儲(chǔ)數(shù)據(jù)庫(kù)中departments表的數(shù)據(jù))
在pojo包下,創(chuàng)建Departments實(shí)體類;
public class Departments{private int departmentId;private String departmentName;private int locationId;public int getDepartmentId(){return departmentId;} public void setDepartmentId(int departmentId){this.departmentId = departmentId;}public String getDepartmentName(){return departmentName;}public void setDepartmentName(String departmentName){this.departmentName = departmentName;}public int getLocationId(){return locationId;}public void setLocationId(int locationId){this.locationId = locationId;} }四、工具層(對(duì)departments表進(jìn)行操作)
在commons包下創(chuàng)建JdbcUtil工具類;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ResourceBundle;public class JdbcUtil{private static String driver;private static String jdbcUrl;private static String username;private static String userpassword;static{//讀取properties文件ResourceBundle bundle = ResourceBundle.getBundle("jdbc");driver = bundle.getString("driver");jdbcUrl = bundle.getString("jdbcUrl");username = bundle.getString("username");userpassword = bundle.getString("userpassword");try{//驅(qū)動(dòng)注冊(cè)Class.forName(driver);//通過(guò)jdk反射機(jī)制將數(shù)據(jù)庫(kù)驅(qū)動(dòng)類實(shí)例化}catch(ClassNotFoundException e){e.printStackTrace();}}//獲取Connection對(duì)象public static Connection getConnection(){Connection conn = null;try{conn = DriverManager.getConnection(jdbcUrl,username,userpassword);}catch(SQLException e){e.printStackTrace();}return conn;}//關(guān)閉Statementpublic static void closeStatement(Statement state){if(state!=null){try{state.close();}catch(SQLException e){e.printStackTrace();}}}//關(guān)閉Connectionpublic static void closeConnection(Connection conn){if(conn!=null){try{conn.close();}catch(SQLException e){e.printStackTrace();}}}//關(guān)閉ResultSetpublic static void closeResultSet(ResultSet rs){if(rs!=null){try{rs.close();}catch(SQLException e){e.printStackTrace();}}} //關(guān)閉資源public static void closeResource(Statement state , Connection conn , ResultSet rs){closeStatement(state);closeConnection(conn);closeResultSet(rs);}public static void closeResource(Statement state , Connection conn){closeStatement(state);closeConnection(conn);}//事務(wù)回滾public static void rollback(Connection conn){if(conn!=null){try{conn.rollback();}catch(SQLException e){e.printStackTrace();}}} }五、持久層(數(shù)據(jù)訪問(wèn)層)
?持久層下接口的命名都是以“dao”結(jié)尾的;
?Dao代表data access object 數(shù)據(jù)庫(kù)訪問(wèn)對(duì)象;
?抽象方法的命名規(guī)則:不要包含帶有業(yè)務(wù)邏輯的名稱;
?接口實(shí)現(xiàn)類命名規(guī)則:要實(shí)現(xiàn)的接口的名稱后+Impl;
在dao包下建立DepartmentsDao接口(對(duì)departments表進(jìn)行操作)
包含那些對(duì)部門操作的抽象行為
在dao.impl包下創(chuàng)建實(shí)現(xiàn)類;
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List;import com.bjsxt.commons.JdbcUtil; import com.bjsxt.dao.DepartmentsDao; import com.bjsxt.pojo.Departments;public class DepartmentsDaoImpl implements DepartmentsDao{public List<Departments> selectDeptByName(String deptName){Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;List<Departments> list = new ArrayList<Departments>();try{conn = JdbcUtil.getConnection();ps = conn.prepareStatement("select * from departments where department_name = ?");ps.setString(1,deptName);//捆值;rs = ps.executeQuery();while(rs.next()){Departments d = new Departments();/*如果查詢多條結(jié)果的情況下我們要在while中定義數(shù)據(jù)模型*/d.setDepartmentId(rs.getInt("department_id"));d.setDepartmentName(rs.getString("department_name"));d.setLocation_id(rs.getInt("location_id"));list.add(d);}}catch(Exception e){e.printStackTrace();}finally{JdbcUtil.closeResource(ps,conn);}return list; }public void insertDept(Departments dept){Connection conn = null;PreparedStatement ps = null;try{conn = JdbcUtil.getConnection();conn.setAutoCommit(false);ps = conn.prepareStatement("insert into departments values(0,?,?)");ps.setString(1,dept.getDepartmentName());ps.setInt(2,dept.getLocationId());ps.execute(); conn.commit();}catch(Exception e){e.printStackTrace();}finally{JdbcUtil.closeResource(ps,conn);}} }六、業(yè)務(wù)層
?業(yè)務(wù)層的方法命名要貼近這次要做什么樣的業(yè)務(wù)處理;
在service包下創(chuàng)建DepartmentsSerivce接口;
//向Departments表中添加數(shù)據(jù) public interface DepartmentsService{public void addDepartments(Departments dept); }在serivce.impl包下創(chuàng)建實(shí)現(xiàn)類,實(shí)現(xiàn)類命名規(guī)則同上;
public class DepartmentsServiceImpl implements DepartmentsService{public void addDepartments(Departments dept){DepartmentsDao deptDao = new DepartmentsDaoImpl();deptDao.insertDept(dept);} }七、測(cè)試層
在src下創(chuàng)建test包,并在其中創(chuàng)建Test類
public class Test{public static void main(String[] args){Departments dept = new Departments();dept.setDepartmentName("體育部");dept.setLocationId(30);DepartmentsService ds = new DepartmentsServiceImpl();ds.addDepartments(dept); } } 《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的JDBC之在分层结构中实现业务的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: JDBC之应用程序分层
- 下一篇: JDBC之封装通用的BaseDao