ในอนาคตอันใกล้ บริษัท จะพัฒนาโครงการใหม่และใช้กรอบ Struts2+MyBatis+Spring ดังนั้นหลังจากเรียนรู้แล้วฉันจะโพสต์ไว้ในบล็อกของฉันหวังว่าจะช่วยคุณ!
ส่วนใหญ่ตระหนักถึงการเพิ่มผู้ใช้การลบการปรับเปลี่ยนและการดำเนินการค้นหา
1. นำเข้าแพ็คเกจ JAR ที่เกี่ยวข้อง
2. การกำหนดค่า web.xml ส่วนใหญ่กำหนดค่า struts2 และฤดูใบไม้ผลิ
เนื้อหาของไฟล์ web.xml มีดังนี้:
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://ww.w3.org/200 XSI: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/Javaee/WeB-App_2_2_2_2_2_2_2_2_ <welcome-file> index.jsp </welcome-file> </welcome-file-list> <!-การโหลดไฟล์การกำหนดค่าสปริง-> <ผู้ฟัง> <Sistener-class> org.springframework.web.context.contextloaderlistener </listener-class> </listener> <! <param-name> contextconfiglocation </param-name> <param-value> classpath: beans.xml </param-value> </context-param> <!-กำหนดค่า struts2-> <filter> org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecuteFilter </filter-class> </ตัวกรอง> <filter-mapping>
3. กำหนดค่าไฟล์การกำหนดค่าสปริงส่วนใหญ่รวมถึงการกำหนดค่าแหล่งข้อมูลการทำธุรกรรม myBaits ฯลฯ
ไฟล์การกำหนดค่า beans.xml มีดังนี้:
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http:/ XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework http://www.springframework.org/schema/aop http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://ww.springframework.org/schema/schema http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context ถั่วที่มีความคิดเห็น-> <บริบท: Annotation-config /> <!-กำหนดค่าแพ็คเกจที่จะสแกน-> <บริบท: Component-Scan base-package = "com.pdsu.edu"> < /บริบท: Component-Scan> <! proxy-target-class = "true" /> <!-ตำแหน่งไฟล์การกำหนดค่าฐานข้อมูล-> <บริบท: สถานที่ที่สถานที่ตั้งสถานที่ = "classpath: jdbc.properties" /> <!-กำหนดค่าแหล่งข้อมูล dbcp-> <bean id = "dataSource" destroy-method = "close"> < name = "url" value = "$ {jdbc.url}" /> <ชื่อคุณสมบัติ = "ชื่อผู้ใช้" value = "$ {jdbc.username}" /> <ชื่อคุณสมบัติ = "รหัสผ่าน" value = "$ {jdbc.password}" /> <! value = "$ {jdbc.minidle}"> </property> <!-เวลารอคอยสูงสุดในมิลลิวินาที-> <property name = "maxwait" value = "$ {jdbc.maxwait}"> </property> <! value = "$ {jdbc.initialsize}"> </property> </ebean> <!-กำหนดค่า mybitassqlsessionfactoryBean-> <bean id = "sqlsessionfactory"> <property name = "dataSource" ref = "DataSource"/> กำหนดค่า SQLSessionTemplate-> <bean id = "SQLSessionTemplate"> <constructor-arg name = "SQLSessionFactory" ref = "SQLSessionFactory" /> < /bean> <! การใช้คำอธิบายประกอบคำอธิบายประกอบ-> <tx: Transaction-Driven Transaction-Manager = "TransactionManager"/> </ebeans> 4. รายละเอียดไฟล์การกำหนดค่า JDBC
jdbc.DriverClassName = com.mysql.jdbc.driver jdbc.url = jdbc: mysql: // localhost: 3306/Operationlog jdbc.username = root jdbc.password = jdbc.maxactive = 2 jdbc.maxidle = 5 jdbc. = 3000
5. กำหนดค่าไฟล์การกำหนดค่าหลัก MyBatis:
<? XML เวอร์ชัน = "1.0" การเข้ารหัส = "UTF-8"?> <! การกำหนดค่า doctype สาธารณะ "-// mybatis.org//dtd config 3.0 // en" "http://mybatis.org/dtd/mybatis-3-configig.dtd" type = "com.pdsu.edu.domain.user"/> </typealiases> <mappers> <mapper resource = "com/pdsu/edu/domain/sqlmappers/user.xml"/> </mappers>
6. กำหนดค่าไฟล์ user.xml
<? xml เวอร์ชัน = "1.0" การเข้ารหัส = "utf-8"?> <! doctype mapper สาธารณะ "-// mybatis.org//dtd mapper 3.0 // en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" <resultmap type = "com.pdsu.edu.domain.user" id = "userresult"> <result property = "id" คอลัมน์ = "id" jdbctype = "จำนวนเต็ม" javatype = "java.lang.integer" /> id = "userlogin" parameterType = "user" resultmap = "userresult"> เลือก * จากผู้ใช้ที่ชื่อผู้ใช้ =#{ชื่อผู้ใช้} และรหัสผ่าน =#{รหัสผ่าน} </select> <select id = "selectallUser" paramethAp = "userResult"> select * เลือก * จากผู้ใช้โดยที่ id =#{id} </select> <insert id = "InsertUser" parameterType = "ผู้ใช้"> <! [cdata [แทรกลงในผู้ใช้ (ชื่อผู้ใช้รหัสผ่าน) ค่า (#{ชื่อผู้ใช้},#{รหัสผ่าน})] userName =#{ชื่อผู้ใช้}, password =#{รหัสผ่าน} โดยที่ id =#{id} </update> <delete id = "deleteUser" parameterType = "int"> ลบจากผู้ใช้ที่ id =#{id} 7. วิธีการเขียนเอนทิตีผู้ใช้
ผู้ใช้ระดับสาธารณะใช้ serializable {ส่วนตัวคงที่สุดท้าย Long SerialVersionUID = -4415990281535582814L; ID จำนวนเต็มส่วนตัว; ชื่อผู้ใช้สตริงส่วนตัว; รหัสผ่านสตริงส่วนตัว จำนวนเต็มสาธารณะ getId () {return id; } โมฆะสาธารณะ setId (ID จำนวนเต็ม) {this.id = id; } สตริงสาธารณะ getUserName () {ส่งคืนชื่อผู้ใช้; } โมฆะสาธารณะ setUserName (ชื่อผู้ใช้สตริง) {this.userName = ชื่อผู้ใช้; } สตริงสาธารณะ getPassword () {ส่งคืนรหัสผ่าน; } โมฆะสาธารณะ setPassword (รหัสผ่านสตริง) {this.password = รหัสผ่าน; } @Override สตริงสาธารณะ toString () {return "ผู้ใช้ [id =" + id + ", รหัสผ่าน =" + รหัสผ่าน + ", ชื่อผู้ใช้ =" + ชื่อผู้ใช้ + "]"; } @Override สาธารณะ int hashCode () {สุดท้าย int prime = 31; int ผลลัพธ์ = 1; result = prime * result + ((id == null)? 0: id.hashCode ()); ผลการกลับมา; } @Override บูลีนสาธารณะเท่ากับ (Object obj) {ถ้า (this == obj) คืนค่าจริง; ถ้า (obj == null) ส่งคืนเท็จ; ถ้า (getClass ()! = obj.getClass ()) ส่งคืนเท็จ; ผู้ใช้อื่น ๆ = (ผู้ใช้) obj; if (id == null) {ถ้า (อื่น ๆ . id.id! = null) ส่งคืน false; } อื่นถ้า (! id.equals (อื่น ๆ )) ส่งคืน false; กลับมาจริง; - 8. วิธีการเขียน userdao
อินเทอร์เฟซสาธารณะ userdao {public abstract void insertuser (ผู้ใช้ผู้ใช้); บทคัดย่อ Public Void UpdateUser (ผู้ใช้ผู้ใช้); บทคัดย่อสาธารณะเป็นโมฆะ DELETEUSER (จำนวนเต็ม USERID); ผู้ใช้นามธรรมสาธารณะ FindUserByID (จำนวนเต็ม USERID); รายการนามธรรมสาธารณะ <user> findall (); Userlogin ผู้ใช้นามธรรมสาธารณะ (ผู้ใช้); - 9. การใช้งาน userdao
@Repository คลาสสาธารณะ UserDaoImpl ใช้ UserDao {สตริงสุดท้ายส่วนตัว insert_user = "InsertUser"; สตริงสุดท้ายส่วนตัว update_user = "updateUser"; สตริงสุดท้ายส่วนตัว delete_user = "deleteuser"; สตริงสุดท้ายส่วนตัว find_user_byid = "finduserbyid"; สตริงสุดท้ายส่วนตัว select_all_user = "selectalluser"; สตริงสุดท้ายส่วนตัว user_login = "userlogin"; @autowired Private SQLSessionTemplate SQLSessionTemplate; โมฆะสาธารณะ InsertUser (ผู้ใช้ผู้ใช้) {SQLSessionTemplate.Insert (insert_user, ผู้ใช้); } public void updateUser (ผู้ใช้ผู้ใช้) {SQLSessionTemplate.Update (update_user, ผู้ใช้); } โมฆะสาธารณะ DELETEUSER (จำนวนเต็ม USERID) {SQLSessionTemplate.delete (DELETE_USER, USERID); } ผู้ใช้ Public FindUserById (จำนวนผู้ใช้จำนวนเต็ม) {ส่งคืน sqlsessionTemplate.selectone (find_user_byid, userId); } รายการสาธารณะ <user> findall () {return sqlsessionTemplate.selectList (select_all_user); } ผู้ใช้สาธารณะ UserLogin (ผู้ใช้ผู้ใช้) {ส่งคืน SQLSessionTemplate.SelecTone (user_login, ผู้ใช้); - 10. อินเตอร์เฟสผู้ใช้บริการ
ผู้ใช้งานสาธารณะผู้ใช้งาน {// เพิ่ม Void Adduser นามธรรมสาธารณะ (ผู้ใช้); บทคัดย่อ Public Void UpdateUser (ผู้ใช้ผู้ใช้); บทคัดย่อสาธารณะเป็นโมฆะ DELETEUSER (จำนวนเต็ม USERID); ผู้ใช้นามธรรมสาธารณะ FindUserByID (จำนวนเต็ม USERID); รายการบทคัดย่อสาธารณะ <user> findalluser (); ล็อกอินผู้ใช้นามธรรมสาธารณะ (ผู้ใช้ผู้ใช้); - 11. การใช้งานอินเทอร์เฟซ Userservice
@Service @Transactional คลาสสาธารณะ UserserViceImpl ใช้ Userservice {@AutoWired UserDao UserDao; // เพิ่มโมฆะสาธารณะผู้ใช้ Adduser (ผู้ใช้ผู้ใช้) {userDao.InsertUser (ผู้ใช้); } // อัปเดตผู้ใช้โมฆะสาธารณะ UpdateUser (ผู้ใช้ผู้ใช้) {userDao.UpDateUser (ผู้ใช้); } โมฆะสาธารณะ DELETEUSER (จำนวนเต็ม USERID) {UserDao.DeleteUser (USERID); } ผู้ใช้ Public FindUserById (Integer UserID) {return userdao.finduserById (userId); } รายการสาธารณะ <user> findalluser () {return userdao.findall (); } การเข้าสู่ระบบของผู้ใช้สาธารณะ (ผู้ใช้ผู้ใช้) {return userdao.userlogin (ผู้ใช้); - 12. กำหนดค่า struts2
<? XML เวอร์ชัน = "1.0" การเข้ารหัส = "UTF-8"?> <! Doctype struts สาธารณะ "-// Apache Software Foundation // dtd struts การกำหนดค่า 2.1 // en" "http://struts.apache.org/dtds/struts-2.1.dtd" value = "UTF-8"/> <!-ระบุชุดการเข้ารหัสเริ่มต้นซึ่งใช้กับ setCharacterencoding () และ freemarker ของ httpservletrequest และเอาต์พุตของ vilocity-> <constant name = "struts.configuration name = "struts.devmode" value = "true"/> <!-พิมพ์ข้อมูลข้อผิดพลาดโดยละเอียดในโหมดการพัฒนา-> <constant name = "struts.ui.theme" value = "xhtml"/> <package name = "ผู้ใช้" namespace = "/ผู้ใช้" type = "redirectaction"> user_queryalluser.action </result> <name result = "input">/index.jsp </result> <name result = "userlist">/userlist.jsp </result> <result name = "adduser">/useradd.jsp </results>
13. การใช้งานเฉพาะผู้ใช้งาน
@Controller @Scope ("Prototype") ผู้ใช้ระดับสาธารณะขยายการดำเนินการ actionsupport {@autoWired Userservice Userservice; ผู้ใช้ส่วนตัว รายการส่วนตัว <user> userlist; สตริงสาธารณะ execute () โยนข้อยกเว้น {return null; } การเข้าสู่ระบบสตริงสาธารณะ () {if (user! = null) {user user2 = userservice.login (ผู้ใช้); if (user2! = null) {return success; }} this.addfielderror ("user.username", "ข้อผิดพลาดในชื่อผู้ใช้หรือรหัสผ่าน!"); ส่งคืนอินพุต; } สตริงสาธารณะ addui () {return "adduser"; } สตริงสาธารณะ updateUi () {user = userservice.finduserById (user.getId ()); return "UpdateUser"; } สตริงสาธารณะเพิ่ม () {userservice.adduser (ผู้ใช้); กลับมาประสบความสำเร็จ } Public String DELETE () {UsersERVICE.DELETEUSER (user.getId ()); กลับมาประสบความสำเร็จ } การอัปเดตสตริงสาธารณะ () {userservice.updateUser (ผู้ใช้); กลับมาประสบความสำเร็จ } ผู้ใช้สาธารณะ getUser () {return user; } โมฆะสาธารณะ setUser (ผู้ใช้ผู้ใช้) {this.user = ผู้ใช้; } สตริงสาธารณะ queryalluser () {userlist = userservice.findalluser (); ส่งคืน "ผู้ใช้"; } รายการสาธารณะ <user> getUserList () {return userlist; } โมฆะสาธารณะ setUserList (รายการ <user> userlist) {this.userlist = userlist; - 14. การใช้งานหน้าเข้าสู่ระบบ
<%@ page language = "java" import = "java.util.*" pageencoding = "utf-8"%> <%@ taglib prefix = "s" uri = "/struts-tags"%> String basepath = request.getScheme ()+": //"+request.getServerName ()+":"+request.getServerport ()+path+"/"; %> <! doctype html public "-// w3c // dtd html 4.01 transitional // en"> <html> <head> <base href = "<%= basepath%>"> <title> ผู้ใช้เข้าสู่ระบบ </title> <meta http-equiv = "pragma" http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "Expires" content = "0"> <meta http-equiv = "คำหลัก" content = "keyword1, คำหลัก 2, คำหลัก type = "text/css" href = "styles.css"> -> <s: head/> </head> <body> <center> <H1> ผู้ใช้เข้าสู่ระบบ </h1> <s: a action = "user_addui" namespace = "ผู้ใช้" <s: textfield label = "username" name = "user.username"> </s: textfield> <s: รหัสผ่าน label = "รหัสผ่าน" name = "user.password"> </s: รหัสผ่าน> <s: ส่งค่า = "ล็อกอิน"> </s: ส่ง>
15. เพิ่มหน้า
<%@ page language = "java" import = "java.util.*" pageencoding = "utf-8"%> <%@ taglib คำนำหน้า = "s" uri = "/struts-tags"%> <! ผู้ใช้ </title> <meta http-equiv = "pragma" content = "no-cache"> <meta http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "หมดอายุ" เนื้อหา = "0"> </head> namespace = "/user" method = "post"> <s: textfield label = "username" name = "user.username"> </s: textfield> <s: รหัสผ่าน label = "รหัสผ่าน" ชื่อ = "user.password"> </s: รหัสผ่าน>
16. แก้ไขหน้า
<%@ page language = "java" import = "java.util.*" pageencoding = "utf-8"%> <%@ taglib prefix = "s" uri = "/struts-tags"%> <! ผู้ใช้ </title> <meta http-equiv = "pragma" content = "no-cache"> <meta http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "Expires" content = "0"> </head> namespace = "/user" method = "post"> <s: hidden name = "user.id"> </s: hidden> <s: textfield label = "username" name = "user.username"> </s: textfield> <s: password label = "รหัสผ่าน" </html>
17. หน้ารายการ
<%@ page language = "java" import = "java.util.*" pageencoding = "utf-8"%> <%@ taglib คำนำหน้า = "s" uri = "/struts-tags"%> <! รายการ </title> <meta http-equiv = "pragma" content = "no-cache"> <meta http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "หมดอายุ" เนื้อหา = "0"> </head> namespace = "/user"> เพิ่มผู้ใช้ใหม่ </s: a> </h3> <table> <tr> <th> รหัสผู้ใช้ </th> <th> ชื่อผู้ใช้ </th> <th> รหัสผ่านผู้ใช้ </th> <th> การดำเนินการ </th> </tr> value = "ชื่อผู้ใช้"/> </td> <td> <s: ค่าคุณสมบัติ = "รหัสผ่าน"/> </td> <td> <s: a action = "user_updateui" namespace = "/user"> <s: param = "user.id"> $ {id} namespace = "/user"> <s: param name = "user.id"> $ {id} </s: param> delete </s: a> </td> </tr> </s: iterator> </table> </enter> </body> </html>ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น