ฉันไม่ได้เรียนรู้สิ่งใหม่ ๆ มาระยะหนึ่งแล้ว หลังจากวันที่ 1 ตุลาคมฉันรู้สึกว่าก่อนหน้านี้ฉันทำสิ่งต่าง ๆ ดีกว่า ฉันจะไม่พูดเรื่องไร้สาระมากนัก มาพูดคุยเกี่ยวกับการใช้ mybatis เพื่อเชื่อมต่อกับฐานข้อมูล MySQL ในบทความก่อนหน้านี้ฉันเขียนการทดสอบ JDBC และตารางฐานข้อมูลของฉันยังคงเหมือนเดิม เนื่องจากข้อมูลส่วนใหญ่ที่ฉันค้นหาทางออนไลน์คือ Eclipse เนื่องจากฉันคุ้นเคยกับ Ides ฉันจึงมาที่นี่พร้อมกับปัญหามากมาย
ดูโครงสร้างทางวิศวกรรม
ให้ฉันพูดถึงรหัส Java ก่อน DAO เป็นอินเทอร์เฟซแบบสอบถามโมเดลคือ XML ของ BEN และคำสั่งค้นหาที่สอดคล้องกัน ฉันรู้สึกว่ามันแย่ไปหน่อย user.xml ดีกว่าใน DAO คุณจะรู้หลังจากเข้าใจแล้ว เนื่องจากฉันเป็นผู้เริ่มต้นฉันไม่เข้าใจหลายสิ่งมากนัก คำจำกัดความที่สมเหตุสมผลของแพ็คเกจจะได้รับในภายหลัง
ฉันใช้การทดสอบสำหรับมัน
มาพูดคุยเกี่ยวกับทรัพยากรการกำหนดค่าเป็นไฟล์การกำหนดค่า log4j เป็นการพิมพ์บันทึกและอื่น ๆ ไม่สามารถใช้งานได้ในขณะนี้
มาดูรหัสกันเถอะฉันจะมอบให้คุณตามลำดับการสร้างไฟล์
ก่อนอื่นต้องมีวัตถุซึ่งสร้างขึ้นตามฐานข้อมูล
user.java
แพ็คเกจ com.fanyafeng.model; นำเข้า java.util.date;/*** ผู้แต่ง: fanyafeng* ข้อมูล: 16/10/11 14: 56* อีเมล: [email protected]*/public ผู้ใช้คลาสส่วนตัว id;} สตริงสาธารณะ getUserName () {return username;} โมฆะสาธารณะ setUserName (ชื่อผู้ใช้สตริง) {this.username = ชื่อผู้ใช้;} วันที่สาธารณะ getBirthday () {คืนวันเกิด;} โมฆะสาธารณะ setBirthday (วันเกิดวันเกิด) {this.sex = sex;} public String getAddress () {return address;} public void setaddress (ที่อยู่สตริง) {this.address = address;}@emverridepublic String toString () {return "ผู้ใช้ {" + "id =" + id + " + ", address = '" + ที่อยู่ +'/'' + '}';}}
iuserdao.java
แพ็คเกจ com.fanyafeng.dao; นำเข้า com.fanyafeng.model.user; นำเข้า java.util.list;/*** ผู้แต่ง: fanyafeng* ข้อมูล: 16/10/11 14: 55* อีเมล: [email protected]*/public interface iuserdao เป็นโมฆะเพิ่ม (); โมฆะสาธารณะ del (int id); โมฆะสาธารณะดัดแปลง (int id);}
user.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.fanyafeng.dao.iuserdao"> <select id = "selectUserById" parameterType = "int" resultType = "com.fanyafeng.model.user"> select * จากผู้ใช้ที่ id = #{id} การฉีด-> <select id = "queryuserByName" parameterType = "String" resultType = "com.fanyafeng.model.user"> เลือก * จากผู้ใช้ที่ชื่อผู้ใช้เช่น #{ชื่อ} </select> <! id = "เพิ่ม" parameterType = "com.fanyafeng.model.user"> แทรกลงในผู้ใช้ (รหัส, ชื่อผู้ใช้, วันเกิด, เพศ, ที่อยู่) ค่า (#{id},#{ชื่อผู้ใช้},#{วันเกิด},#{sex}userTest.java
แพ็คเกจ com.fanyafeng.test; นำเข้า com.fanyafeng.model.user; นำเข้า org.apache.ibatis.io.resources; นำเข้า org.apache.ibatis.session.sqlsession; นำเข้า org.apache.ibatis.session.sqlsessionfactory; org.apache.ibatis.session.sqlsessionfactorybuilder นำเข้า java.io.ioexception; นำเข้า java.io.reader; นำเข้า java.util.date; นำเข้า java.util.list;/*** ผู้เขียน: Fanyafeng* ข้อมูล: 16/10/11 14: 58* SQLSessionFactory SQLSessionFactory; เครื่องอ่านแบบคงที่ส่วนตัว; คงที่ {ลอง {reader = resources.getResourceAsreader ("configuration.xml"); SQLSessionFactory = ใหม่ SQLSessionFactoryBuilder () สร้าง (reader);} Main (String [] args) {sqlsession sqlsession = sqlsessionfactory.opensession (); ลอง {// ผู้ใช้ // ผู้ใช้ = (ผู้ใช้) sqlsession.selectone ("com.fanyafeng.dao.iuserdao.selectuserbyid", 2); user.getBirthday ()); // system.out.println (user.getId () + user.getUserName ()); //// list <ผู้ใช้> userlist = sqlsession.selectList ("com.fanyafeng.dao.iumerdao.queryuseryname" สำหรับ (int i = 0; i <userlist.size (); i ++) {system.out.println (userlist.get (i) .toString ());} ผู้ใช้ผู้ใช้ = ผู้ใช้ใหม่ (); user.setId (100); user.setBirthday (วันที่ใหม่); squat "); int isadd = sqlsession.insert (" com.fanyafeng.dao.iuserdao.add ", ผู้ใช้); sqlsession.commit (); // อย่าลืม} ในที่สุด {sqlsession.close ();}}}}}}}}}}}}}configuration.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <! doctype configurationPublic "-// mybatis.org//dtd config 3.0 // en" "http://mybatis.org/dtd/mybatis-3-config.dtd" ที่นี่-> <!-<typealias alias = "user" type = "com.fanyafeng.model.user"/>-> <!-</typealiases>-> <!-</syealiases>-> <! type = "jdbc"/> <!-พูลการเชื่อมต่อฐานข้อมูล-> <dataSource type = "pooled"> <property name = "driver" value = "com.mysql.jdbc.driver"/> <property name = "url" value = "jdbc: mysql value = "root"/> <property name = "password" value = "" // </dataSource> </environment> </environments> <mappers> <mapper resource = "com/fanyafeng/model/user.xml" // </aemappers>
สำหรับ log4j โปรดเพิ่มตามงานอดิเรกของคุณ ความคิดเห็นในรหัสถูกเขียนอย่างละเอียดดังนั้นฉันจะไม่เข้าไปดูรายละเอียด มีสถานที่ที่นี่ทำให้ฉันรู้สึกหงุดหงิดมาก ฉันขอให้เจ้านายผู้ยิ่งใหญ่รู้ว่าเกิดอะไรขึ้น มีไดเรกทอรีเป้าหมายในภาพหน้าจอ นี่คือตำแหน่งที่เก็บข้อมูลสำหรับไฟล์ที่รวบรวม อย่างไรก็ตามคำถามคือไฟล์ Java จะถูกรวบรวมและวางไว้ที่ไหน อย่างไรก็ตามไฟล์ XML ที่ไม่ใช่ทรัพยากรไม่ได้ถูกใส่เข้าไปและมีข้อยกเว้นถูกโยนออกไป ฉันตรวจสอบข้อมูลจำนวนมากและตอบคำถาม จากนั้นฉันก็แก้ไขได้เช่นนี้และดูไฟล์ pom.xml หลังจากที่ฉันเปลี่ยนฉันเพิ่มแท็กบิลด์
<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 "> <moderVervesion> 4.0.0 </modelversion> <roupId> com.fanyafeng </groupid> artifactid> mybatisdemo </artifactid> <packaging> สงคราม </packaging> <cersion> 1.0-snapshot </version> <name> MyBatisDemo หนอง webapp </name> <url> http://maven.apache.org </url> <uffer> <buildName> MyBatisDemo </finalname> N/Resources </Directory> <cenned> <cult> **/*. xml </cult> <cult> **/*. คุณสมบัติ </include> </cold> </surese> <resources> < ไดเรกทอรี> src/main/java </directory> <incond> <calful> **/*. xml </include> </cold> </sorsors> </resources> </build> <pendencies> <pendency> <sdependency> https://mvnrepository.com/artifact/mysql/mysql-connector-java-> <การพึ่งพา> <GroupId> mysql </groupId> <ratifactid> mysql-connector-java </artifactid> https://mvnrepository.com/artifact/org.mybatis/mybatis-> <การพึ่งพา> <Groupid> org.mybatis </groupid> <ratifactid> mybatis </artifactid> https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12-> <cendency> <roupid> org.slf4j </groupid> <ArtifactId> SLF4J-LOG4J12 https://mvnrepository.com/artifact/org.slf4j/slf4j-api-> <การพึ่งพา> <GroupId> org.slf4j </groupId> <ArtIfactId> SLF4J-API </artifactId> https://mvnrepository.com/artifact/log4j/log4j-> <cendency> <sderctiD> <roupId> log4j </groupId> <ratifactId> log4j </artifactid> <cersion> 1.2.17 </เวอร์ชัน> </การพึ่งพา> https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core-> <pendency> <roupid> org.apache.logging.log4j </groupid> https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api-> <การพึ่งพาอาศัย> <roupid> org.apache.logging.log4j </groupid> https://mvnrepository.com/artifact/org.javassist/javassist-> <pendency> <merdency> <roupId> org.javassist </groupId> https://mvnrepository.com/artifact/commons-logging/commons-logging-> <การพึ่งพา> <roupid> Commons-Logging </groupId> <ratifactid> Commons-Logging </artifactid> -> <predency> <roupId> cglib </groupId> <ratifactid> cglib </artifactid> <version> 2.2.2 </เวอร์ชัน> </การพึ่งพา> <!-https://mvnrepository.com/artifact/asm/asm -> <predency> <roupId> ASM </groupId> <ratifactId> ASM </artifactId> <version> 3.3.1 </เวอร์ชัน> </การพึ่งพา
นี่ก็เพียงพอแล้วฉันได้ดำเนินการค้นหาและเพิ่มเติมที่นี่และมีการศึกษาสิ่งอื่น ๆ
ข้างต้นคือการเชื่อมต่อ Intellij mybatis กับฐานข้อมูล MySQL ที่แนะนำโดยตัวแก้ไข ฉันหวังว่ามันจะเป็นประโยชน์กับทุกคน หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับทุกคนในเวลา ขอบคุณมากสำหรับการสนับสนุนเว็บไซต์ Wulin.com!