จากนั้นในการใช้งานล่าสุดให้เพิ่มฐานข้อมูล Orcale Query MyBatis
ขั้นตอนที่ 1: สร้างแพ็คเกจที่จำเป็นใหม่สองสามรายการผลลัพธ์มีดังนี้
ขั้นตอนที่ 2: สร้าง PersonService.java ภายใต้แพ็คเกจบริการเพื่อตรวจสอบอินเตอร์เฟสวิธีการของบุคคลตามชื่อ
แพ็คเกจ com.example.first.service; นำเข้า com.example.first.entity.person; อินเตอร์เฟสสาธารณะบุคคล
ขั้นตอนที่ 3: สร้าง PersonServiceImpl.java ใหม่ภายใต้แพ็คเกจ ServiceImpl เพื่อใช้งาน Interface Personservice.java
แพ็คเกจ com.example.first.serviceimpl; นำเข้า com.example.first.persondao.personmapperdao; นำเข้า com.example.first.entity.person; นำเข้า com.example.first.service.personservice; org.springframework.stereotype.service; นำเข้า org.springframework.transaction.annotation.transactional;@service@transactionalpublic ชั้นเรียน @Override บุคคลสาธารณะ queryPersonByName (ชื่อสตริง) {person person = personmapperdao.findbyName (ชื่อ); คนกลับ; -ขั้นตอนที่ 4: สร้าง personmapperdao.java ใหม่ภายใต้ Persondao มีวิธีการสอบถามบุคคล
แพ็คเกจ com.example.first.persondao; นำเข้า com.example.first.entity.person; นำเข้า org.apache.ibatis.annotations.mapper; @mapperpublic interface personmapperdao {person findByName (ชื่อสตริง);};};};ขั้นตอนที่ 5: สร้าง personmapper.xml ใหม่ภายใต้ทรัพยากร
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <! doctype mapper สาธารณะ "-// mybatis.org//dtd mapper 3.0 // en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" namespace = "com.example.first.persondao.personmapperdao"> <resultmap id = "findperson" type = "com.example.first.entity.person"> <property = "ชื่อ" = "ชื่อ" ชื่ออายุจากบุคคลที่ name = #{ชื่อ} </select> </mapper>ขั้นตอนที่ 6: เพิ่มแหล่งข้อมูลเส้นทางไฟล์ Mapper และเส้นทางเอนทิตีใน Application.properties
Spring.jpa.database = Oraclespring.datasource.driver-class-name = oracle.jdbc.driver.oracledriverspring.datasource.url = jdbc: Oracle: Thin:@// 192.168.3.177:1521/orclspring.data.data.data.data.data.data.data.data.data source.username = liguang_devspring.datasource.password = 123456spring.jpa.hibernate.ddl-auto = updatemybatis.mapperlocations = classpath:/mapper/* com.example.first.entityspring.thymeleaf.prefix = classpath: /templates/spring.thymeleaf.suffix=.htmlspring.thymeleaf.mode=html5
ขั้นตอนที่ 7: เพิ่มการพึ่งพาในไฟล์ POM
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/ XSI: schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" <ArtIfactId> SpringBoot </artifactId> <persion> 0.0.1-Snapshot </เวอร์ชัน> <cackaging> jar </packaging> <name> Springboot </name> <scription> โครงการสาธิตสำหรับการบูตฤดูใบไม้ผลิ </คำอธิบาย> <carent> <Sersion> 1.5.6.Release </เวอร์ชัน> <inelypath/> <!-การค้นหาผู้ปกครองจากที่เก็บ-> </parent> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <java.version> 1.8 </java.version> </premerties> <การพึ่งพา> <การพึ่งพา> <roupId> org.springframework.boot </groupid> <ArtIfactId> การทดสอบสปริง-บูท-สตาร์เทสต์ </artifactid> <pope> ทดสอบ </cope> </dercendency> <การพึ่งพา> <roupId> org.springframework.boot </groupid> <RoupID> Oracle </groupId> <ratifactId> ojdbc7 </artifactId> <cersion> 1.0.0.1 </Servive> </perdency> <!-การพึ่งพา myBatis-> <การพึ่งพา> <roupId> org.mybatis.spring.boot </Groupid> <Sersion> 1.1.1 </Service> </perdency> <การพึ่งพา> <roupId> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Starter-JDBC </artifactid> </การพึ่งพา> </การพึ่งพา> <สร้าง> <ArtIfactId> Spring-Boot-Maven-Plugin </artifactid> </plugin> </plugins> </uffer
ขั้นตอนที่ 8: ป้อน http: // localhost: 8080/person/show? name = zhang ในเบราว์เซอร์
สรุป
ด้านบนเป็นตัวอย่างรหัสคำอธิบายของสปริงบูต + mybatis + ขั้นตอนการใช้งาน orcale แนะนำโดยตัวแก้ไข ฉันหวังว่ามันจะเป็นประโยชน์กับทุกคน หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับทุกคนในเวลา ขอบคุณมากสำหรับการสนับสนุนเว็บไซต์ Wulin.com!