1. บทนำสู่ MyBatis
MyBatis เป็นเฟรมเวิร์กเลเยอร์การคงอยู่ที่ยอดเยี่ยมที่รองรับการสืบค้น SQL แบบธรรมดาขั้นตอนที่เก็บไว้และการทำแผนที่ขั้นสูง
MyBatis กำจัดการตั้งค่าด้วยตนเองของรหัส JDBC เกือบทั้งหมดและพารามิเตอร์และการค้นหาการห่อหุ้มของชุดผลลัพธ์
MyBatis สามารถใช้ XML อย่างง่ายหรือคำอธิบายประกอบสำหรับการกำหนดค่าและการแมปดิบอินเทอร์เฟซการทำแผนที่และ pojos ของ Java (วัตถุ Java เก่าธรรมดา) ลงในบันทึกในฐานข้อมูล
jdbc -> dbutils (encapsulation อัตโนมัติ) -> mybatis -> hibernate
MyBatis เขียน SQL ใน XML จากนั้นเข้าถึงฐานข้อมูล
2. เริ่มต้นด้วย mybatis
2.1. สร้างโครงการ Java ใหม่
เพิ่ม mybatis และ mysql jar jar: mybatis-3.1.1.jar, mysql-connector-java-5.1.7-bin.jar
2.2. สร้างตารางใหม่
สร้างฐานข้อมูล mybatis; ใช้ mybatis; สร้างผู้ใช้ตาราง (ID int คีย์หลัก auto_increment, ชื่อ varchar (20), อายุ int); แทรกลงในผู้ใช้ (ชื่อ, อายุ) ค่า ('tom', 12); แทรกลงในผู้ใช้ (ชื่ออายุ) ค่า ('แจ็ค', 11);2.3. เพิ่มไฟล์กำหนดค่า mybatis conf.xml
<? XML เวอร์ชัน = "1.0" การเข้ารหัส = "UTF-8"?> <! การกำหนดค่า doctype สาธารณะ "-// myBatis.org//dtd config 3.0 // en" "http://mybatis.org/dtd/mybatis-3-config.dtd id = "การพัฒนา"> <transactionManager type = "jdbc" /> <dataSource type = "pooled"> <property name = "driver" value = "com.mysql.jdbc.driver" /> <property name = "url" value = "jdbc: mysql: // localhost: 3306 /mybat name = "password" value = "root"/> </dataSource> </environment> </environments> </การกำหนดค่า>
2.4. กำหนดคลาสเอนทิตีที่สอดคล้องกับตาราง
ผู้ใช้ระดับสาธารณะ {ID ส่วนตัว ID; ชื่อสตริงส่วนตัว; อายุ int ส่วนตัว; // get, set method}2.5. กำหนดไฟล์การแมป SQL 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.atguigu.mybatis_test.test1.usermapper"> <select id = "getUser" พารามิเตอร์ = "int int" resultType = "com.atguigu.mybatis_test.test1.user"
2.6. ลงทะเบียนไฟล์ usermapper.xml ในไฟล์ conf.xml
<Mappers> <mapper resource = "com/atguigu/mybatis_test/test1/usermapper.xml"/> </mappers>
2.7. การเขียนรหัสทดสอบ: เรียกใช้คำสั่งเลือกที่กำหนดไว้
การทดสอบระดับสาธารณะ {โมฆะคงที่สาธารณะหลัก (สตริง [] args) พ่น ioexception {string resource = "conf.xml"; // โหลดไฟล์การกำหนดค่าของ mybatis (มันยังโหลดไฟล์การแมปที่เกี่ยวข้อง) เครื่องอ่าน = resources.getResourceArederer (ทรัพยากร); // สร้างโรงงาน SQLSessionFactoryBuilder (). build (reader); // สร้างเซสชัน sqlsessionsqlsession ที่สามารถเรียกใช้ SQL ในไฟล์การแมป = sessionFactory.opensession (); // การแมป SQL identification string string = "com.atguigu.mybatis.bean.bean.batis.batis.batis.batis.batis.batis.batis. sqluser user = session.selectone (คำสั่ง, 1); system.out.println (ผู้ใช้);}}3. ใช้งานตารางผู้ใช้
3.1.xml การใช้งาน
3.1.1. กำหนดไฟล์ XML การแมป SQL:
<insert id = "InsertUser" parameterType = "com.atguigu.ibatis.bean.user"> แทรกลงในผู้ใช้ (ชื่อ, อายุ) ค่า (#{ชื่อ},#{อายุ}); </insert> <delete id = "deleteUser" parameterType = "int" parameterType = "com.atguigu.ibatis.bean.user"> อัปเดตผู้ใช้ตั้งชื่อ =#{ชื่อ}, อายุ =#{อายุ} โดยที่ id =#{id} </update> <select id = "selectUser" parameterType = "int" id =#{id} </select> <select id = "selectallusers" resultType = "com.atguigu.ibatis.bean.user"> เลือก * จากผู้ใช้ </select>3.1.2. ลงทะเบียนไฟล์การแมปนี้ใน config.xml
<mapper resource = "com/atguigu/ibatis/bean/usermapper.xml"/>
3.1.3. เรียกใน dao
ผู้ใช้สาธารณะ GetUserById (ID int) {SQLSession Session = SessionFactory.OpenSession (); ผู้ใช้ผู้ใช้ = Session.SESECTONE (URI+". SelectUser", ID); return user;}3.2. การดำเนินการตามคำอธิบายประกอบ
3.2.1. กำหนดอินเทอร์เฟซสำหรับการแมป SQL
อินเทอร์เฟซสาธารณะ usermapper {@insert ("แทรกลงในผู้ใช้ (ชื่ออายุ) ค่า (#{ชื่อ},#{อายุ})") intuser int สาธารณะ (ผู้ใช้ผู้ใช้);@delete ("ลบจากผู้ใช้ที่ id =#{id}") id =#{id} ") public int updateUser (ผู้ใช้ผู้ใช้);@select (" เลือก * จากผู้ใช้ที่ id =#{id} ") ผู้ใช้สาธารณะ getUserById (int id);@select (" เลือก * จากผู้ใช้ ") รายการสาธารณะ <ผู้ใช้> getAlluser ();};};3.2.2. ลงทะเบียนอินเทอร์เฟซการแมปนี้ในการกำหนดค่า
<mapper/>
3.2.3. เรียกใน dao
ผู้ใช้สาธารณะ getUserById (ID int) {SQLSession Session = SessionFactory.OpenSession (); USERMAPPER MAPPER = Session.GetMapper (USERMAPPER.CLASS); ผู้ใช้ = mappper.getUserById (ID); return user;}}4. หลายสถานที่ที่สามารถปรับให้เหมาะสมได้
4.1. การกำหนดค่าสำหรับการเชื่อมต่อกับฐานข้อมูลสามารถวางไว้ในไฟล์คุณสมบัติแยกกัน
## db.properties <br> <properties resource = "db.properties" /> <property name = "driver" value = "$ {driver}" /> <property name = "url" value = "$ {url}" /> <property name = "usernce}4.2. กำหนดนามแฝงสำหรับคลาสเอนทิตีและลดความซับซ้อนของการอ้างอิงในไฟล์ XML การแมป SQL
<typealiases> <typealias type = "com.atguigu.ibatis.bean.user" alias = "_ user"/> </ypealiases>
4.3. คุณสามารถเพิ่มไฟล์การกำหนดค่า log4j ภายใต้ SRC เพื่อพิมพ์ข้อมูลบันทึก
1. เพิ่มขวด:
log4j-1.2.16.jar
2.1. log4j.properties (วิธีที่ 1)
log4j.properties, log4j.rootlogger = การดีบัก, คอนโซล#consolelog4j.appender.console = org.apache.log4j.consoleapenderlog4j.appender.console.layout = org.log.log4j.patternlaynlay %-5p [ %c] - %m%nlog4j.logger.java.sql.resultset = infolog4j.logger.org.apache = infolog4j.logger.java.sql.connection = debuglog4j.logger.java.sql.statement = debuglog4
2.2. log4j.xml (วิธีที่ 2)
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <! doctype log4j: ระบบการกำหนดค่า "log4j.dtd"> <log4j: การกำหนดค่า xmlns: log4j = "http://jakarta.apache.org/log4j/" value = "%-5p%d {mm-dd hh: mm: ss, ss}%m (%f:%l) /n" /> < /layout> < /appender> <logger name = "java.sql"> <ระดับ levle = "debug" /> </ogger> /> < /logger> <root> <levle value = "debug" /> <appender-ref ref = "stdout" /> </root> </log4j: การกำหนดค่า>5. แก้ไขความขัดแย้งระหว่างชื่อฟิลด์และชื่อแอตทริบิวต์เอนทิตี
5.1. เตรียมโต๊ะและฟิลด์
สร้างคำสั่งซื้อตาราง (ORDER_ID int คีย์หลัก auto_increment, order_no varchar (20), order_price ลอย); แทรกลงในคำสั่ง (order_no, order_price) ค่า ('aaaa', 23); แทรกลงในคำสั่งซื้อ (order_no, order_price) 22);5.2. กำหนดคลาสเอนทิตี
คำสั่งซื้อระดับสาธารณะ {Private Int ID; String ส่วนตัว orderno; ราคาลอยตัวส่วนตัว;}5.3. ใช้แบบสอบถามของ getOrderById (ID):
วิธีที่ 1: กำหนดนามแฝงในคำสั่ง SQL
<select id = "selectorder" parameterType = "int" resultType = "_ order"> เลือก order_id id, order_no orderno, order_price ราคาจากคำสั่งซื้อที่ order_id =#{id} </select>วิธีที่ 2: ถึง <resultmap>
<select id = "selectorderResultMap" parameterType = "int" resultmap = "orderResultMap"> เลือก * จากคำสั่งซื้อที่ order_id =#{id} </select> <resultmap type = "_ order" ord = "orderResultMap"> property = "price" column = "order_price"/> <result property = "price" คอลัมน์ = "order_price"/> <resultmap>6. ใช้การสืบค้นตารางการเชื่อมโยง
6.1. สมาคมแบบตัวต่อตัว
6.1.1. เสนอข้อกำหนด
ข้อมูลชั้นเรียนแบบสอบถามตาม ID คลาส (ข้อมูลกับครู)
6.1.2. สร้างตารางและข้อมูล
สร้าง Table Teacher (t_id int คีย์หลัก auto_increment, t_name varchar (20)); สร้างคลาสตาราง (c_id int คีย์หลัก auto_increment, c_name varchar (20), teacher_id int); เปลี่ยนคลาสตารางเพิ่มข้อ จำกัด fk_teacher_id คีย์ต่างประเทศ ใส่เข้าไปในค่าครู (t_name) ('ls1'); แทรกลงในค่าครู (t_name) ค่า ('ls2'); แทรกลงในคลาส (c_name, ค่าครู) ค่า ('bj_a', 1); แทรกลงในชั้นเรียน (c_name, ค่าครู) ค่า ('bj_b', 2);6.1.3. กำหนดคลาสเอนทิตี:
ครูชั้นเรียนสาธารณะ {ID INT ส่วนตัว; ชื่อสตริงส่วนตัว;} ชั้นเรียนสาธารณะ {ID INT ส่วนตัว; ชื่อสตริงส่วนตัว; ครูเอกชน;}6.1.4. กำหนดไฟล์ mapl mapl classmapper.xml
<!-วิธีการที่ 1: ผลลัพธ์ที่ซ้อนกัน: ใช้แผนที่ผลลัพธ์ซ้อนกันเพื่อประมวลผลชุดย่อยของผลลัพธ์ร่วมกันที่ซ้ำกันเลือก * จากคลาส C, ครู T, นักเรียน s โดยที่ c.teacher_id = t.t_id และ c.c_id = s.class_id และ c.c_id = 1-> <select id = "getClass3" c.teacher_id = t.t_id และ c.c_id = s.class_id และ c.c_id = 1-> <select id = "getClass3" พารามิเตอร์ = "int" resultmap = "classResultMap3"> เลือก * จากคลาส C, ครู T, นักเรียน c.c_id =#{id} </select> <resultmap type = "_ classes" id = "classResultMap3"> <id property = "id" คอลัมน์ = "c_id"/> <คุณสมบัติผลลัพธ์ = "ชื่อ" คอลัมน์ = "c_id"/> < property = "name" column = "t_name"/> <property result = "name" คอลัมน์ = "t_name"/> </seceless> <!-ofType ระบุประเภทวัตถุในคอลเลกชันนักเรียน-> <property collection = "นักเรียน" OFType = "_ นักเรียน"> <id property = "id" คอลัมน์ = column = "s_name"/> </collection> </resultmap> <!-วิธีที่ 2: การสืบค้นซ้อนกัน: ส่งคืนประเภทคอมเพล็กซ์ที่คาดหวังโดยดำเนินการคำสั่งการแมป SQL อื่นเลือก * จากคลาสที่ c_id = 1; เลือก * จากครูที่ t_id = 1 // 1 คือค่าของครู id = "getClass4" parameterType = "int" resultmap = "classResultMap4"> เลือก * จากคลาสที่ c_id =#{id} </select> <resultmap type = "_ classes" id = "classResultMap4"> column = "teacher_id" javatype = "_ teacher" select = "getTeacher2"> </eceless> <collection property = "นักเรียน" OfType = "_ student" คอลัมน์ = "c_id" select = "getStudent"> </collection> </resultmap> ครูที่ t_id =#{id} </select> <select id = "getStudent" parameterType = "int" resultSpe = "_ student"> เลือก s_id id, ชื่อ s_name จากนักเรียนที่ class_id =#{id} </select>6.1.5. ทดสอบ
@TestPublic เป็นโมฆะ testoo () {sqlSession sqlsession = factory.opensession (); คลาส c = sqlsession.selectone ("com.atguigu.day03_mybatis.test5.oomapper.getClass" sqlsession = factory.opensession (); คลาส c = sqlsession.selectone ("com.atguigu.day03_mybatis.test5.oomapper.getclass2", 1); system.out.println (c);}}6.2. สมาคมหนึ่งถึงหลายคน
6.2.1. เสนอข้อกำหนด
สอบถามข้อมูลชั้นเรียนที่สอดคล้องกันตาม ClassID รวมถึงนักเรียนและครู
6.2.2. สร้างตารางและข้อมูล:
สร้าง Table Student (s_id int key primary auto_increment, s_name varchar (20), class_id int); แทรกลงในนักเรียน (s_name, class_id) ค่า ('xs_a', 1); แทรกเข้าไปในนักเรียน (s_name, class_id) ค่า ('xs_b', 1); นักเรียน (s_name, class_id) ค่า ('xs_d', 2); แทรกลงในนักเรียน (s_name, class_id) ค่า ('xs_e', 2); แทรกลงในนักเรียน (s_name, class_id) ค่า ('xs_f', 2);6.2.3. กำหนดคลาสเอนทิตี
นักเรียนชั้นเรียนสาธารณะ {ID ส่วนตัว ID; ชื่อสตริงส่วนตัว;} ชั้นเรียนสาธารณะ {ID INT ส่วนตัว; ชื่อสตริงส่วนตัว; ครูเอกชน; รายการส่วนตัว <Tudent> นักเรียน;}6.2.4. กำหนดไฟล์ mapl mapl classmapper.xml
<!-วิธีการที่ 1: ผลลัพธ์ที่ซ้อนกัน: ใช้แผนที่ผลลัพธ์ซ้อนกันเพื่อประมวลผลชุดย่อยของผลลัพธ์ร่วมกันที่ซ้ำกันเลือก * จากคลาส C, ครู T, นักเรียน s โดยที่ c.teacher_id = t.t_id และ c.c_id = s.class_id และ c.c_id = 1-> <select id = "getClass3" c.teacher_id = t.t_id และ c.c_id = s.class_id และ c.c_id = 1-> <select id = "getClass3" พารามิเตอร์ = "int" resultmap = "classResultMap3"> เลือก * จากคลาส C, ครู T, นักเรียน c.c_id =#{id} </select> <resultmap type = "_ classes" id = "classResultMap3"> <id property = "id" คอลัมน์ = "c_id"/> <คุณสมบัติผลลัพธ์ = "ชื่อ" คอลัมน์ = "c_id"/> < property = "name" column = "t_name"/> <property result = "name" คอลัมน์ = "t_name"/> </seceless> <!-ofType ระบุประเภทวัตถุในคอลเลกชันนักเรียน-> <property collection = "นักเรียน" OFType = "_ นักเรียน"> <id property = "id" คอลัมน์ = column = "s_name"/> </collection> </resultmap> <!-วิธีที่ 2: การสืบค้นซ้อนกัน: ส่งคืนประเภทคอมเพล็กซ์ที่คาดหวังโดยดำเนินการคำสั่งการแมป SQL อื่นเลือก * จากคลาสที่ c_id = 1; เลือก * จากครูที่ t_id = 1 // 1 คือค่าของครู id = "getClass4" parameterType = "int" resultmap = "classResultMap4"> เลือก * จากคลาสที่ c_id =#{id} </select> <resultmap type = "_ classes" id = "classResultMap4"> column = "teacher_id" javatype = "_ teacher" select = "getTeacher2"> </eceless> <collection property = "นักเรียน" OfType = "_ student" คอลัมน์ = "c_id" select = "getStudent"> </collection> </resultmap> ครูที่ t_id =#{id} </select> <select id = "getStudent" parameterType = "int" resultSpe = "_ student"> เลือก s_id id, ชื่อ s_name จากนักเรียนที่ class_id =#{id} </select>6.2.5. ทดสอบ
@TestPublic เป็นโมฆะ testom () {sqlSession sqlsession = factory.opensession (); คลาส c = sqlsession.selectone ("com.atguigu.day03_mybatis.test5.oomapper.getClass3", 1); sqlsession = factory.opensession (); คลาส c = sqlsession.selectone ("com.atguigu.day03_mybatis.test5.oomapper.getclass4", 1); system.out.println (c);}}7. แบบสอบถาม SQL แบบไดนามิกและฟัซซี่
7.1. ความต้องการ
ใช้ผู้ใช้แบบสอบถามหลายเงื่อนไข (การจับคู่ชื่อฟัซซี่อายุระหว่างค่าต่ำสุดที่ระบุและค่าสูงสุด)
7.2. เตรียมฐานข้อมูลและตาราง
สร้างตาราง d_user (ID int หลักคีย์ Auto_Increment, ชื่อ varchar (10), อายุ int (3)); แทรกลงใน d_user (ชื่อ, อายุ) ค่า ('tom', 12); แทรกลงใน d_user (ชื่ออายุ) ค่า ('bob', 13); แทรกลงใน d_user (ชื่อ, อายุ) ค่า ('แจ็ค', 18); 7.3.ConditionUser (คลาสเอนทิตีที่มีเงื่อนไขแบบสอบถาม) ชื่อสตริงส่วนตัว; private int minage; int maxage ส่วนตัว;7.4. คลาสเอนทิตีตารางผู้ใช้
ID INT ส่วนตัว; ชื่อสตริงส่วนตัว; อายุ int ส่วนตัว;
7.5.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.atguigu.day03_mybatis.test6.usermapper"> <select id = "getUser" พารามิเตอร์ testrype = "com.atguigu.day03_mybatis.test6.conditionuser อายุ> =#{minage} และอายุ <=#{maxage} <ถ้า test = 'name! = "%null%"'> และชื่อเช่น#{name} </if> </select> </mapper>7.6.usertest (ทดสอบ)
ชั้นเรียนสาธารณะ userTest {โมฆะสาธารณะคงที่หลัก (สตริง [] args) พ่น IOException {reader reader = resources.getResourceAsreader ("conf.xml"); sqlsessionfactory sessionfactory = sqlsessionfactorybuilder (reader) "com.atguigu.day03_mybatis.test6.usermapper.getuser"; รายการ <user> list = sqlsession.selectList (คำสั่ง, เงื่อนไขใหม่ ("%a%", 1, 12)); system.out.println (รายการ);}}}}แท็ก SQL แบบไดนามิกที่มีอยู่ใน mybatis
8. การโทรขั้นตอนที่เก็บไว้
8.1. สร้างความต้องการ
สอบถามจำนวนเพศชายหรือเพศหญิงหากขาเข้าเป็น 0 ตัวเมียเป็นอย่างอื่น
8.2. เตรียมตารางฐานข้อมูลและขั้นตอนที่เก็บไว้:
สร้างตาราง p_user (ID int หลักคีย์หลัก auto_increment, ชื่อ varchar (10), เซ็กซ์ถ่าน (2)); แทรกลงใน p_user (ชื่อ, เพศ) ค่า ('a', "ชาย"); แทรกลงใน p_user (ชื่อ, เพศ) ค่า ('b', "หญิง"); แทรกลงใน p_user (ชื่อ, เพศ) ค่า ('c', "ชาย"); #create ขั้นตอนที่เก็บไว้ (ค้นหาจำนวนเพศชายหรือเพศหญิงถ้าขาเข้าคือ 0, ผู้หญิงเป็นอย่างอื่น) คั่น $ สร้างขั้นตอน mybatis.ges_user_count (ใน sex_id int, user_count int) เริ่มต้นถ้า sex_id = 0 thenselect count (*) นับ (*) จาก mybatis.p_user โดยที่ p_user.sex = 'male' เป็น user_count; สิ้นสุดถ้า; สิ้นสุด $#โทรตามขั้นตอนที่เก็บไว้; ตั้ง @user_count = 0; โทร mybatis.ges_user_count (1, @user_count); เลือก @user_count;8.3. สร้างคลาสเอนทิตีของตาราง
ผู้ใช้ระดับสาธารณะ {รหัสส่วนตัวรหัสส่วนตัวชื่อสตริงส่วนตัว; สตริงส่วนตัวเพศ;}8.4.USERMAPPER.XML
<mapper namespace = "com.atguigu.mybatis.test7.usermapper"> <!-คำถามที่ได้รับจำนวนชายหรือหญิง ถ้าการเข้ามาเป็น 0 แล้วผู้หญิงจะเป็นเพศชายเป็นอย่างอื่น call mybatis.get_user_count (1, @user_count);-> <select id = "getCount" stationalType = "callable" parameterMap = "getCountMap" id = "getCountMap"> <parameter property = "sex_id" mode = "ใน" jdbctype = "จำนวนเต็ม"/> <parameter property = "user_count" โหมด = "out" jdbctype = "integer" //
8.5. ทดสอบ
แผนที่ <สตริง, จำนวนเต็ม> parammap = ใหม่ hashmap <> (); parammap.put ("sex_id", 0); session.selectone (คำสั่ง, parammap); จำนวนเต็ม usercount = parammap.get ("user_count"); system.out.println (usercount);9. แคช mybatis
9.1. เข้าใจแคช mybatis
เช่นเดียวกับเฟรมเวิร์กเลเยอร์การคงอยู่ส่วนใหญ่ MyBatis ยังให้การสนับสนุนแคช L1 และ L2
1. ระดับ 1 แคช: แคชท้องถิ่น HASHMAP ตาม PerpetualCache ขอบเขตการจัดเก็บคือเซสชัน หลังจากการล้างเซสชันหรือปิดแคชทั้งหมดในเซสชั่นจะถูกล้าง
2. กลไกของแคชทุติยภูมินั้นเหมือนกับแคชหลัก โดยค่าเริ่มต้นจะใช้ที่เก็บข้อมูล PerpetualCache และ HashMap ความแตกต่างคือขอบเขตการจัดเก็บคือ MAPPER (เนมสเปซ) และสามารถปรับแต่งแหล่งเก็บข้อมูลเช่น EHCACHE
3. สำหรับกลไกการอัปเดตข้อมูลแคชเมื่อขอบเขตที่แน่นอน (เซสชันแคชระดับแรก/เนมสเปซแคชระดับที่สอง) จะดำเนินการในการดำเนินการ C/U/D แคชทั้งหมดในการเลือกภายใต้ขอบเขตนี้จะถูกล้างตามค่าเริ่มต้น
9.2.MyBatis ระดับ 1 แคช
9.2.1 แบบสอบถามตามงาน
สอบถามวัตถุบันทึกผู้ใช้ที่เกี่ยวข้องตาม ID
9.2.2 เตรียมตารางฐานข้อมูลและข้อมูล
สร้างตาราง c_user (ID int คีย์หลัก Auto_increment, ชื่อ varchar (20), อายุ int); แทรกลงใน c_user (ชื่อ, อายุ) ค่า ('tom', 12); แทรกลงใน c_user (ชื่อ, อายุ) ค่า ('แจ็ค', 11);9.2.3 สร้างคลาสเอนทิตีของตาราง
ผู้ใช้ระดับสาธารณะใช้ serializable {ID ส่วนตัว ID; ชื่อสตริงส่วนตัว; อายุ int ส่วนตัว;9.2.4.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.atguigu.mybatis.test8.usermapper"> <select id = "getUser" parameterType = "int" resultType = "_ cuser"> เลือก * จาก c_user ที่ id =#{id} setName =#{ชื่อ}, age =#{อายุ} โดยที่ id =#{id} </update> </mapper>9.2.5 การทดสอบ
/** ระดับ 1 แคช: นั่นคือแคชระดับเซสชัน (โดยค่าเริ่มต้น)*/@testpublic void testcache1 () {sqlsession session = mybatisutils.getSession (); string attement = "com.atguigu.mybatis.test8.usermapper.getuser" แคชระดับ 1 จะถูกใช้โดยค่าเริ่มต้น*//*user = session.selectone (คำสั่ง 1); system.out.println (ผู้ใช้);*//*1 มันจะต้องเป็นเซสชั่นเดียวกัน หากวัตถุเซสชันถูกปิด () มันเป็นไปไม่ได้ที่จะใช้*//*เซสชัน = myBatisutils.getSession (); user = session.selectone (คำสั่ง 1); system.out.println (ผู้ใช้);*//*2 เงื่อนไขการสืบค้นนั้นเหมือนกัน*//*user = session.selectone (คำสั่ง, 2); system.out.println (ผู้ใช้);*//*3 session.clearcache () ยังไม่ได้ดำเนินการเพื่อทำความสะอาดแคช*//*session.clearcache (); user = session.selectone (คำสั่ง, 2); system.out.println (ผู้ใช้);*//*4 ไม่มีการดำเนินการที่ได้รับการเพิ่มลบและแก้ไข (การดำเนินการเหล่านี้จะทำความสะอาดแคช)*//*session.update ("com.atguigu.mybatis.test8.usermapper.updateuser" ผู้ใช้ใหม่ (2, "ผู้ใช้", 23)) ผู้ใช้ = เซสชัน9.3.Mybatis ระดับ 2 แคช
9.3.1. เพิ่ม <แคช> ใน usermapper.xml
<mapper namespace = "com.atguigu.mybatis.test8.usermapper"> <cache/>
9.3.2. การทดสอบ
/** การทดสอบระดับ 2 แคช*/@testpublic เป็นโมฆะ testCache2 () {string statement = "com.atguigu.mybatis.test8.usermapper.getUser"; sqlsession session = myBatisutils.getSession () ผู้ใช้ = ผู้ใช้ 1); session.commit (); system.out.println ("user ="+ผู้ใช้); sqlsession session2 = mybatisutils.getSession (); user = session2.selectone (คำสั่ง 1); session.commit ();9.3.3. หมายเหตุเพิ่มเติม
1. คำสั่ง SELECT ทั้งหมดในไฟล์คำสั่งการแมปจะถูกแคช
2. แผนที่ทั้งหมดแทรกอัปเดตและลบคำสั่งในไฟล์คำสั่งจะรีเฟรชแคช
3. แคชจะถูกลองใหม่โดยใช้อัลกอริทึมที่ใช้น้อยที่สุดเมื่อเร็ว ๆ นี้ (LRU ซึ่งใช้น้อยที่สุด)
4. แคชจะถูกรีเฟรชตามช่วงเวลาที่กำหนด
5. แคชจะจัดเก็บ 1024 วัตถุ
<cacheeviction = "FIFO" // กลยุทธ์การรีไซเคิลเป็นครั้งแรก, first-out flushInterval = "60000" // เวลารีเฟรชอัตโนมัติ 60SSIZE = "512" // แคชสูงถึง 512
10. การรวมสปริง mybatis
10.1. เพิ่ม jar
【 mybatis 】
mybatis-3.2.0.jar
mybatis-spring-1.1.1.jar
log4j-1.2.17.jar
【ฤดูใบไม้ผลิ】
Spring-AOAP-3.2.0.release.jar
Spring-beans-3.2.0.release.jar
Spring-Context-3.2.0.release.jar
Spring-Core-3.2.0.release.jar
Spring-expression-3.2.0.release.jar
Spring-jdbc-3.2.0.release.jar
SPRING-TEST-3.2.4.RELEASE.JAR
Spring-tx-3.2.0.release.jar
AOPALLIANCE-1.0.JAR
CGLIB-NODEP-2.2.3.JAR
Commons-Logging-1.1.1.jar
【แพ็คเกจไดรเวอร์ mysql 】
mysql-connector-java-5.0.4-bin.jar
10.2. ตารางฐานข้อมูล
สร้างตาราง s_user (user_id int auto_increment คีย์หลัก, user_name varchar (30), วันที่ user_birthday, user_salary double)
10.3. คลาส Entity: ผู้ใช้
ผู้ใช้ระดับสาธารณะ {ID ส่วนตัว ID; ชื่อสตริงส่วนตัว; วันเกิดส่วนตัว; เงินเดือนส่วนตัว; // set, รับเมธอด}10.4.DAO อินเทอร์เฟซ: USERMAPPER (XXXMAPPER)
อินเทอร์เฟซสาธารณะ usermapper {โมฆะบันทึก (ผู้ใช้ผู้ใช้); โมฆะอัปเดต (ผู้ใช้ผู้ใช้); เป็นโมฆะลบ (ID int); ผู้ใช้ findById (ID int); รายการ <user> findall ();};10.5.sql ไฟล์การแมป: 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.atguigu.mybatis.test9.usermapper"> <resultmap type = "user" id = "userresult"> <result column = "user_id" คุณสมบัติ = "id"/> <result column = "user_name" property = "เงินเดือน"/> <result column = "user_salary" คุณสมบัติ = "เงินเดือน"/> <resultmap> <!-รับ ID หลังจากแทรกข้อมูล-> <แทรก id = "บันทึก" keycolumn = "user_id" keyProperty = "id" useGeneratedKeys = "true"> s_user (user_name, user_birthday, user_salary) ค่า ( #{ชื่อ}, #{วันเกิด}, #{เงินเดือน}) </แทรก> <อัปเดต id = "อัปเดต"> อัปเดต s_userset user_name = #{ชื่อ} #{id} </update> <delete id = "delete"> ลบจาก s_userwhere user_id = #{id} </delete> <select id = "findById" resultmap = "userresult"> เลือก *จาก s_userwhere user_id = #} s_user </select> </mapper>10.6.Spring การกำหนดค่าไฟล์: beans.xml
<? xml version = "1.0" encoding = "utf-8"?> <ถั่ว xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" "xmlns: context =" http://www.springframework.org/schema/contex amework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context/sprin g-context-3.2.xsdhttp: //www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp: //www.springframework.org/schema/tx/spring-tx-3 1. แหล่งข้อมูล: DriverManagerDataSource -> <bean id = "dataSource" class = "org.springframework.jdbc.datasource.driverManagerDataSource"> <property name = "driverclassname" value = "com.mysql.jdbc.driver"/> value = "jdbc: mysql: // localhost: 3306/mybatis"/> <property name = "ชื่อผู้ใช้" value = "root"/> <property name = "รหัสผ่าน" value = "root"/> </ebean> <!-2 โรงงาน SQLSession ของ MyBatis: SQLSessionFactoryBeanDataSource/TypeAliasEspackage-> <bean id = "SQLSessionFactory"> <property name = "DataSource" ref = "DataSource"/> MyBatis สแกนโดยอัตโนมัติเพื่อโหลดไฟล์การแมป SQL: MappersCannerConfigurersQlSessionFactory/basepackage-> <bean> <property name = "basepackage" value = "com.atigu.spring_mybatis2.mapper"/> การจัดการธุรกรรม: DataSourceTransactionManager-> <bean id = "txManager"> <property name = "DataSource" ref = "DataSource"/> </epean> <!-5. ใช้ธุรกรรมการประกาศ-> <tx: transaction-manager-manager
10.7.MyBatis การกำหนดค่าไฟล์: mybatis-config.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <! doctype configurationPublic "-// mybatis.org//dtd config 3.0 // en" "http://mybatis.org/dtd/mybatis-3-config.dtd" ไม่จำเป็น-> <!-ตั้งค่าไฟล์การกำหนดค่าภายนอก-> <!-ตั้งชื่อหมวดหมู่-> <!-ตั้งค่าสภาพแวดล้อมการเชื่อมต่อฐานข้อมูล-> <!-ไฟล์การแม็พ-> </การกำหนดค่า>
10.8. ทดสอบ
@runwith (springjunit4classrunner.class) // ใช้กรอบทดสอบ Springtest @contextconfiguration ("/beans.xml") // โหลดการกำหนดค่าระดับสาธารณะ smtest {@autowired // inject usermapper usermapper usermapper; @testpublic void () ผู้ใช้ วันที่ ()); user.setName ("Mary"); user.setsalary (300); usermapper.save (ผู้ใช้); System.out.println (user.getId ());}@testpublic update () {user = usermapper.findbyid (2); delete () {usermapper.delete (3);}@testpublic void findById () {ผู้ใช้ผู้ใช้ = usermapper.findbyId (1); system.out.println (ผู้ใช้);}@testpublic void findall ()ข้างต้นคือการแนะนำอย่างรวดเร็วเกี่ยวกับ mybatis (การวิเคราะห์ที่กระชับและง่ายและเข้าใจง่าย) แนะนำโดยบรรณาธิการ ฉันหวังว่ามันจะเป็นประโยชน์กับทุกคน หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับทุกคนในเวลา ขอบคุณมากสำหรับการสนับสนุนเว็บไซต์ Wulin.com!