一、用到的实体类如下 :
Student.java
Paket com.Comy.entity; impor java.io.serializable; impor java.util.date; siswa kelas publik mengimplementasikan serial {private static final long serialversionuid = 1l; Private Int ID; nama string pribadi; kelahiran kencan pribadi; grup grup pribadi; grup publik getGroup () {return group; } public void setGroup (grup grup) {this.group = grup; } public int getId () {return id; } public void setid (int id) {this.id = id; } public string getName () {return name; } public void setName (name string) {this.name = name; } tanggal publik getBirth () {return kelahiran; } public void setBirth (tanggal lahir) {this.birth = kelahiran; } @Override Public String ToString () {return "Student [lahir =" + lahir + ", grup =" + grup + ", id =" + id + ", name =" + name + "]"; }}Grup.java
Paket com.Comy.entity; impor java.util.list; grup kelas publik {private int id; nama string pribadi; posisi string pribadi; Daftar Privat <Sahwa> Siswa; Daftar Publik <Sahasiswa> GetStudents () {Siswa yang kembali; } setStudents public void (Daftar <Sahasiswa> Siswa) {this.students = Siswa; } public int getId () {return id; } public void setid (int id) {this.id = id; } public string getName () {return name; } public void setName (name string) {this.name = name; } public string getPosition () {Posisi return; } public void setPosition (posisi string) {this.position = position; } @Override Public String ToString () {return "group [id =" + id + ", name =" + name + ", position =" + position + "]"; }}二、实体对应的表结构
Siswa 表:
Buat Table Student (ID int Kunci Utama, Nama Varchar (20), Tanggal Lahir, Referensi Group_ID int G_Group (G_ID));
g_group 表:
Buat Tabel G_Group (G_ID int Key Primer, G_Name Varchar (20), G_Posisi Varchar (30));
urutan:
Buat sekuens Student_id_Sequence; Buat sekuens group_id_sequence;
三、 Siswa 和 Kelompok 的映射文件如下 , 你可以在映射文件中找到 , 关于 mybatis 的增删改查操作 , mybatis 调用存储过程 , mybatis 分页以及 mybatis 对一对一、多对多的处理
xml 文件中都标有注释 , 看的时候配合下面的具体实现看 , 虽然有点乱
Student.xml
<? Xml Version = "1.0" encoding = "UTF-8"?> <! Doctype mapper public "-// mybatis.org//dtd mapper 3.0 // en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper cangki = "comybatis-3-mapper.dtd"> <!-mybatis 缓存-> <cache penggusuran = "lru" flushinterval = "600000" size = "1024" readonly = "false" /> <!-sql 标签用来定义一些可以被重用的 sql 语句或字段或片段等-> <sql id = "studentcolumns"> pilih id, nama, kelahiran dari siswa < /sql> <!-此处获得多对一的关系 此处获得多对一的关系 此处获得多对一的关系 此处获得多对一的关系 此处获得多对一的关系 所以一对一的写法跟此相同 此处获得多对一的关系 所以一对一的写法跟此相同 此处获得多对一的关系 此处获得多对一的关系 此处获得多对一的关系 所以一对一的写法跟此相同 所以一对一的写法跟此相同 此处获得多对一的关系 此处获得多对一的关系 所以一对一的写法跟此相同 此处获得多对一的关系 此处获得多对一的关系 所以一对一的写法跟此相同 id="getStudentAndGroup" > <id column="id" property="id"/> <result column="name" property="name"/> <result column="birth" property="birth"/> <association property="group" column="group_id" javaType="Group"> <id column="g_id" property="id"/> <result column="g_name" property="name"/> <result column="g_position" property="position"/> </association> </resultMap> <select id="many2one" resultMap="getStudentAndGroup" parameterType="int" > select s.id,s.name,s.birth,s.group_id,g.g_id,g.g_name,g.g_position from student s left join g_group g on s.group_id = g.g_id where s.id = #{id} </select> <!-- 意图是获得一个学生,并且获得该学生所属的组,跟上面的意思差不多 ,用association的select属性--> <!-- 于上面的相比个人感觉上面的效率要高些,因为上面只有一条sql语句 --> <resultMap type="Student" id="getStudentAndGroupUseSelectMap"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="birth" property="birth"/> <association property="group" column="group_id" javaType="Group" select="selectGroup" /> </resultMap> <select id="getStudentAndGroupUseSelect" resultMap="getStudentAndGroupUseSelectMap" parameterType="int"> select * from student where id = #{id} </select> <select id="selectGroup" resultType = "grup" parameTerType = "int" flushcache = "false" usecache = "true"> <!-此处实用缓存-> pilih g_id sebagai id, g_name sebagai nama, g_posisi sebagai posisi dari g_group di mana g_id = #{id} </pilih <!-动态 sql 语句 的测试 的测试 的测试 {{ID {ID {/SELECT/<! parameterType = "student" resultType = "student"> pilih * dari siswa <where> <if test = "id! = null"> id> 2 </if> <if test = "name! = null"> name like '%g%' </if> </where> </schect> <!-mybatis 调用存储过程-> </if> </where> </schect> <!-mybatis 调用存储过程-> </ifer "ID" "ID" "ID" "ID" "ID" "ID" "ID" "ID" "ID" "ID" "ID" "ID" " kolom = "name" properti = "name"/> <hasil kolom = "kelahiran" properti = "kelahiran"/> </resultMap> <pilih id = "getAllUser" pernyataantype = "callable"> {call get_all_student (#{students, out, jdbctype = kursor, javatype = resultsetet = {向 my {myBctype/cursor, myBctype/{{{{{~ myBctype/{{{myBctypeP = {{{myBctypeP = {{{myBctypeP = -> <masukkan id = "add" parameTerType = "student" keycolumn = "id"> <sfectKey keyproperty = "id" order = "sebelum" resultType = "int"> pilih stu_id_sequence.nextval dari dual </selectKey> insert ke siswa (id name, name, lahir) (#{id},#{}} {{{{#{{#{#{{#{{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{#{ --> <select id="getById" parameterType="int" resultType="Student"> <include refid="studentColumns"/> where id=#{id} </select> <!-- 此处的实现方法是一个分页的原型,请查看IStudentDAOImpl.java中的调用方法 --> <select id="getAllStudent" resultMap="studentMap"> <include refid="studentColumns"/> order by ID <!-此处是引用了上面预定义好的 SQL 语句-> </pilih> </mapper>以上所述是小编给大家介绍的 mybatis 存储过程、 mybatis 分页、 mybatis 一对多增删改查操作 , 希望对大家有所帮助 , 如果大家有任何疑问请给我留言 , 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!