การรวมเข้ากับ Spring3
ในฐานะที่เป็นกรอบพื้นฐานสปริงสามารถรวมเฟรมเวิร์กแบ็คเอนด์เช่นไฮเบอร์เนต, mybatis ฯลฯ
บทความก่อนหน้านี้แนะนำการใช้ mybatis เพียงอย่างเดียวและตรรกะทั่วไปคือ:
SQLSessionFactory <- ไฟล์การกำหนดค่า (รวมถึงการกำหนดค่าการเชื่อมต่อฐานข้อมูล)
ixxxmapper <- sqlsession <- sqlsessionfactory
<- อินเทอร์เฟซ Mapper <- Mapper XML
หลังจากได้รับ ixxmapper คุณสามารถโทรหาวิธีการสำหรับการโต้ตอบข้อมูล
เมื่อรวมเข้ากับฤดูใบไม้ผลิวัตถุข้างต้นจำเป็นต้องได้รับการจัดการเป็นถั่ว:
DataSource Bean <- การกำหนดค่าการเชื่อมต่อฐานข้อมูล
sqlsessionfactory bean <- แหล่งข้อมูล
<- ไฟล์กำหนดค่า
Usermapper Bean <- sqlsessionfactory
<- อินเทอร์เฟซ Mapper
1. เพิ่มการพึ่งพาใน pom.xml:
<properties> <mybatis.spring.version> 1.2.1 </mybatis.spring.version> <dbcp.version> 1.4 </dbcp.version> <pring.version> 3.1.2. การปล่อยตัว-การพึ่งพาอาศัยกัน <RoupID> org.mybatis </groupId> <ratifactid> mybatis-spring </artifactid> <cersion> $ {mybatis.spring.version} </version> </derness> <การพึ่งพา> <! <ArtIfactId> Spring-Jdbc </artifactId> <persion> $ {Spring.version} </เวอร์ชัน> </การพึ่งพา> <การพึ่งพา> <!-DataSource เป็นอินสแตนซ์ของ BasicDataSource-> <roupId> Commons-DBCP </GroupId> </permentency> <การพึ่งพาอาศัย> <roupId> org.springframework </groupId> <ratifactId> การทดสอบฤดูใบไม้ผลิ </artifactid> <version> $ {Spring.version} </version> 2. สร้างไฟล์ Beans-da.xml ภายใต้ classpath:
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id = "DataSource" value = "com.mysql.jdbc.driver" /> <property name = "url" value = "jdbc: mysql: // localhost: 3306 /hbatis? catreatencoding = utf8" /> <property name = "username" value = "root" /> < id = "sqlsessionfactory"> <!-sqlsessionfactory bean-> <property name = "dataSource" ref = "dataSource" /> <!-แหล่งข้อมูล-> <property name = "configlocation" value = "classpath: configuration.xml" /> <! name = "sqlsessionfactory" ref = "sqlsessionfactory" /> <property name = "mapperinterface" value = "com.john.hbatis.mapper.iusermapper" /> <!
3. ชั้นเรียนทดสอบ:
@ContextConfiguration (locations = {"classpath: beans-da.xml"}) คลาสสาธารณะ SpringIntegrationTest ขยาย AbstractTestNgSpringContextSTests {logger สุดท้าย logger สุดท้าย = loggerFactory.getLogger @Resource MAPPER IUSERMAPPER; @Test โมฆะสาธารณะ QueryTest () {ผู้ใช้ผู้ใช้ = mappper.getUserById (1); log.info ("ชื่อ: {}, ที่อยู่: {}", user.getName (), user.getAddress ()); - การรวมเข้ากับ SpringMVC
ที่นี่เราสร้างการรวมเข้ากับ Spring3:
1. เพิ่ม SpringMVC และ Freemarker ขึ้นพึ่งพา Pom.xml:
<properties> <freemarker.version> 2.3.19 </freemarker.version> <servlet.version> 2.5 </servlet.version> </premerties> <predency> <roupid> org.freemarker </groupid> <Ederency> <sderctId> javax.servlet </groupId> <ratifactId> servlet-api </artifactid> <version> $ {servlet.version} </version> <scope> ให้ </cope> </dependency>2. เพิ่มผู้ฟังของ Spring และ Servlet ของ SpringMVC ใน web.xml:
<Sistener> <Sistener-Lass> org.springframework.web.context.contextloaderlistener </listener-class> <!-ฟังเหตุการณ์คอนเทนเนอร์เริ่มต้นและปิดบริบทของแอปพลิเคชันเว็บ <ผู้ฟัง> org.springframework.web.context.contextcleanuplistener </listener-class> <!-ทำความสะอาดทรัพยากรทำลายล้างที่เกี่ยวข้องกับฤดูใบไม้ผลิใน ServletContext เมื่อเว็บแอปพลิเคชันถูกปิด-> </listener> <servlet> <servlet-class> org.springframework.web.servlet.dispatcherservlet </servlet-class> <!-<init-Param> <param-name> contextconfiglocation </param-name> สำหรับ $ {project.name} -servlet.xml ในไดเรกทอรี web-inf-> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name> hbatis </servlet-name>3. สร้างใหม่ภายใต้ Web-Inf:
ไฟล์การกำหนดค่าสปริง applicationcontext.xml:
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: p = "http://ww.springframework xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns: บริบท = "http://www.springframework.org/schema/context http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context location = "classpath: /database.properties" /> <!-ไฟล์การกำหนดค่าฐานข้อมูล-> <bean id = "dataSource" p: driverclassname = "$ {driverclassname}" p: url = "$ {url}" p: username = "$ {user_name} id = "SQLSessionFactory"> <!-SQLSessionFactory Object-> <property name = "DataSource" ref = "DataSource" /> <!-แหล่งข้อมูล-> <property name = "configlocation" value = "configuration.xml" /> <! value = "classpath*: com/john/hbatis/model/*. xml"/>-> <!-คุณสามารถกำหนดค่าไฟล์การแมปใน configuration.xml หรือที่นี่ แต่คุณไม่สามารถมี parametermap, resultmap, sql ฯลฯ name = "basepackage" value = "com.john.hbatis.mapper"/> </ebean> </ebeans>Database.properties ภายใต้ ClassPath:
driverclassName = com.mysql.jdbc.driver url = jdbc: mysql: // localhost: 3306/mybatis? ตัวละคร = UTF8 user_name = รหัสผ่านรูท = 123456
หมายเหตุ: เนื่องจาก MappersCannerConfigurer อาจทำให้ชื่อผู้ใช้ดึงบัญชีของผู้ใช้ทำให้การเชื่อมต่อฐานข้อมูลล้มเหลวดังนั้นจึงเปลี่ยนเป็นค่าอื่น: user_name
ไฟล์การกำหนดค่า SpringMvc hbatis-servlet.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: mvc = "http://www.springframework.org/schema/MVC http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.-mvc. requestmappinghandlermapping, requestmappinghandleradapter และ exclitionhandlerexceptionresolver เพื่อให้การสนับสนุนสำหรับคำอธิบายประกอบเช่น @requestmapping, @exceptionhandler ฯลฯ-> <บริบท: คอมโพเนนต์-สแกนแพคเกจ = "com.john.hbatis.controller" /> <! โปรเซสเซอร์-> <bean id = "viewResolverftl"> <property name = "viewClass" value = "org.springframework.web.servlet.view.freeMarker.freeMarkerView"/> <property name = "contentType" value = "ข้อความ/html; name = "cache" value = "false"/> <property name = "viewNames"> <array> <dange>*. ftl </value> </array> </property> <!-<property name = "คำต่อท้าย" value = "ftl"/>-> <property name = "order" value = "0" id = "freemarkerConfig"> <property name = "templateLoaderPaths"> <slist> <dalue>/web-inf/ftl/</alues> <!-เส้นทางการโหลดเทมเพลต-> </list> </property> </ebean>
4. MVC:
เลเยอร์ควบคุม: usercontroller.java
@Controller @RequestMapping ("/บทความ") ชั้นเรียนสาธารณะ userController {@autowired iusermapper Mapper; @RequestMapping ("/list") public String showall (modelmap modelmap) {list <s article> articles = mappper.getarticlesByUserid (1); modelmap.addattribute ("บทความ", บทความ); กลับ "main.ftl"; -ดูเลเยอร์: main.ftl:
<#list บทความเป็นบทความ> <div> $ {article.id} $ {startic.title}: $ {sticor.content} </div> </#list>5. เริ่มโครงการและป้อนเบราว์เซอร์: http: // localhost: 8080/hbatis/บทความ/list.htm เพื่อดูผลลัพธ์