1. At the time of writing the project will encounter a database field names and the name of the entity class attribute different circumstances, might be for the convenience may also be other requirements, whatever, we are to solve this problem
Prepare a database table is as follows
1 CREATE TABLE orders( 2 order_id INT PRIMARY KEY AUTO_INCREMENT, 3 order_no VARCHAR(20), 4 order_price FLOAT 5 );
We took the class definition of an entity class
1 public class Order { 2 private int id; 3 private String orderNo; 4 private float price; 5 }
Visible entity class and database table field names we define inconsistent, there are two solutions for this
Method One: Define alias sql statement
1 <select id="selectOrder" parameterType="int" resultType="_Order"> 2 select order_id id, order_no orderNo,order_price price from orders where order_id=#{id} 3 </select>
Method two: solved by <resultMap>
<select id="selectOrderResultMap" parameterType="int" resultMap="orderResultMap"> select * from orders where order_id=#{id} </select> <resultMap type="_Order" id="orderResultMap"> <id property="id" column="order_id"/> <result property="orderNo" column="order_no"/> <result property="price" column="order_price"/> </resultMap>
For example (which relates to the nested ( Association ) associated with the query results, the following will explain it)
2. Implement contingency table query
Defined entity classes
teacher entity class
private int tId; private String tName;
student entity class
private int sId; private String sName; private int ClassId;
clazz entity class
private int cId; private String cName; private int tId; private Teacher teacher; private List<Student> students;
Wherein clazz entity class, a class corresponding to a teacher, a class corresponding to a plurality of students
At this time, the following mapping file ClazzMapper.xml
For many, many to one question we can have two solutions
Method One: Secondary Query
Method Two: Union-table query
. 1 <? XML Version = "1.0" encoding = "UTF-. 8" ?> 2 <! DOCTYPE Mapper the PUBLIC "- // mybatis.org//DTD Mapper 3.0 // EN" . 3 "http://mybatis.org/ DTD /. 3-MyBatis-mapper.dtd " > . 4 <-! namespace: namespace represented. Now the aim is to distinguish id. -> . 5 < Mapper namespace = "com.zhiyou100.zhl.dao.ClazzDao" > . 6 < The resultMap type = "com.zhiyou100.zhl.bean.Clazz" id = "myMap" > . 7 < ID column = "c_id" Property = " column="c_name" property="cName"/> 9 <result column="teacher_id" property="tId"/> 10 <association property="teacher" javaType="com.zhiyou100.zhl.bean.Teacher"> 11 <id column="t_id" property="tId"/> 12 <result column="t_name" property="tName"/> 13 </association> 14 <collection property="students" ofType="com.zhiyou100.zhl.bean.Student"> 15 <id column="s_id" property="sId"/> 16 <result column="s_name" property="sName"/> 17 </collection> 18 </resultMap> 19 20 <select id="selectById" resultMap="mymap"> 21 select * from class c join teacher t join student s on c.teacher_id=t.t_id and s.class_id=c.c_id where c_id=#{cId}; 22 </select> 23 24 </mapper>
Unit testing