แนวคิดพื้นฐาน
1.1. ฤดูใบไม้ผลิ
ฤดูใบไม้ผลิเป็นเฟรมเวิร์กโอเพ่นซอร์สซึ่งเป็นกรอบการพัฒนา Java ที่มีน้ำหนักเบาซึ่งเกิดขึ้นในปี 2546 ซึ่งได้มาจากความคิดและต้นแบบบางอย่างที่อธิบายโดย Rod Johnson ในหนังสือของเขาผู้เชี่ยวชาญด้านการพัฒนาและการออกแบบ J2EE แบบตัวต่อตัว มันถูกสร้างขึ้นเพื่อจัดการกับความซับซ้อนของการพัฒนาแอปพลิเคชันระดับองค์กร ฤดูใบไม้ผลิใช้ Javabeans ขั้นพื้นฐานเพื่อทำสิ่งที่เป็นไปได้ก่อนหน้านี้โดย EJB อย่างไรก็ตามการใช้งานของสปริงไม่ได้ จำกัด อยู่ที่การพัฒนาฝั่งเซิร์ฟเวอร์ จากความเรียบง่ายความสามารถในการทดสอบและมุมมองการมีเพศสัมพันธ์แบบหลวมแอปพลิเคชัน Java ใด ๆ จะได้รับประโยชน์จากฤดูใบไม้ผลิ พูดง่ายๆคือสปริงคือการผกผันที่มีน้ำหนักเบาของการควบคุม (IOC) และกรอบคอนเทนเนอร์ที่มุ่งเน้น (AOP)
1.2. SpringMVC
Spring MVC เป็นผลิตภัณฑ์ที่ตามมาของ SpringFramework และได้รับการรวมเข้ากับการไหลของเว็บฤดูใบไม้ผลิ สปริง MVC แยกบทบาทของคอนโทรลเลอร์วัตถุโมเดล dispatchers และวัตถุตัวจัดการและการแยกนี้ทำให้ง่ายต่อการปรับแต่ง
1.3. mybatis
MyBatis เป็นโครงการโอเพ่นซอร์สของ Apache ในปี 2010 โครงการนี้ถูกย้ายจาก Apache Software Foundation เป็น Google Code และถูกเปลี่ยนชื่อ MyBatis MyBatis เป็นกรอบการคงอยู่ของจาวา Ibatis จัดเตรียมเฟรมเวิร์กเลเยอร์การคงอยู่รวมถึงแผนที่ SQL และวัตถุการเข้าถึงข้อมูล (DAO) MyBatis กำจัดการตั้งค่าด้วยตนเองของรหัส JDBC เกือบทั้งหมดและพารามิเตอร์และการดึงชุดผลลัพธ์ MyBatis ใช้ XML อย่างง่ายหรือคำอธิบายประกอบสำหรับการกำหนดค่าและการแมปดั้งเดิมเพื่อแมปอินเทอร์เฟซและ pojos ของ Java (วัตถุ Java เก่าธรรมดา) ลงในบันทึกในฐานข้อมูล
ขั้นตอนที่ 1: สร้างโครงการเว็บผ่าน Maven
ขั้นตอนที่ 2: นำเข้าไฟล์ POM ลงในแพ็คเกจ JAR
(1) pom.xml
ฉันคัดลอกไฟล์ POM ทั้งหมดและคุณต้องลบสิ่งที่เริ่มต้นบางอย่างและเก็บสำเนาที่คุณสร้างขึ้นด้วย POM ของคุณเอง
<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.test </groupid> <ratifactid> ssm </artifactid> <url> http://maven.apache.org </url> <properties> <!-หมายเลขเวอร์ชันสปริง-> <pring.version> 4.0.2.release </spring.version> <! <lf4j.version> 1.7.7 </slf4j.version> <log4j.version> 1.2.17 </log4j.version> </log4j <RoupID> org.springframework </groupId> <ratifactid> Spring-core </artifactid> <persion> $ {spring.version} </version> </perdency> <การพึ่งพา> <roupid> org.springframework </groupid> <การพึ่งพา> <roupId> org.springframework </groupId> <ratifactid> Spring-Oxm </artifactid> <cersion> $ {Spring.version} </sention> </การพึ่งพา> <predency> </perctency> <การพึ่งพา> <roupId> org.springframework </groupId> <ratifactId> Spring-jdbc </artifactid> <persion> $ {Spring.version} </Serve> </การพึ่งพา> <cerson> $ {spring.version} </version> </การพึ่งพา> <การพึ่งพา> <loupId> org.springframework </groupId> <ratifactid> Spring-Aop </artifactid> <Spring.spring.version} </เวอร์ชัน> <ArtIfactId> Spring-Context-Support </artifactId> <persion> $ {spring.version} </เวอร์ชัน> </การพึ่งพา> <การพึ่งพาอาศัย> <roupId> org.springframework </groupid> <RoupID> org.mybatis </groupId> <ratifactid> mybatis </artifactid> <cersion> $ {mybatis.version} </sention> </predency> <!-แพ็คเกจ mybatis/ฤดูใบไม้ผลิ-> <Sersion> 1.2.2 </Servive> </การพึ่งพา> <!-นำเข้าแพ็คเกจ java ee jar-> <การพึ่งพา> <roupId> Javax </groupId> <ratifactid> javaee-api </artifactid> <persion> 7.0 </เวอร์ชัน> <ArtIfactId> mysql-connector-java </artifactid> <persion> 5.1.30 </เวอร์ชัน> </การพึ่งพา> <!-นำเข้าแพ็คเกจ JAR ของ DBCP เพื่อกำหนดค่าฐานข้อมูลใน ApplicationContext.xml-> <Sersion> 1.2.2 </Servent> </perctency> <!-คลาสแท็ก JSTL-> <การพึ่งพา> <loupId> JSTL </groupId> <ArtIfactId> JSTL </artifactId> <cersion> 1.2 </เวอร์ชัน> </การพึ่งพา> <! <ArtIfactId> log4j </artifactId> <persion> $ {log4j.version} </เวอร์ชัน> </การพึ่งพา> <!-รูปแบบวัตถุเพื่ออำนวยความสะดวกในการบันทึกการบันทึก-> <การพึ่งพา <RoupID> org.slf4j </groupId> <ratifactId> slf4j-api </artifactid> <cersion> $ {slf4j.version} </เวอร์ชัน> </การพึ่งพา> <การพึ่งพา <Sersion> $ {SLF4J.Version} </Sident> </การพึ่งพา> <!-LOG End-> <!-อินพุต JSON-> <การพึ่งพา> <GroupId> org.codehaus.jackson </groupid> <ratifactid> การพึ่งพา Jackson-Mapper-Asl </artifactid> <RoupID> Commons-FileUpload </GroupID> <ratifactId> Commons-FileUpload </artifactId> <Sersion> 1.3.1 </เวอร์ชัน> </การพึ่งพาอาศัย> <การพึ่งพาอาศัย> <roupId> Commons-io </GroupId> <ratifactId> Commons-io </artifactid> <ARTIFACTID> Commons-Codec </artifactId> <version> 1.9 </เวอร์ชัน> </การพึ่งพาอาศัย> </pendencies> <uffer> <buildName> SSM </malName>ขั้นตอนที่ 3: ดูโครงสร้างโครงการโดยรวมให้ฉันอธิบายก่อนว่าฉันไม่มีสิ่งที่เกี่ยวข้องใด ๆ เกี่ยวกับ SpringMVC ที่นี่เพราะฉันรวม Spring-Mybatis เป็นครั้งแรกและเมื่อทดสอบว่ามันประสบความสำเร็จหรือไม่
ขั้นตอนที่ 4: สร้างคลาสรุ่น
ผู้ใช้ระดับสาธารณะ {ID INT ส่วนตัว; ชื่อสตริงส่วนตัว; รหัสผ่านสตริงส่วนตัว รหัสผ่านสตริงส่วนตัว 2; /*การให้วิธีการตั้งค่าและรับวิธี toString*/}ขั้นตอนที่ 5: สร้างฐานข้อมูลอินเทอร์เฟซ UserDao และไฟล์การแมป Mapper
(1) สร้างตารางที่ง่ายมาก
(2) อินเทอร์เฟซ UserDao
อินเทอร์เฟซสาธารณะ userDao {ผู้ใช้ findUserById (ผู้ใช้ผู้ใช้);}(3) uesrmapper.xml
<? xml เวอร์ชัน = "1.0" การเข้ารหัส = "utf-8"?> <! doctype mapperpublic "-// mybatis.org//dtd mapper 3.0 // en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd -> <mapper namespace = "com.ssm.dao.userdao"> <!-ข้อมูลผู้ใช้แบบสอบถามตาม ID-> <!-ID: ระบุคำสั่งที่ไม่ซ้ำกันซึ่งเหมือนกับชื่อเมธอดผู้ใช้#{}: แสดงถึงตัวยึดตำแหน่ง หากพารามิเตอร์ประเภทง่าย ๆ ถูกส่งผ่านใน #{} ชื่อใน #{} คือพารามิเตอร์โดยพลการ: ประเภทของพารามิเตอร์อินพุตซึ่งเหมือนกับพารามิเตอร์ในผลลัพธ์ userDaO () ผลลัพธ์: parameterTy.sUS.SUS resultType = "com.ssm.model.user"> เลือก * จากผู้ใช้ <โดยที่> <ถ้า test = "name! = null"> และ name = #{name} </ถ้า> <ถ้า test = "รหัสผ่าน! = null"> และรหัสผ่าน = #{รหัสผ่านขั้นตอนที่ 5: สร้าง Userservice และ UserserViceImpl
(1) ผู้ใช้บริการ
ผู้ใช้ส่วนต่อประสานสาธารณะ {ผู้ใช้สาธารณะ GetUserById (ผู้ใช้ผู้ใช้); -(2) UserserViceImpl
นำเข้า org.springframework.stereotype.service; นำเข้า com.ssm.dao.userdao; นำเข้า com.sm.model.user; นำเข้า com.ssm.service.userService; @Service ("ผู้ใช้") ผู้ใช้ระดับเอกชน ผู้ใช้สาธารณะ getUserById (ผู้ใช้ผู้ใช้) {ส่งคืน this.userdao.finduserById (ผู้ใช้); -ขั้นตอนที่ 6: สร้างไฟล์ JDBC และบันทึกบันทึก
(1) jdbc.properties
jdbc.driver = com.mysql.jdbc.driverjdbc.url = jdbc/: mysql/: // localhost/: 3306/test? useunicode = true && characterencoding = utf-8jdbc.username = rootjdbc.password = รูท
(2) log4j.properties
log4j.rootlogger = ข้อมูล, คอนโซล, ไฟล์ #define ปลายทางการบันทึกเป็นคอนโซล log4j.appender.console = org.apache.log4j.consoleAppender log4j.appender.console.target = system.out #can org.apache.log4j.patternlayout log4j.appender.console.layout.conversionPattern = [%c] -%m%n #เมื่อขนาดไฟล์ถึงขนาดที่ระบุไฟล์ใหม่จะถูกสร้างขึ้น log4j logs/ssm.log #define ขนาดไฟล์สูงสุด log4j.appender.file.maxFilesize = 10MB #OUTPUT ดังนั้นบันทึก หากคุณเปลี่ยนเป็นการดีบักมันหมายถึงบันทึกเอาต์พุตที่การดีบักหรือสูงกว่าระดับ log4j.appender.file.threshold = ทั้งหมด log4j.appender.file.layout = org.apache.log4j.patternlayout log4j.appender.layaUt.conversionpattern = [ hh/: mm/: ss}] [%c]%m%n
ขั้นตอนที่ 7: รวม Spring-mybatis.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 "> <! ไฟล์-> <bean id = "PropertyConfigurer"> <property name = "location" value = "classpath: jdbc.properties" /> </ebean> <!-2. พูลการเชื่อมต่อฐานข้อมูล-> <bean id = "dataSource" destroy-method = "ปิด"> value = "$ {jdbc.url}" /> <property name = "username" value = "$ {jdbc.username}" /> <property name = "รหัสผ่าน" value = "$ {jdbc.password}" /> </ebean> <! id = "SQLSessionFactory"> <!-ระบุการอ้างอิงพูลการเชื่อมต่อฐานข้อมูล-> <ชื่อคุณสมบัติ = "DataSource" ref = "DataSource"/> <!-การสแกนอัตโนมัติของไฟล์ mapping.xml-> <property name = "mapperlocations" value = "classpath: com/ssm/mapper สปริงจะค้นหาคลาสภายใต้มันโดยอัตโนมัติ-> <bean> <property name = "basepackage" value = "com.ssm.dao"/> <property name = "SQLSessionFactoryBeanName" value = "SQLSessionFactory"> </property> <property name = "DataSource" ref = "DataSource"/> </ebean> </epeans>ขั้นตอนที่ 8: สร้างคลาสทดสอบ
หลังจากขั้นตอนข้างต้น (log4j ไม่ตรงกันและไม่มีผล) เราได้เสร็จสิ้นการรวมของฤดูใบไม้ผลิและ mybatis เพื่อให้เราสามารถเขียนรหัสทดสอบเพื่อลองว่ามันประสบความสำเร็จหรือไม่
คลาสทดสอบถูกสร้างขึ้นใน SRC/Test/Java หากการทดสอบประสบความสำเร็จหมายความว่าฤดูใบไม้ผลิและ mybatis ได้รับการรวมเข้าด้วยกันแล้ว ข้อมูลเอาต์พุตถูกพิมพ์ไปยังคอนโซลโดยใช้ log4j
(1) คลาสทดสอบ testmybatis
แพ็คเกจ ssm; นำเข้า Javax.annotation.resource; นำเข้า org.apache.log4j.logger; นำเข้า org.junit.test; นำเข้า org.junit.runner.runwith; นำเข้า org.springframework.test.context.context.contextcontextonfiguration; org.springframework.test.context.junit4.springjunit4classrunner; นำเข้า com.ssm.model.user; นำเข้า com.ssm.service.userservice;/*ให้การทดสอบวิ่งในสภาพแวดล้อมการทดสอบฤดูใบไม้ผลิ*/@runwith โหลด*/ @contextConfiguration (locations = {"classpath: spring-mybatis.xml"}) คลาสสาธารณะ testmybatis {@resource (name = "userservice") ผู้ใช้ส่วนตัวผู้ใช้บริการ; @Test โมฆะสาธารณะ test1 () {ผู้ใช้ผู้ใช้ = ผู้ใช้ใหม่ (); user.setName ("Zhang San"); user.setPassword ("123"); ผู้ใช้ user1 = userservice.getUserById (ผู้ใช้); System.out.println (user1.toString ()); -ในที่สุด กุญแจสำคัญคือการดูผลลัพธ์พื้นหลังซึ่งเป็นเวลาที่จะเป็นพยานปาฏิหาริย์ หากวัตถุเอาต์พุตเป็นเอาต์พุตหมายความว่าการกำหนดค่าและการรวมของคุณสำเร็จ!
จากนั้นเราเริ่มรวม SpringMVC
ขั้นตอนที่ 9: กำหนดค่า 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 "> <! <บริบท: Component-Scan base-package = "com.ssm.controller"/> <!-หลีกเลี่ยงการดาวน์โหลดไฟล์เมื่อ IE ดำเนินการ ajax และกลับไปที่ json-> <bean id = "mappingjacksonhttpmessageconverter" </ebean> <!-เริ่มฟังก์ชั่นคำอธิบายประกอบ SpringMVC เพื่อทำการแม็พของคำขอและคำอธิบายประกอบที่เสร็จสมบูรณ์ pojos-> <bean> <property name = "MessageConverters"> <list> <ref Bean = "MappingJacksonhttpMessageConverter"/> <! ไฟล์, การกำหนดค่าโหมดดู-> <bean> <!-ความเข้าใจของฉันเกี่ยวกับการกำหนดค่าที่นี่คือคำนำหน้าโดยอัตโนมัติและคำต่อท้ายสตริงที่ส่งคืนโดยวิธีการของการกระทำที่ตามมาจะกลายเป็นที่อยู่ URL ที่มีอยู่-> <ชื่อคุณสมบัติ = "คำนำหน้า" value = "/web-inf/jsp/"/> <ชื่อคุณสมบัติ = "Suffix" หากคุณไม่ใช้การอัปโหลดไฟล์คุณไม่จำเป็นต้องกำหนดค่า แน่นอนถ้าคุณไม่ตรงกันคุณไม่จำเป็นต้องแนะนำแพ็คเกจการอัปโหลดคอมโพเนนต์ในไฟล์การกำหนดค่า-> <bean id = "MultipartResolver"> <!-การเข้ารหัสเริ่มต้น-> <property name = "defaultEncoding" value = "UTF-8" /> <! -> <property name = "MaxinMemorySize" value = "40960"/> </ebean> </ebeans>
ขั้นตอนที่ 10: กำหนดค่าไฟล์ web.xml
การแนะนำของ Spring-mybatis.xml และ servlet Spring-MVC ที่กำหนดค่าไว้สำหรับการรวม SSM การรวมกรอบสองเฟรมก่อนหน้านี้ไม่จำเป็นต้องมีการกำหนดค่าใด ๆ ที่นี่
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <web-app xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://java.sun.com/xml 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_0. แอปพลิเคชันเว็บที่สร้างขึ้น </display-name> <!-ไฟล์การกำหนดค่า Spring และ MyBatis-> <context-Param> <param-Name> ContextConfigLocation </param-name> <param-value> classpath: spring-mybatis.xml </param-value> <Tilter-class> org.springframework.web.filter.characterencodingFilter </filter-lass> <sync-upported> True </async-upported> <init-Param> <param-name> การเข้ารหัส </param-name> <side-name> encodingFilter </filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-ผู้ฟังฤดูใบไม้ผลิ-> <Sistener-Class> org.springframework.web.context.context.context.context.context.context.context.context.context.context.context.context.context.context.context <Sistener-class> org.springframework.web.util.introspectorCleanupListener </listener-class> </listener> <!-Spring MVC Servlet-> <servlet> <init-Param> <param-Name> contextConfigLocation </param-name> <param-value> classpath: springmvc.xml </param-value> </init-param> <load-on-startup> 1 </load-on-startup> <sync-supported> <servlet-name> springmvc </servlet-name> <!-ที่นี่คุณสามารถกำหนดค่า *.do ซึ่งสอดคล้องกับนิสัยการต่อท้ายของ struts-> <url-pattern>/</url-pattern>
ขั้นตอนที่ 11 การทดสอบขั้นสุดท้าย:
(1) เขียน login.jsp ก่อน
<%@ page language = "java" contentType = "ข้อความ/html; charset = utf-8" pageencoding = "utf-8"%> <html> <head> <title> การเข้าสู่ระบบของผู้ใช้: </title> </head> <body> <body> name = "username"> </input> <br> <br> <br> <dable> รหัสผ่าน: </label> <input type = "text" id = "รหัสผ่าน" name = "รหัสผ่าน"> </input> <br> <br> <input type = "subment" value = "เข้าสู่ระบบ"
อินเทอร์เฟซ:
(2) การเขียน usercontroller
นำเข้า Javax.servlet.http.httpservletRequest นำเข้า org.springframework.steretype.controller; นำเข้า org.springframework.web.bind.annotation.requestmapping; นำเข้า com.sm.dao.userdao; {@Resource UserDao UserDao ส่วนตัว; @RequestMapping ("/jsp/login") การเข้าสู่ระบบสตริงสาธารณะ (คำขอ httpservletRequest) {string username = request.getParameter ("ชื่อผู้ใช้"); สตริงรหัสผ่าน = request.getParameter ("รหัสผ่าน"); ผู้ใช้ผู้ใช้ = ผู้ใช้ใหม่ (); // สอบถามฐานข้อมูล user.setName (ชื่อผู้ใช้); user.setPassword (รหัสผ่าน); ผู้ใช้ผู้ใช้ = userDao.finduserById (ผู้ใช้); // ถ้ามีนักเรียนหมายความว่าการเข้าสู่ระบบจะสำเร็จถ้า (ผู้ใช้! = null) {return "susscss"; } // ฉันไม่ได้เขียน jsp นี้ตราบใดที่คุณรู้คุณสามารถเขียนผลตอบแทนของคุณเอง "err";}}(3) ในที่สุด susscss.jsp
<%@ page language = "java" contentType = "text/html; charset = utf-8" pageencoding = "utf-8"%> <html> <body> <p> รหัสผ่านบัญชีผู้ใช้ถูกต้องการเข้าสู่ระบบนั้นสำเร็จ </p> </body> </html>
สมบูรณ์แบบ!
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น