時(shí)間:2023-08-30 01:06:01 | 來(lái)源:網(wǎng)站運(yùn)營(yíng)
時(shí)間:2023-08-30 01:06:01 來(lái)源:網(wǎng)站運(yùn)營(yíng)
大數(shù)據(jù)從入門(mén)到深入:JavaEE 之 動(dòng)態(tài)網(wǎng)頁(yè)開(kāi)發(fā)基礎(chǔ) JSP實(shí)戰(zhàn)優(yōu)化:使用分層技術(shù)處理業(yè)務(wù):import java.io.File;/** * 自動(dòng)創(chuàng)建包工具類(lèi) * @author My * */public class AutoCreatePackage { /** * 自動(dòng)創(chuàng)建包 * * @param args */ public static void main(String[] args) { // 獲取src目錄 String srcdir = System.getProperty("user.dir") + "//src"; // 創(chuàng)建com包 File file = new File(srcdir + "//com"); // 判斷 if (!file.exists()) { file.mkdir(); } // 創(chuàng)建com.hnxy包 file = new File(srcdir + "//com//hnxy"); // 判斷 if (!file.exists()) { file.mkdir(); } // 創(chuàng)建com.hnxy.entity file = new File(srcdir + "//com//hnxy//entity"); // 判斷 if (!file.exists()) { file.mkdir(); } // 創(chuàng)建com.hnxy.utils file = new File(srcdir + "//com//hnxy//utils"); // 判斷 if (!file.exists()) { file.mkdir(); } // 創(chuàng)建com.hnxy.dao file = new File(srcdir + "//com//hnxy//dao"); // 判斷 if (!file.exists()) { file.mkdir(); } // 創(chuàng)建com.hnxy.service file = new File(srcdir + "//com//hnxy//service"); // 判斷 if (!file.exists()) { file.mkdir(); } // 創(chuàng)建com.hnxy.web file = new File(srcdir + "//com//hnxy//web"); // 判斷 if (!file.exists()) { file.mkdir(); } // 創(chuàng)建com.hnxy.dao.impl file = new File(srcdir + "//com//hnxy//dao//impl"); // 判斷 if (!file.exists()) { file.mkdir(); } // 創(chuàng)建com.hnxy.service.impl file = new File(srcdir + "//com//hnxy//service//impl"); // 判斷 if (!file.exists()) { file.mkdir(); } System.out.println("所有包創(chuàng)建完畢,請(qǐng)選中src包按F5鍵!"); }}
有的同學(xué)會(huì)問(wèn)沒(méi)有放在默認(rèn)包下的話就會(huì)出現(xiàn)下面的報(bào)錯(cuò)package com.hnxy.dao;import java.util.List;import com.hnxy.entity.Person;/** * 人員表的數(shù)據(jù)庫(kù)操作接口 意義 定義我們現(xiàn)在的思想 * @author My * */public interface PersonDAO { /** * 添加方法 * @param person 要添加的對(duì)象 * @return 返回1 代表添加成功 返回0 代表添加失敗 * @throws Exception 聲明一個(gè)我也不知道會(huì)報(bào)什么錯(cuò)的異常 */ public int insertPerson(Person person)throws Exception; /** * 更新方法 * @param person 要更新的對(duì)象 * @return 返回1 代表更新成功 返回0 代表更新失敗 * @throws Exception 聲明一個(gè)我也不知道會(huì)報(bào)什么錯(cuò)的異常 */ public int updatePerson(Person person)throws Exception; /** * 刪除方法 * @param person 要?jiǎng)h除的對(duì)象 * @return 返回1 刪除成功 返回0 刪除失敗 * @throws Exception 聲明一個(gè)我也不知道會(huì)報(bào)什么錯(cuò)的異常 */ public int deletePerson(Person person)throws Exception; /** * 根據(jù)主鍵ID查詢信息 * @param id 要查詢的數(shù)據(jù)ID * @return 查詢到了 返回對(duì)象 沒(méi)有查詢到 返回null * @throws Exception 聲明一個(gè)我也不知道會(huì)報(bào)什么錯(cuò)的異常 */ public Person findPersonByID(Integer id)throws Exception; /** * 查詢?nèi)?/span> * @return 查詢到了返回對(duì)象list 沒(méi)有查詢到返回null * @throws Exception 聲明一個(gè)我也不知道會(huì)報(bào)什么錯(cuò)的異常 */ public List<Person> findAllPersons()throws Exception;}
定義好結(jié)構(gòu)之后我們?cè)傧朕k法去實(shí)現(xiàn),對(duì)于接口的實(shí)現(xiàn)類(lèi),一般我們也會(huì)放到一個(gè)特定的包下, 如果是dao層接口的實(shí)現(xiàn)類(lèi) 就放在dao包下的impl包下 package com.hnxy.dao.impl;import java.sql.Connection;import java.util.List;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.BeanHandler;import org.apache.commons.dbutils.handlers.BeanListHandler;import com.hnxy.dao.PersonDAO;import com.hnxy.entity.Person;import com.hnxy.utils.JdbcUtil;/** * 數(shù)據(jù)庫(kù)操作層的實(shí)現(xiàn)類(lèi) * @author My * */public class PersonDAOImpl implements PersonDAO { @Override public int insertPerson(Person person) throws Exception { // 創(chuàng)建方法的返回值 int count = 0; // 獲取數(shù)據(jù)庫(kù)連接 Connection conn = JdbcUtil.getConn(); // 創(chuàng)建SQL執(zhí)行對(duì)象 QueryRunner qr = new QueryRunner(); // 編寫(xiě)SQL語(yǔ)句 String sql = "insert into person values (null,?,?,?,?)"; // 占位符賦值 Object[] params = {person.getName(),person.getSex(),person.getAge(),person.getFrom()}; // 執(zhí)行 count = qr.update(conn,sql,params); // 關(guān)閉 JdbcUtil.closeConn(conn); // 返回 return count; } @Override public int updatePerson(Person person) throws Exception { // 創(chuàng)建方法的返回值 int count = 0; // 獲取數(shù)據(jù)庫(kù)連接 Connection conn = JdbcUtil.getConn(); // 創(chuàng)建SQL的執(zhí)行對(duì)象 QueryRunner qr = new QueryRunner(); // 編寫(xiě)SQL語(yǔ)句 String sql = "update person set `name`=?,`sex`=?,`age`=?,`from`=? where `id`=?"; // 占位符賦值 Object[] params = {person.getName(),person.getSex(),person.getAge(),person.getFrom(),person.getId()}; // 執(zhí)行 count = qr.update(conn, sql,params); // 關(guān)閉 JdbcUtil.closeConn(conn); // 返回 return count; } @Override public int deletePerson(Person person) throws Exception { // 創(chuàng)建方法的返回值 int count = 0; // 獲取數(shù)據(jù)庫(kù)連接 Connection conn = JdbcUtil.getConn(); // 創(chuàng)建SQL的執(zhí)行對(duì)象 QueryRunner qr = new QueryRunner(); // 編寫(xiě)SQL語(yǔ)句 String sql = "delete from person where `id` = ?"; // 占位符賦值 Object[] params = {person.getId()}; // 執(zhí)行 count = qr.update(conn, sql, params); // 關(guān)閉 JdbcUtil.closeConn(conn); // 返回 return count; } @Override public Person findPersonByID(Integer id) throws Exception { // 創(chuàng)建方法的返回值 Person person = null; // 獲取數(shù)據(jù)庫(kù)連接 Connection conn = JdbcUtil.getConn(); // 創(chuàng)建SQL的執(zhí)行對(duì)象 QueryRunner qr = new QueryRunner(); // 編寫(xiě)SQL語(yǔ)句 String sql = "select * from person where `id` = ?"; // 是否需要占位符賦值? Object[] params = {id}; // 執(zhí)行 person = qr.query(conn, sql,new BeanHandler<Person>(Person.class),params); // 關(guān)閉連接 JdbcUtil.closeConn(conn); // 返回 return person; } @Override public List<Person> findAllPersons() throws Exception { // 創(chuàng)建方法的返回值 List<Person> plist = null; // 獲取數(shù)據(jù)庫(kù)連接 Connection conn = JdbcUtil.getConn(); // 創(chuàng)建SQL執(zhí)行對(duì)象 QueryRunner qr = new QueryRunner(); // 編寫(xiě)SQL語(yǔ)句 String sql = "select * from person"; // 占位符賦值 // 執(zhí)行 plist = qr.query(conn,sql,new BeanListHandler<Person>(Person.class)); // 關(guān)閉 spring 偉大的程序 JdbcUtil.closeConn(conn); // 返回 return plist; }}
這樣DAO層開(kāi)發(fā)就完成了,其實(shí)我們可以看到就是這樣分層開(kāi)發(fā)更加規(guī)范更加符合面向?qū)ο?思想,但是其復(fù)雜難度也隨之增加,這也是分層開(kāi)發(fā)不好的一點(diǎn)?package com.hnxy.service;import java.util.List;import com.hnxy.entity.Person;/** * 人員表的Service層接口 * @author My * */public interface PersonService { /** * 添加方法 * @param person 要添加的對(duì)象 * @return 返回1 代表添加成功 返回0 代表添加失敗 * @throws Exception 聲明一個(gè)我也不知道會(huì)報(bào)什么錯(cuò)的異常 */ public int insertPerson(Person person)throws Exception; /** * 更新方法 * @param person 要更新的對(duì)象 * @return 返回1 代表更新成功 返回0 代表更新失敗 * @throws Exception 聲明一個(gè)我也不知道會(huì)報(bào)什么錯(cuò)的異常 */ public int updatePerson(Person person)throws Exception; /** * 刪除方法 * @param person 要?jiǎng)h除的對(duì)象 * @return 返回1 刪除成功 返回0 刪除失敗 * @throws Exception 聲明一個(gè)我也不知道會(huì)報(bào)什么錯(cuò)的異常 */ public int deletePerson(Person person)throws Exception; /** * 根據(jù)主鍵ID查詢信息 * @param id 要查詢的數(shù)據(jù)ID * @return 查詢到了 返回對(duì)象 沒(méi)有查詢到 返回null * @throws Exception 聲明一個(gè)我也不知道會(huì)報(bào)什么錯(cuò)的異常 */ public Person findPersonByID(Integer id)throws Exception; /** * 查詢?nèi)?/span> * @return 查詢到了返回對(duì)象list 沒(méi)有查詢到返回null * @throws Exception 聲明一個(gè)我也不知道會(huì)報(bào)什么錯(cuò)的異常 */ public List<Person> findAllPersons()throws Exception;}
實(shí)現(xiàn)類(lèi)的編寫(xiě): package com.hnxy.service.impl;import java.util.List;import com.hnxy.dao.PersonDAO;import com.hnxy.dao.impl.PersonDAOImpl;import com.hnxy.entity.Person;import com.hnxy.service.PersonService;/** * 業(yè)務(wù)層實(shí)現(xiàn)類(lèi) 主要做業(yè)務(wù)操作(針對(duì)客戶的不同需求,然后調(diào)用DAO層完成數(shù)據(jù)封裝) * * @author My * */public class PersonServiceImpl implements PersonService { // 創(chuàng)建DAO層的對(duì)象 private PersonDAO personDAO = new PersonDAOImpl(); @Override public int insertPerson(Person person) throws Exception { return personDAO.insertPerson(person); } @Override public int updatePerson(Person person) throws Exception { return personDAO.updatePerson(person); } @Override public int deletePerson(Person person) throws Exception { return personDAO.deletePerson(person); } @Override public Person findPersonByID(Integer id) throws Exception { return personDAO.findPersonByID(id); } @Override public List<Person> findAllPersons() throws Exception { return personDAO.findAllPersons(); } }
3. 頁(yè)面控制器整理 <%@page import="com.hnxy.entity.Person"%><%@page import="com.hnxy.service.impl.PersonServiceImpl"%><%@page import="com.hnxy.service.PersonService"%><%@page import="org.apache.commons.dbutils.handlers.BeanListHandler"%><%@page import="java.util.List"%><%@page import="org.apache.commons.dbutils.QueryRunner"%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><% // 創(chuàng)建service層對(duì)象 PersonService service = new PersonServiceImpl(); // 執(zhí)行 查詢 肯定獲取一個(gè)model List<Person> persons = service.findAllPersons(); // 保存模型 給 下一個(gè)頁(yè)面展示 request.setAttribute("plist", persons); // 轉(zhuǎn)發(fā) request.getRequestDispatcher("findAll_view.jsp").forward(request, response);%>
2) 按ID查詢的控制器<%@page import="com.hnxy.entity.Person"%><%@page import="com.hnxy.service.impl.PersonServiceImpl"%><%@page import="com.hnxy.service.PersonService"%><%@page import="org.apache.commons.dbutils.handlers.BeanHandler"%><%@page import="org.apache.commons.dbutils.QueryRunner"%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><% // 刪除業(yè)務(wù) // 第一階段 接收頁(yè)面數(shù)據(jù) // 1. 設(shè)定編碼 request.setCharacterEncoding("UTF-8"); // 2. 接收數(shù)據(jù) String pid = request.getParameter("pid"); // 第二階段 數(shù)據(jù)庫(kù)處理 PersonService service = new PersonServiceImpl(); Person person = service.findPersonByID(Integer.parseInt(pid)); // 第三階段 服務(wù)器響應(yīng) // 保存要更新的信息 request.setAttribute("person", person); // 轉(zhuǎn)發(fā)給下一個(gè)頁(yè)面展示person request.getRequestDispatcher("update.jsp").forward(request, response);%>
3) 添加的控制器 <%@page import="com.hnxy.entity.Person"%><%@page import="com.hnxy.service.impl.PersonServiceImpl"%><%@page import="com.hnxy.service.PersonService"%><%@page import="org.apache.commons.dbutils.QueryRunner"%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><% // 添加業(yè)務(wù) // 第一階段 接收數(shù)據(jù) // 1. 設(shè)定頁(yè)面請(qǐng)求字符流的編碼 request.setCharacterEncoding("UTF-8"); // 2. 接收頁(yè)面數(shù)據(jù) String pname = request.getParameter("pname"); String psex = request.getParameter("psex"); String age = request.getParameter("age"); String pfrom = request.getParameter("pfrom"); // 第二階段數(shù)據(jù)庫(kù)處理階段 // 封裝數(shù)據(jù) Person person = new Person(); person.setAge(Integer.parseInt(age)); person.setName(pname); person.setFrom(pfrom); person.setSex(psex); // 創(chuàng)建業(yè)務(wù)層對(duì)象 PersonService service = new PersonServiceImpl(); int count = service.insertPerson(person); // 第三階段 根據(jù)數(shù)據(jù)庫(kù)的處理結(jié)果 進(jìn)行相應(yīng)結(jié)果處理 if(count > 0){ // 添加成功 // 重新定向到查詢?nèi)康捻?yè)面 response.sendRedirect("findAll_server.jsp"); }else{ // 添加失敗 response 響應(yīng)流 String msg = "<script type='text/javascript'>history.go(-1);alert('添加失敗!');</script>"; // 服務(wù)器想給客戶端發(fā)送一段代碼 讓客戶端知道添加失敗了 // 你這段文字到底是什么類(lèi)型的? text/html 那么瀏覽器就把你的這段話當(dāng)做是HTML文本進(jìn)行解析 response.setContentType("text/html; charset=UTF-8"); // 服務(wù)端給客戶端發(fā)送的數(shù)據(jù)類(lèi)型是什么 // 打印回去 out.print(msg); }%>
4) 更新的控制器<%@page import="com.hnxy.service.impl.PersonServiceImpl"%><%@page import="com.hnxy.service.PersonService"%><%@page import="com.hnxy.entity.Person"%><%@page import="org.apache.commons.dbutils.QueryRunner"%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><% // 添加業(yè)務(wù) // 第一階段 接收數(shù)據(jù) // 1. 設(shè)定頁(yè)面請(qǐng)求字符流的編碼 request.setCharacterEncoding("UTF-8"); // 2. 接收頁(yè)面數(shù)據(jù) String pname = request.getParameter("pname"); String psex = request.getParameter("psex"); String age = request.getParameter("age"); String pfrom = request.getParameter("pfrom"); String pid = request.getParameter("pid"); // 第二階段數(shù)據(jù)庫(kù)處理階段 // 封裝數(shù)據(jù) // 創(chuàng)建業(yè)務(wù)層對(duì)象 PersonService service = new PersonServiceImpl(); Person person = service.findPersonByID(Integer.parseInt(pid)); if (null != person) { person.setAge(Integer.parseInt(age)); person.setFrom(pfrom); person.setId(Integer.parseInt(pid)); person.setName(pname); person.setSex(psex); int count = service.updatePerson(person); // 第三階段 根據(jù)數(shù)據(jù)庫(kù)的處理結(jié)果 進(jìn)行相應(yīng)結(jié)果處理 if (count > 0) { // 添加成功 // 重新定向到查詢?nèi)康捻?yè)面 response.sendRedirect("findAll_server.jsp"); } else { // 添加失敗 response 響應(yīng)流 String msg = "<script type='text/javascript'>history.go(-1);alert('更新失敗!');</script>"; // 服務(wù)器想給客戶端發(fā)送一段代碼 讓客戶端知道添加失敗了 // 你這段文字到底是什么類(lèi)型的? text/html 那么瀏覽器就把你的這段話當(dāng)做是HTML文本進(jìn)行解析 response.setContentType("text/html; charset=UTF-8"); // 服務(wù)端給客戶端發(fā)送的數(shù)據(jù)類(lèi)型是什么 // 打印回去 out.print(msg); } } else { // 添加失敗 response 響應(yīng)流 String msg = "<script type='text/javascript'>history.go(-1);alert('沒(méi)有查詢到要更新的數(shù)據(jù)!');</script>"; // 服務(wù)器想給客戶端發(fā)送一段代碼 讓客戶端知道添加失敗了 // 你這段文字到底是什么類(lèi)型的? text/html 那么瀏覽器就把你的這段話當(dāng)做是HTML文本進(jìn)行解析 response.setContentType("text/html; charset=UTF-8"); // 服務(wù)端給客戶端發(fā)送的數(shù)據(jù)類(lèi)型是什么 // 打印回去 out.print(msg); }%>
5) 刪除的控制器 <%@page import="com.hnxy.service.impl.PersonServiceImpl"%><%@page import="com.hnxy.service.PersonService"%><%@page import="com.hnxy.entity.Person"%><%@page import="org.apache.commons.dbutils.QueryRunner"%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><% // 添加業(yè)務(wù) // 第一階段 接收數(shù)據(jù) // 1. 設(shè)定頁(yè)面請(qǐng)求字符流的編碼 request.setCharacterEncoding("UTF-8"); // 2. 接收頁(yè)面數(shù)據(jù) String pid = request.getParameter("pid"); // 第二階段數(shù)據(jù)庫(kù)處理階段 // 封裝數(shù)據(jù) // 創(chuàng)建業(yè)務(wù)層對(duì)象 PersonService service = new PersonServiceImpl(); Person person = service.findPersonByID(Integer.parseInt(pid)); if (null != person) { int count = service.deletePerson(person); // 第三階段 根據(jù)數(shù)據(jù)庫(kù)的處理結(jié)果 進(jìn)行相應(yīng)結(jié)果處理 if (count > 0) { // 添加成功 // 重新定向到查詢?nèi)康捻?yè)面 response.sendRedirect("findAll_server.jsp"); } else { // 添加失敗 response 響應(yīng)流 String msg = "<script type='text/javascript'>history.go(-1);alert('刪除失敗!');</script>"; // 服務(wù)器想給客戶端發(fā)送一段代碼 讓客戶端知道添加失敗了 // 你這段文字到底是什么類(lèi)型的? text/html 那么瀏覽器就把你的這段話當(dāng)做是HTML文本進(jìn)行解析 response.setContentType("text/html; charset=UTF-8"); // 服務(wù)端給客戶端發(fā)送的數(shù)據(jù)類(lèi)型是什么 // 打印回去 out.print(msg); } } else { // 添加失敗 response 響應(yīng)流 String msg = "<script type='text/javascript'>history.go(-1);alert('沒(méi)有查詢到要?jiǎng)h除的數(shù)據(jù)!');</script>"; // 服務(wù)器想給客戶端發(fā)送一段代碼 讓客戶端知道添加失敗了 // 你這段文字到底是什么類(lèi)型的? text/html 那么瀏覽器就把你的這段話當(dāng)做是HTML文本進(jìn)行解析 response.setContentType("text/html; charset=UTF-8"); // 服務(wù)端給客戶端發(fā)送的數(shù)據(jù)類(lèi)型是什么 // 打印回去 out.print(msg); }%>
關(guān)鍵詞:實(shí)戰(zhàn),基礎(chǔ),使用,技術(shù),入門(mén),數(shù)據(jù),深入,動(dòng)態(tài)
客戶&案例
營(yíng)銷(xiāo)資訊
關(guān)于我們
客戶&案例
營(yíng)銷(xiāo)資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。