一、用到的实体类如下:
student.java
แพ็คเกจ com.company.entity; นำเข้า java.io.serializable; นำเข้า java.util.date; นักเรียนชั้นเรียนสาธารณะใช้ serializable {ส่วนตัวคงที่สุดท้าย long serialversionuid = 1l; ID int ส่วนตัว; ชื่อสตริงส่วนตัว; วันเกิดส่วนตัว กลุ่มกลุ่มเอกชน; กลุ่มสาธารณะ getGroup () {กลุ่มส่งคืน; } โมฆะสาธารณะ setGroup (กลุ่มกลุ่ม) {this.group = กลุ่ม; } สาธารณะ int getId () {return id; } โมฆะสาธารณะ setId (int id) {this.id = id; } สตริงสาธารณะ getName () {ชื่อคืน; } โมฆะสาธารณะ setName (ชื่อสตริง) {this.name = name; } วันที่สาธารณะ getBirth () {ส่งคืน; } โมฆะสาธารณะ setBirth (วันเกิดวันที่) {this.birth = เกิด; } @Override สตริงสาธารณะ toString () {return "นักเรียน [เกิด =" + เกิด + ", กลุ่ม =" + กลุ่ม + ", id =" + id + ", name =" + name + "]"; -Group.java
แพ็คเกจ com.company.entity; นำเข้า java.util.list; กลุ่มชั้นเรียนสาธารณะ {ID INT ส่วนตัว; ชื่อสตริงส่วนตัว; ตำแหน่งสตริงส่วนตัว รายการส่วนตัว <Tudent> นักเรียน; รายการสาธารณะ <Tudent> getStudents () {ส่งคืนนักเรียน; } โมฆะสาธารณะ setstudents (รายการ <student> นักเรียน) {this.students = นักเรียน; } สาธารณะ int getId () {return id; } โมฆะสาธารณะ setId (int id) {this.id = id; } สตริงสาธารณะ getName () {ชื่อคืน; } โมฆะสาธารณะ setName (ชื่อสตริง) {this.name = name; } การ getPosition สตริงสาธารณะ () {ตำแหน่งส่งคืน; } โมฆะสาธารณะ setPosition (ตำแหน่งสตริง) {this.position = ตำแหน่ง; } @Override สตริงสาธารณะ toString () {return "กลุ่ม [id =" + id + ", name =" + name + ", position =" + position + "]"; -二、实体对应的表结构
นักเรียน表:
สร้างนักเรียนตาราง (รหัสหลัก ID, ชื่อ Varchar (20), วันเกิด, Group_id int อ้างอิง g_group (G_ID));
g_group 表:
สร้างตาราง g_group (คีย์หลัก g_id int, g_name varchar (20), g_position varchar (30));
ลำดับ:
สร้าง sequence student_id_equence; สร้าง sequence group_id_equence;
三、 นักเรียน和กลุ่ม, 你可以在映射文件中找到, 关于 mybatis 的增删改查操作, mybatis 调用存储过程 mybatis 分页以及 mybatis 对一对一、多对多的处理
XML 文件中都标有注释, 看的时候配合下面的具体实现看, 虽然有点乱
student.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.company.dao.istudentdao"> <!-mybatis 缓存-> <cache eviction = "lru" flushinterval = "600000" size = "1024" readonly = "false" /> <! <!-此处获得多对一的关系, 但就单条记录而言却是一对一的关系, 所以一对一的写法跟此相同-> <resultmap type = "student" id = "getStudentAndGroup"> <id column = "id" คุณสมบัติ = "id"/> <column result = "name" property = "name"/> <results = "birth" property = "การเกิด"/> <result column = "g_name" property = "name"/> <result column = "g_position" คุณสมบัติ = "ตำแหน่ง"/> </eceless> </resultmap> <select id = "many2one" resultmap = "getstudentandgroup" parametertype = "int"> select, s.id, s.birth, s.birth, s.birth, s.birth, s. เข้าร่วม g_group g บน s.group_id = g.g_id โดยที่ s.id = #{id} </select> <!-意图是获得一个学生, 并且获得该学生所属的组, 跟上面的意思差不多, 用การเชื่อมโยง的เลือก属性-> <! column = "name" property = "name"/> <result column = "birth" property = "birth"/> <eceless property = "group" column = "group_id" javatype = "group" select = "selectgroup"/> </resultmap> <select id = #{id} </select> <select id = "selectgroup" resultType = "group" parameterType = "int" flushcache = "false" usecache = "true"> <!-此处实用缓存-> เลือก g_id เป็น id, g_name เป็นชื่อ g_position <select id = "getStudentBySomecondition" parameterType = "นักเรียน" resultType = "นักเรียน"> เลือก * จากนักเรียน <โดยที่> <ถ้าทดสอบ = "id! = null"> id> 2 </ถ้า> <ถ้าทดสอบ = "ชื่อ! id = "studentmap"> <id column = "id" คุณสมบัติ = "id"/> <result column = "name" property = "name"/> <result column = "birth" property = "birth"/> </resultmap> <select id = "getAlluser" out = out, motelype = "callable" resultmap = studentmap})} </select> <!-mybatis 向นักเรียน表中插入一条数据-> <แทรก id = "เพิ่ม" parameterType = "นักเรียน" keycolumn = "id"> <selectkey keyProperty = "id" ord = "ก่อนหน้า ค่า (#{id},#{ชื่อ},#{เกิด}) </แทรก> <!-根据 id 获得学生的信息-> <select id = "getById" parameterType = "int" resultType = "นักเรียน"> <รวม refid = "studentColumns"/> <select id = "getAllstudent" resultmap = "studentmap"> <รวม refid = "studentcolumns"/>>> ord โดย id <!-此处是引用了上面预定义好的 sql 语句-> </select> </mapper>以上所述是小编给大家介绍的 mybatis 存储过程、 mybatis 分页、 mybatis 一对多增删改查操作, 希望对大家有所帮助, 如果大家有任何疑问请给我留言, 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持! 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!