ฉันเพิ่งเข้ามาติดต่อกับ Springboot และไม่คุ้นเคยกับหลาย ๆ สิ่งเช่นวิธีการอธิบายประกอบวิธีการกำหนดค่า ฯลฯ ; ฉันได้ยินมาว่ามันยอดเยี่ยมมากดังนั้นฉันจึงพยายามเรียนรู้ หลังจากทำความคุ้นเคยกับโปรแกรมแรกของ Springboot ฉันคิดว่าเมื่อสปริงรวม MyBatis มันใช้แหล่งข้อมูลเพื่อเชื่อมต่อกับฐานข้อมูลดังนั้นฉันจึงต้องการลองใช้ C3P0 เพื่อเชื่อมต่อกับฐานข้อมูล ดังนั้นจึงมีเนื้อหาต่อไปนี้:
ขั้นแรกขั้นตอนแรกคือการสร้างแพ็คเกจการนำเข้าโครงการ Maven:
pom.xml
<carent> <scarreid> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Starter-Parent </artifactid> <sersion> 1.4.1.Release </เวอร์ชัน> </parent> <Scope> ทดสอบ </cope> </perctency> <การพึ่งพา> <GroupId> org.springframework.boot </groupid> <ratifactid> การทดสอบสปริง-สตาร์เทสต์ </artifactid> <cersion> 1.4.1.release </เวอร์ชั่น> <ArtIfactId> Spring-Boot-Starter-WEB </artifactid> <sersion> 1.4.1.Release </เวอร์ชัน> </การพึ่งพา> <การพึ่งพา> <roupId> org.mybatis.spring.boot </groupid> <RoupID> MySQL </groupID> <TarTifactId> mySQL-connector-java </artifactId> <Sersion> 5.1.33 </Side> </การพึ่งพาการพึ่งพา> <Dernency> <plugins> <roupId> org.springframework.boot </groupid> <ratifactid> Spring-Boot-Maven-Plugin </artifactId> </plugin> </plugins>
บริการแอปพลิเคชัน
นำเข้า org.mybatis.spring.annotation.mapperscan; นำเข้า org.springframework.boot.springapplication; นำเข้า org.springframework.boot.autoconfigure.enableautoconfiguration; org.springframework.boot.autoconfigure.springbootapplication;@springbootapplication@mapperscan (value = "com.myk.spring.t3") // สแกนแพ็คเกจ Mapper@enableautoconfiguration // autoload ทั้งหมดที่จำเป็นสำหรับแอปพลิเคชัน เมื่อใช้คุณสมบัติยกเว้นจะถูกห้ามให้กำหนดค่าแอปพลิเคชันคลาสสาธารณะคลาสที่แน่นอนโดยอัตโนมัติ {โมฆะสาธารณะคงที่หลัก (สตริง [] args) {springapplication.run (application.class, args); -เนื้อหาไฟล์กำหนดค่า (ฐานข้อมูลการกำหนดค่า)
DataSource: URL: JDBC: MySQL: // IP: พอร์ต/DatabasEname Driver-class: com.mysql.jdbc.driver ชื่อผู้ใช้: *** รหัสผ่าน: ***
เตรียมอย่างถูกต้องลองเปิดใช้งานบริการแอปพลิเคชันและดำเนินการต่อเพื่อกำหนดค่าแหล่งข้อมูลโดยไม่มีข้อผิดพลาด ลองใช้วิธีการมากมายมันเป็นไปไม่ได้ที่จะฉีดด้วยตัวเองดังนั้นฉันจึงนึกถึงการกำหนดค่าของฤดูใบไม้ผลิและพยายามกำหนดค่าด้วยวิธีนั้น ดังนั้น dataSourceConfig.java
นำเข้า javax.sql.datasource; นำเข้า org.mybatis.spring.sqlsessionfactorybean; นำเข้า org.springframework.beans.factory.annotation.qualifier; นำเข้า org.springframework.boot.autoconfigure.jdbc.datasurce org.springframework.boot.context.properties.configurationProperties; นำเข้า org.springframework.context.annotation.bean; นำเข้า org.springframework.context.annotation.configuration; com.mchange.v2.c3p0.combopooledDataSource; @configurationPublic คลาส DataSourceConfig {@bean (name = "dataSource") @qualifier (value = "dataSource") // แหล่งข้อมูลหลัก @configurationProperties (คำนำหน้า = "c3p0.primary") // ระบุค่าคุณสมบัติที่นำหน้าด้วย c3p0 ในไฟล์ dataSource public/{return dataSourceBuilder.create () SQLSessionFactory */ @Bean สาธารณะ SQLSessionFactoryBean SQLSessionFactoryBean () {SQLSessionFactoryBean SQLSessionFactory = ใหม่ SQLSessionFactoryBean (); SQLSessionFactory.SetDataSource (DataSource ()); ส่งคืน sqlsessionfactory; - อาจมีคำถามว่าทำไมจำเป็นต้องมีเพียง SQLSessionFactory ที่นี่และคุณต้องคุ้นเคยกับการรวม MyBatis ในฤดูใบไม้ผลิ นอกจากนี้ให้ดูที่ชั้นล่างและเข้าใจว่าวิธีนี้คุณสามารถรวบรวมโดยอัตโนมัติ
แหล่งข้อมูลพร้อมแล้วลองเชื่อมต่อกับฐานข้อมูล
แก้ไขอินเตอร์เฟส
Examplemapper.java
นำเข้า java.util.list; Interface Interface ExampleMapper {รายการสาธารณะ <ผู้ใช้> getUsers ();}แก้ไขไฟล์ mapper xml
<? xml เวอร์ชัน = "1.0" การเข้ารหัส = "utf-8"?> <! doctype mapperpublic "-// mybatis.org//dtd mapper 3.0 // en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd id = "getUsers" resultType = "com. *. ผู้ใช้"> เลือก * จากผู้ใช้ </select> </mapper>
แก้ไขคลาสเอนทิตีเพราะคุณต้องการสะท้อนผลลัพธ์ในคลาสถั่ว
users.java
ผู้ใช้ชั้นเรียนสาธารณะ {ส่วนตัว int userId; ชื่อผู้ใช้สตริงส่วนตัว; @Override สตริงสาธารณะ toString () {return "ผู้ใช้ [userId =" + userId + ", ชื่อผู้ใช้ =" + ชื่อผู้ใช้ + "]"; } public int getUserId () {return userId; } โมฆะสาธารณะ setUserId (int userId) {this.userId = userId; } สตริงสาธารณะ getUserName () {ส่งคืนชื่อผู้ใช้; } โมฆะสาธารณะ setUserName (ชื่อผู้ใช้สตริง) {this.userName = ชื่อผู้ใช้; } ผู้ใช้สาธารณะ (int userId, ชื่อผู้ใช้สตริง) {super (); this.userid = userid; this.username = ชื่อผู้ใช้; } ผู้ใช้สาธารณะ () {super (); // todo stub stub stub}}}} การแก้ไขและการดำเนินการ
ตัวอย่าง 2.java
นำเข้า java.util.list; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.web.bind.annotation.requestmapping; นำเข้า org.springframework.web.bind.annotation.responsebody; org.springframework.web.bind.annotation.restcontroller;@restcontroller@requestmapping (value = "/example2") // การทำแผนที่คลาสสาธารณะ @ResponseBody @RequestMapping (value = "/gu") รายการสาธารณะ <ผู้ใช้> getUsers () {return ExampleMapper.getUsers (); - ป้อนที่อยู่: http: // localhost: 8080/example2/gu
แสดงผลลัพธ์บนพื้นผิว:
[{"userId": 1, "ชื่อผู้ใช้": "AS"}, {"userId": 2, "ชื่อผู้ใช้": "เรา"}, {"userId": 3, "ชื่อผู้ใช้": "zx"}]
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น