เมื่อเร็ว ๆ นี้ระบบใน บริษัท พัฒนาถูกสร้างขึ้นด้วยกรอบ SSM แน่นอนว่ามันแตกต่างจากบล็อกในครั้งนี้ มีไฟล์การกำหนดค่ามากมาย ไฟล์การกำหนดค่าที่จำเป็นสำหรับการพัฒนาระดับองค์กรนั้นยุ่งยากมาก วันนี้ฉันจะบันทึกการสร้างกรอบ SSM อย่างง่ายและการดำเนินงานของการใช้ CRUD
เราใช้ปลั๊กอิน Maven เพื่อกำหนดค่าแพ็คเกจ JAR ที่เราต้องการ เนื่องจากมีการดำเนินการไม่มากนักเราจึงไม่ได้กำหนดค่ามากนัก เราควรให้ความสนใจกับเวอร์ชันของ JDK ที่คุณใช้และเลือก JDK ด้วยหมายเลขเวอร์ชันที่แตกต่างกัน
<Project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xsi: schemalocation = "http:/MANCMMMMMMMMMMMMMMMMMMMMMMMMMMBM http://maven.apache.org/maven-v4_0_0.xsd "> <modersion> 4.0.0 </modelversion> <roupid> com.lr </groupid> <ratifactid> ssm </artifactid> <url> http://maven.apache.org </url> <!-ใช้ในการตั้งค่าหมายเลขเวอร์ชัน-> <properties> <srping.version> 4.0.2.release </srping.version> <mybatis.version> <log4j.version> 1.2.17 </log4j.version> </properties> <!-ใช้แพ็คเกจ Jar-> <การพึ่งพา> <!-การทดสอบหน่วย-> <pendency> <roupid> junit </groupid> </predency> <!-แพ็คเกจ Java EE-> <การพึ่งพา> <roupId> javax </groupId> <ratifactid> javaee-api </artifactid> <persion> 7.0 </เวอร์ชัน> </percterency> <! <RoupID> com.fasterxml.jackson.core </groupId> <ratifactid> Jackson-Databind </artifactid> <sersion> 2.8.8 </เวอร์ชัน> </predency> <! <Sersion> $ {SRPING.VERSION} </เวอร์ชัน> </การพึ่งพา> <การพึ่งพา> <loupId> org.springframework </groupId> <ratifactid> Spring-core </artifactid> <version> $ {srping.version} <ArtIfactId> Spring-Oxm </artifactId> <persion> $ {srping.version} </เวอร์ชัน> </การพึ่งพา> <การพึ่งพา> <sdependency> <sdependency> <sdependency> <RoupID> org.springframework </groupId> <ratifactId> Spring-Jdbc </artifactid> <version> $ {srping.version} </เวอร์ชัน> </dercendency> <Sersion> $ {SRPING.VERSION} </เวอร์ชัน> </การพึ่งพา> <การพึ่งพา> <loupId> org.springframework </groupId> <ratifactId> Spring-context </artifactid> <version> $ {srping.version} </เวอร์ชัน> <ArtIfactId> Spring-Context-Support </artifactId> <persion> $ {srping.version} </เวอร์ชัน> </การพึ่งพา> <predency> <sdependency> <sderctiD> org.springframework </GroupId> <RoupID> org.springframework </groupId> <ratifactid> Spring-orm </artifactid> <persion> $ {srping.version} </version> </การพึ่งพา> <การพึ่งพา> <loupid> org.springframework </groupid> <ratifactid> </perctency> <การพึ่งพา> <roupId> org.springframework </groupId> <ratifactId> Spring-Webvc </artifactid> <sersion> $ {srping.version} </เวอร์ชัน> <Sersion> $ {SRPING.VERSION} </เวอร์ชัน> </การพึ่งพา> <!-แพ็คเกจสปริงเฟรมเวิร์กสิ้นสุด-> <!-แพ็คเกจ MyBatis Framework เริ่มต้น-> <การพึ่งพา> <roupId> org.mybatis </groupid> <RoupID> org.mybatis </groupId> <ratifactId> mybatis-spring </artifactiD> <cersion> 1.2.2 </เวอร์ชัน> </การพึ่งพา> <!-แพ็คเกจ MyBatis Framework สิ้นสุดลง-> <! <ArtIfactId> MySQL-Connector-Java </artifactId> <version> 5.1.35 </Side> </การพึ่งพา> <!-นำเข้าแพ็คเกจ DBCP JAR เพื่อกำหนดค่าฐานข้อมูลใน ApplicationContext.xml-> <pendency> </predency> <!-คลาสแท็ก JSTL-> <predency> <sderctId> JSTL </groupId> <ratifactId> jstl </artifactid> <persion> 1.2 </Sident> </การพึ่งพา> <predency> <Ederency> <sderctId> log4j </groupId> <ratifactId> log4j </artifactId> <cersion> $ {log4j.version} </version> </การพึ่งพา> <predency> <roupId> org.slf4j </groupid> </permentency> <predency> <roupId> org.slf4j </groupId> <ratifactId> slf4j-log4j12 </artifactid> <sersion> $ {slf4j.version} </เวอร์ชัน> </การพึ่งพา> <! <roupId> com.alibaba </groupId> <ratifactid> fastjson </artifactid> <cersion> 1.2.6 </เวอร์ชัน> </การพึ่งพาอาศัย> <pendency> <sderncture> <roupid> org.codehaus.jackson </groupid> <ratifactid> -> <predency> <roupId> Commons-FilePileT </groupId> <ratifactId> Commons-FilePileLoad </ArtifactId> <persion> 1.3.1 </เวอร์ชัน> </การพึ่งพา> <การพึ่งพา> <roupid> Commons-io </groupId> <ratifactid> <RoupID> Commons-Codec </groupId> <ratifactId> Commons-Codec </artifactId> <version> 1.10 </version> </การพึ่งพาอาศัยกัน> <!-อัปโหลดแพ็คเกจส่วนประกอบ-> <! </perctency> <predency> <roupId> org.apache.poi </groupid> <ratifactid> poi </artifactid> <sersion> 3.8 </version> </การพึ่งพา> <การพึ่งพา> <roupid> org.apache.poi </groupid> <ratifactid> นอกจากนี้-> </การพึ่งพาอาศัย> <uild> <finalName> maven_project </finalname> <plugins> <sloupid> org.apache.maven.plugins </groupid> <ratifactid> maven-compiler-plugin </artifactid> </plugin> </plugins> </uffer> </ched>จากนั้นกำหนดค่าการเชื่อมต่อฐานข้อมูลและเปลี่ยนเป็นฐานข้อมูลของคุณเอง
driver = com.mysql.jdbc.driverurl = jdbc/: mysql/: // locahost/: 3306/dbusername = rootpassword = rootmaxactive = 20maxidle = 20minidle = 1maxwait = 60000
ไฟล์การกำหนดค่า Spring-dao.xml จะค้นหาคลาสภายใต้โดยอัตโนมัติ
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: p = "http://www.springframework.org/schema/p" xmlns: บริบท = "http://www.springframework.org/schema/mvc" xsi: schemalocation = "http:/ http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "> <! ถูกสแกนและ Mappers ภายใต้แพ็คเกจนี้จะถูกค้นหา สามารถระบุแพ็คเกจหลายแพ็คเกจคั่นด้วยเครื่องหมายจุลภาคหรือเครื่องหมายอัฒภาคระหว่างแพ็คเกจ -> <ชื่อคุณสมบัติ = "basePackage" value = "com.lr.dao"/> <property name = "sqlsessionfactory" ref = "sqlsessionfactory">
ไฟล์รวมสำหรับไฟล์กำหนดค่าสปริงและ mybatis
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: p = "http://www.springframework.org/schema/p" xmlns: บริบท = "http://www.springframework.org/schema/mvc" xsi: schemalocation = "http:/ http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "> <! id = "dataSource" destroy-method = "close"> <property name = "driverclassName" value = "com.mysql.jdbc.driver" /> <property name = "url" value = "jdbc: mysql: // localhost: 3306 /db? name = "password" value = "root" /> <!-เริ่มต้นขนาดการเชื่อมต่อ-> <property name = "ค่าเริ่มต้น" value = "3" /> <!-จำนวนสูงสุดของกลุ่มการเชื่อมต่อ-> <ชื่อคุณสมบัติ = "maxactive" value = "20" /> <! value = "20" /> <!-พูลการเชื่อมต่อฟรีขั้นต่ำ-> <ชื่อคุณสมบัติ = "minidle" value = "1" /> <!-รับเวลารอการเชื่อมต่อสูงสุด-> <property name = "maxwait" value = "60000" <property name = "dataSource" ref = "dataSource"/> <!-สแกนไฟล์ mapping.xml โดยอัตโนมัติ-> <property name = "mapperlocations" value = "classpath: com/lr/mapper/*. xml"> </property>
กำหนดค่าไฟล์สำหรับสิ่งต่างๆ
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: p = "http://www.springframework.org/schema/p" xmlns: context = "http://www.springframework.org/schema/aop" xmlns: tx = "http:/www.schema/aop xmlns: mvc = "http://www.springframework.org/schema/mvc" xsi: schemalocation = "http://www.springframework.org/schema/beans http:/www.springframework http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://ww.springframework http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://ww.springframework (การจัดการธุรกรรม) ผู้จัดการธุรกรรมใช้ jtatransactionmanager สำหรับ Global TX-> <Bean ID = "TransactionManager"> <property name = "DataSource" ref = "DataSource" /> </epean> <!-กำหนดค่าคลาสที่เข้าร่วมในการทำธุรกรรม com.lr.service.*.*(.. )) "/> <aop: ที่ปรึกษา pointcut-ref =" allservicemethod "คำแนะนำ-ref =" txadvice "/> </aop: config> <! name = "*" propagation = "จำเป็น" Rollback-for = "java.lang.exception"/> </tx: แอตทริบิวต์> </tx: คำแนะนำ> </ebeans>
กำหนดค่าไฟล์ SpringMvc.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: p = "http://www.springframework.org/schema/p" xmlns: บริบท = "http://www.springframework.org/schema/mvc" xsi: schemalocation = "http:/ http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "> <! <MVC: คำอธิบายประกอบที่ขับเคลื่อนด้วย/> <MVC: Default-Servlet-Handler/> <!-กำหนด pre-suffix ของไฟล์ที่กระโดด, การกำหนดค่าโหมดดู-> <bean> <property name = "คำนำหน้า" value = "/web-inf/jsp/"/> <property name = "Suffix"
ใช้ได้! - - ไฟล์ที่ต้องกำหนดค่าได้รับการกำหนดค่าเพียงพอสำหรับเราที่จะดำเนินการคลื่นการดำเนินงาน
ถัดไปคือรหัสบางส่วนในพื้นหลังซึ่งส่วนใหญ่รวมถึงคลาสเอนทิตีเลเยอร์ DAO เลเยอร์บริการและเลเยอร์คอนโทรลเลอร์ ฉันอัปโหลดไฟล์พื้นหลังเข้าด้วยกัน ไฟล์ mapper.xml เป็นส่วนที่สำคัญที่สุดของวิธีการใช้งาน
<? xml เวอร์ชัน = "1.0" การเข้ารหัส = "utf-8"?> <! doctype mapper สาธารณะ "-// mybatis.org//dtd mapper 3.0 // en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd <resultmap id = "baseresultmap" type = "com.lr.dto.user"> <result column = "id" คุณสมบัติ = "id" jdbctype = "จำนวนเต็ม" /> <result column = "name" property = "name" jdbcType = "Varcharcharchched" property = "age" jdbctype = "จำนวนเต็ม"/> </resultmap> <sql id = "base_column_list"> id, ชื่อ, รหัสผ่าน, อายุ </sql> <!-เพิ่มผู้ใช้-> <แทรก id = "adduser" parameterType = ค่า (#{ชื่อ},#{รหัสผ่าน},#{อายุ}) </แทรก> <!-ผู้ใช้การสืบค้น-> <เลือก id = "queryByPrimaryKey" resultMap = "baseresultMap" parameterType = "java.lang.integer"> เลือก ผู้ใช้-> <delete id = "deleteByPrimaryKey" parameterType = "java.lang.integer"> ลบจากผู้ใช้ที่ id = #{id} </ลบ> <! name =#{name}, password =#{password}, age =#{age} โดยที่ id =#{id} </update> <select id = "findalluser" resultType = "com.lr.dto.user"> เลือก * จากผู้ใช้ แพ็คเกจ com.lr.dao; นำเข้า java.util.list; นำเข้า com.lr.dto.user; อินเตอร์เฟสสาธารณะ iuserdao {// ผู้ใช้ Query ผู้ใช้ Public QueryByPrimaryKey (ID int); // ลบผู้ใช้สาธารณะ int deleteByPrimaryKey (int id); // อัปเดตผู้ใช้ INTAPTERBYPRIMARYKEY (ผู้ใช้) // เพิ่ม adduser int สาธารณะของผู้ใช้ (ผู้ใช้ผู้ใช้); // QUERY ผู้ใช้รายชื่อสาธารณะทั้งหมด <ผู้ใช้> FindAllUser ();} แพ็คเกจ com.lr.service; นำเข้า java.util.list; นำเข้า com.lr.dto.user; อินเตอร์เฟสสาธารณะ iuserService {// ผู้ใช้ Query ผู้ใช้สาธารณะ GetUserById (int userID); // ลบโมฆะสาธารณะ deleteuser (ID int); // อัปเดตโมฆะสาธารณะผู้ใช้อัพเดท (ผู้ใช้ผู้ใช้); // เพิ่ม Void Adduser ของผู้ใช้ (ผู้ใช้); // ดูรายชื่อผู้ใช้ทั้งหมด <ผู้ใช้> findalluser ();} แพ็คเกจ com.lr.service.impl; นำเข้า java.util.list; นำเข้า javax.annotation.resource; นำเข้า org.springframework.stereotype.service; นำเข้า com.lr.dao.iuserdao; นำเข้า com.lr.dto.user; UserserViceImpl ใช้ IUSERSERVICE {@Resource ส่วนตัว iUserdao UserDao; iuserdao สาธารณะ getuserdao () {return userdao; } โมฆะสาธารณะ setUserDao (iuserdao userdao) {this.userdao = userdao; } // ผู้ใช้ Query @Override ผู้ใช้สาธารณะ getUserById (int userId) {return userdao.QueryByPrimaryKey (USERID); } // อัปเดตผู้ใช้ @Override public void updateUser (ผู้ใช้ผู้ใช้) {userdao.updateByPrimaryKey (ผู้ใช้); } // ลบผู้ใช้ @Override โมฆะสาธารณะ deleteUser (ID int) {userDao.DeleteByPrimaryKey (ID); } // เพิ่มผู้ใช้ @Override public void adduser (ผู้ใช้ผู้ใช้) {userdao.adduser (ผู้ใช้); } // Query ผู้ใช้ทั้งหมด @Override รายการสาธารณะ <user> findalluser () {return userdao.findalluser (); - แพ็คเกจ com.lr.controller; นำเข้า java.util.list; นำเข้า Javax.servlet.http.httpservletrequest; นำเข้า org.springframework.beans.factory.annotation.AutoWired; นำเข้า org.springframework.stereotype.controller; org.springframework.web.bind.annotation.requestmapping; นำเข้า org.springframework.web.bind.annotation.responsebody; นำเข้า com.lr.dto.user; นำเข้า com.lr.service.iusservice; iuserservice สาธารณะ GetUserservice () {return userservice; } โมฆะสาธารณะ setUserservice (iUserService userservice) {this.userService = userservice; } // หน้าหลัก @requestmapping ("/") สตริงสาธารณะ usermgr () {return "showuser"; } // เพิ่มผู้ใช้ @RequestMapping ("/addUser") @ResponseBody โมฆะสาธารณะเป็นโมฆะ userAdd (ผู้ใช้ผู้ใช้) {UserserVice.Adduser (ผู้ใช้); } // ลบผู้ใช้ @RequestMapping ("/deleteUser") @ResponseBody โมฆะสาธารณะ DELETEUSER (ID int) {UsersERVICE.DELETEUSER (ID); } // แก้ไขผู้ใช้ @RequestMapping ("/updateUser") @ResponseBody โมฆะสาธารณะโมฆะ uploadteuser (ผู้ใช้ผู้ใช้) {userservice.updateUser (ผู้ใช้); } // ค้นหาผู้ใช้ตาม ID @RequestMapping ("/showUser") @ResponseBody Showuser ผู้ใช้สาธารณะ (ID int, รุ่นโมเดล) {return userservice.getUserById (ID); } // QUERY ผู้ใช้ทั้งหมด @RequestMapping ("/findAllUser") @ResponseBody รายชื่อสาธารณะ <user> findAlluser () {return userservice.findalluser (); - <%@ page language = "java" import = "java.util.*" pageencoding = "utf-8"%> <! doctype html> <html> <head> <meta charset = "utf-8"> <script type = "ข้อความ/javascript" src = " %>/js/jQuery-1.12.4.min.js "> </script> <script type =" text/javascript "> // เพิ่มผู้ใช้ $ (ฟังก์ชั่น () {$ ("#add "). on (" คลิก ", addnewuser);}) รหัสผ่าน var = $ .Trim ($ ("#txtPassword"). val ()); var age = $ .Trim ($ ("#txtage"). val ()); $ .post ("/ssm/adduser", {"ชื่อ": ชื่อ, "รหัสผ่าน": รหัสผ่าน, "อายุ": อายุ}, ฟังก์ชัน () {แจ้งเตือน ("เพิ่มสำเร็จ!")}); } // ลบผู้ใช้ $ (ฟังก์ชั่น () {$ ("#delete") on ("คลิก", deleteUser);}) ฟังก์ชั่น deleteUser () {var id = $. trim ($ ("#deleteId"). val ()); $ .get ("/ssm/deleteuser", {"id": id}, function () {alert ("ลบสำเร็จ!")}); } // Query ผู้ใช้ทั้งหมด $ (function () {$ ("#findalluser") คลิก (function () {$ .ajax ({type: "post", ประเภทข้อมูล: "json", url: "/ssm/findalluser", ความสำเร็จ: ฟังก์ชั่น (msg) {var str = " str+= "tr> <th>"+msg [i] .id+"</th> <th>"+msg [i] .name+"</th> <th>"+msg [i] // การค้นหาผู้ใช้ตาม ID $ (function () {$ ("#find"). คลิก (ฟังก์ชั่น () {$ .AJAX ({ประเภท: "โพสต์", ข้อมูล: {id: $ ("#findId"). val ()}, ประเภทข้อมูล: "JSON", url: "/ssm/showuser" str+= "<tr> <th>"+user.id+"</th> <th>"+user.name+"</th> <th>"+user.password+"</th> <th>"+user.age+"</th> <tr>" $ ("#update"). on ("คลิก", updateUser); การแจ้งเตือน ($. trim ($ ("#updateage"). val ())) var id = $. trim ($ ("#updateId"). val ()); ชื่อ var = $. trim ($ ("#updateMame"). val ()); รหัสผ่าน var = $. trim ($ ("#updatePassword"). val ()); var age = $. trim ($ ("#updateage"). val ()); $ .post ("/ssm/updateUser", {"id": id, "ชื่อ": ชื่อ, "รหัสผ่าน": รหัสผ่าน, "อายุ": อายุ}, function () {alert ("แก้ไขได้สำเร็จ!")}); } </script> <title> การจัดการผู้ใช้ </title> </head> <body> <div> <p> ชื่อ: <อินพุต type = "text" id = "txtName"> </p> <p> รหัสผ่าน: <อินพุตประเภท = "รหัสผ่าน" id = "txtpassword"> </p> <p> id = "เพิ่ม"> เพิ่ม </button> </p> </div> <hr style = "ความสูง: 1px; border: none; border-top: 1px dashed #0066cc;" /> <div> <p> ป้อน ID ผู้ใช้: <อินพุต type = "text" id = "deleteId"> </p> <p> <button id = "ลบ"> ลบ </button> </p> </div> <hr style = "ความสูง: 1px; border: none; /> <div> <p> <button id = "findalluser"> query ทั้งหมด </button> </p> </div> <div> <table> <thead id = "findall"> <tr> <th> id </th> <th> ชื่อ </th> <th> รหัสผ่าน </th> <th> Style = "ความสูง: 1px; ชายแดน: ไม่มี; /> <div> <p> ป้อน ID ผู้ใช้: <อินพุต type = "text" id = "findId"> </p> <p> <button id = "ค้นหา"> query </button> </p> </div> <div> <table> </div> <hr style = "ความสูง: 1px; ชายแดน: ไม่มี; /> <v> <p> ป้อน ID ผู้ใช้: <อินพุต type = "text" id = "updateId"> </p> <p> ป้อนชื่อผู้ใช้: <อินพุต type = "text" id = "updatename"> </p> <p> ป้อนรหัสผ่าน: <อินพุต type = "รหัสผ่าน" ID = "updatePassword"> </p> <p> id = "อัปเดต"> แก้ไข </button> </p> </div> </body> </html>