ภาพรวม
ทุกวันนี้แอปพลิเคชันอินเทอร์เน็ตส่วนใหญ่ยังคงใช้ mybatis เพื่อใช้งานฐานข้อมูล บทความนี้แนะนำวิธีการรวม mybatis ใน Spring Boot
บทความก่อนหน้านี้แนะนำวิธีการเรียกใช้โครงการโดยตรงใน Spring Boot ด้วย JAR หากคุณต้องการให้คลิกเพื่อดู
สร้างโครงการสปริงบูต
ในการเปิดสปริงบูต - สร้างและเรียกใช้บทความมีส่วนที่อธิบายวิธีการใช้ส่วนประกอบสปริงบูตเพื่อสร้างโครงการ หากคุณต้องการรวม mybatis คุณจะต้องตรวจสอบส่วนประกอบทั้งสอง mysql และ mybatis
แน่นอนคุณสามารถเพิ่มการพึ่งพาโดยตรงในไฟล์ pom.xml โดยไม่ต้องใช้วิธีนี้ ฉันเลือกที่จะเพิ่มการพึ่งพาโดยตรงในไฟล์ pom.xml
การพึ่งพา> <roupId> org.mybatis.spring.boot </groupId> <ratifactid> mybatis-spring-boot-starter </artifactid> <cersion> 1.3.1 </เวอร์ชัน> </การพึ่งพา> <cersion> 5.1.34 </version> </derctency> <predency> <sdeperency> <sdependency> <sderctiD> <roupId> com.alibaba </groupId> <ratifactId> druid </artifactid> <cersion> 1.1.7 </version> </serdency>
แหล่งข้อมูลใช้ดรูอิดของอาลีบาบา เนื้อหาไฟล์ pom.xml ที่สมบูรณ์มีดังนี้:
<? 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" <Sersion> 0.0.1-SNAPSHOT </เวอร์ชัน> <packaging> jar </packaging> <name> การศึกษา </name> <scription> โครงการสาธิตสำหรับการบูตฤดูใบไม้ผลิ </คำอธิบาย> <parent> <mentar> การค้นหาผู้ปกครองจากที่เก็บ-> </parent> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.raporting.outputencoding> UTF-8 </project.reporting.UutPutuCoding> <การพึ่งพา> <roupId> org.mybatis.spring.boot </groupid> <ratifactid> mybatis-spring-boot-starter </artifactid> <version> 1.3.1 </เวอร์ชัน> </การพึ่งพา> </perctency> <predency> <roupId> mysql </groupId> <ratifactid> mysql-connector-java </artifactid> <version> 5.1.34 </เวอร์ชัน> </การพึ่งพา> <pertid> <roupid> com.alibaba </roupid> <erperency> <sderctId> com.alibaba </groupId> <ratifactid> fastjson </artifactid> <cersion> 1.2.45 </เวอร์ชัน> </การพึ่งพา> <การพึ่งพาอาศัย> <roupid> org.springframework.boot </groupid> <ratifactid> </dependencies> <สร้าง> <plugins> <molins> <roupId> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Maven-Plugin </artifactid> </plugin> </plugins>
สร้างตาราง
สร้างตาราง `ผู้ใช้ '(` id` bigint (20) ไม่ใช่ null auto_increment, `name` varchar (30) ไม่ใช่ค่าเริ่มต้น null' ', คีย์หลัก (` id`)) เครื่องยนต์ = InnoDB auto_increment = 8 charset charset = utf8 ความคิดเห็น =' ข้อมูลผู้ใช้ ';
สร้างเอนทิตี
แพ็คเกจ com.springboot.entity; ผู้ใช้ระดับสาธารณะ {Private Long ID; ชื่อสตริงส่วนตัว; Public Long getId () {return id; } โมฆะสาธารณะ setId (Long id) {this.id = id; } สตริงสาธารณะ getName () {ชื่อคืน; } โมฆะสาธารณะ setName (ชื่อสตริง) {this.name = name; } @Override สตริงสาธารณะ toString () {return "ผู้ใช้ {" + "id =" + id + ", name = '" name +'/'' + '}'; -สร้างไฟล์การแมป mybatis และ repo
userrepo.java
แพ็คเกจ com.springboot.repo; นำเข้า com.springboot.entity.user; นำเข้า org.apache.ibatis.annotations.mapper; นำเข้า org.springframework.stereotype.component; นำเข้า java.util.list; ผู้ใช้ SELECTBYPRIMARYKEY (ID ยาว); intupterbyPrimaryKey (ผู้ใช้); int deleteByPrimaryKey (Long ID); รายการ <user> selectall ();}
usermapper.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.springboot.repo.userrepo"> <resultmap id = "baseresultmap" type = "com.springboot.entity.user"> <id column = "id" คุณสมบัติ = "id" jdbctype = "bigint id = "base_column_list"> id, ชื่อ </sql> <select id = "selectbyprimarykey" resultmap = "baseresultmap" parameterType = "java.lang.long"> select <รวม refid = "bigint_column_list"/>> resultmap = "baseresultmap"> เลือก <รวม refid = "base_column_list"/> จากผู้ใช้ </select> <update id = "updateByPrimaryKey" พารามิเตอร์ "parameterType =" com.springboot.entity.user "ผู้ใช้ Update <et> <ถ้าทดสอบ =" ชื่อ! = null " </set> โดยที่ id = #{id, jdbcType = bigint} </update> <delete id = "deleteByPrimaryKey" พารามิเตอร์ parameterType = "java.lang.long"> ลบจากผู้ใช้ที่ id = #{id, jdbctype = bigint} parameterType = "com.springboot.entity.user" useGeneratedKeys = "true" keyProperty = "id"> แทรกลงในผู้ใช้ <trim คำนำหน้า = "(" คำต่อท้าย = ") #{name, jdbcType = varchar} </trim> </insert> </mapper>เขียนแอปพลิเคชัน
เพิ่มไฟล์ต่อไปนี้ไปยังไฟล์ properties ที่สร้างโดย Spring Boot สำหรับเรา:
Spring.datasource.name = Spring_boot_study Spring.datasource.url = jdbc: mysql: // localhost: 3306/ทดสอบ spring.datasource.username = root spring.datasource.password = xxxxxx spring.datasource Spring.datasource.type = com.alibaba.druid.pool.druiddatasource mybatis.mapper-location
การทดสอบหน่วย
แพ็คเกจ com.springboot; นำเข้า com.springboot.entity.user; นำเข้า com.springboot.repo.userrepo; นำเข้า org.junit.test; นำเข้า org.junit.runner.runwith; นำเข้า org.springframework.beans. org.springframework.boot.test.context.springboottest; นำเข้า org.springframework.test.context.junit4.springrunner; นำเข้า java.util.list; @runwith @Test Public Void TestInsert () {ผู้ใช้ผู้ใช้ = ผู้ใช้ใหม่ (); user.setName ("test2"); userrepo.insert (ผู้ใช้); } @Test โมฆะสาธารณะ testUpDate () {ผู้ใช้ผู้ใช้ = ผู้ใช้ใหม่ (); user.setId (6L); user.setName ("test3"); USERREPO.UPDATEBYPRIMARYKEY (ผู้ใช้); } @Test Public Void TestDelete () {userrepo.deleteByPrimaryKey (6L); } @Test โมฆะสาธารณะ testSelectByPrimaryKey () {ผู้ใช้ผู้ใช้ = userrepo.selectByPrimaryKey (7L); System.out.println (ผู้ใช้); } @Test Public Void TestSeleCtall () {list <ผู้ใช้> userlist = userrepo.selectall (); System.out.println (Userlist); -สรุป
ด้านบนเป็นรหัสตัวอย่าง (เวอร์ชันที่ง่ายขึ้น) ของการรวมสปริงบูต mybatis แนะนำให้คุณรู้จัก ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับคุณทันเวลา ขอบคุณมากสำหรับการสนับสนุนเว็บไซต์ Wulin.com!