跟着狂神说的视频教程自己也记录下。
ResultMap 1 2 3 4 5 6 7 8 9 10 11 12 public class User { private int id; private String name; private String password; }
如果不改变去查询,则查到的password是null的。
两种解决方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <select id="selectUserById" resultType="User" > select id , name , pwd as password from user where id = #{id} </select> <resultMap id="UserMap" type="User" > <!-- id为主键 --> <id column="id" property="id" /> <!-- column是数据库表的列名 , property是对应实体类的属性名 --> <result column="name" property="name" /> <result column="pwd" property="password" /> </resultMap> <select id="selectUserById" resultMap="UserMap" > select id , name , pwd from user where id = #{id} </select>
Log4j输出日志
maven需要导入log4j的包
编写配置文件
在mybatis-config.xml中的settings设置日志实现
在测试类中使用Log4j进行输出
Lombok插件 导入lombok之后,在POJO类上一行添加@Data
就可以省去生成Get/Set/ToString
.
分页
使用Limit实现分页
1 2 3 4 5 6 7 8 <select id="selectUser" parameterType="map" resultType="user" > select * from user limit #{startIndex},#{pageSize} </select> Map<String,Integer> map = new HashMap <String,Integer>(); map.put("startIndex" ,(currentPage-1 )*pageSize); map.put("pageSize" ,pageSize); List<User> users = mapper.selectUser(map);
使用RowBounds分页:了解,不推荐使用
注解 利用注解开发就不需要mapper.xml文件了,本质上是利用了动态代理,适当使用注解,有一些弊端
1 2 3 4 5 6 7 8 9 10 @Select("select id,name,pwd password from user") public List<User> getAllUser () ;<mappers> <mapper class="com.kuang.mapper.UserMapper" /> </mappers>
注解的使用细节:
1 2 3 4 5 6 7 8 9 10 11 12 @Delete("delete from user where id = #{id}") int deleteUser (@Param("id") int id) ;@Update("update user set name=#{name},pwd=#{pwd} where id = #{id}") int updateUser (User user) ;@Insert("insert into user (id,name,pwd) values (#{id},#{name},#{pwd})") int addUser (User user) ;@Select("select * from user where id = #{id}") User selectUserById (@Param("id") int id) ;
多对一情形(多个学生对应一个老师)
按查询嵌套处理,即子查询
1 2 3 4 5 6 7 8 9 10 11 12 13 <select id="getStudents" resultMap="StudentTeacher" > select * from student </select> <resultMap id="StudentTeacher" type="Student" > <association property="teacher" column="tid" javaType="Teacher" select="getTeacher" /> </resultMap> <select id="getTeacher" resultType="teacher" > select * from teacher where id = #{id} </select>
按结果嵌套处理:直接查询出结果,对结果集进行映射,即联表查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <select id="getStudents2" resultMap="StudentTeacher2" > select s.id sid, s.name sname , t.name tname from student s,teacher t where s.tid = t.id </select> <resultMap id="StudentTeacher2" type="Student" > <id property="id" column="sid" /> <result property="name" column="sname" /> <!--关联对象property 关联对象在Student实体类中的属性--> <association property="teacher" javaType="Teacher" > <result property="name" column="tname" /> </association> </resultMap>
一对多的情形:(一个老师对应多个学生) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @Data public class Teacher { private int id; private String name; private List<Student> students; } @Data public class Student { private int id; private String name; private int tid; }
按照查询嵌套处理
1 2 3 4 5 6 7 8 9 10 <select id="getTeacher2" resultMap="TeacherStudent2" > select * from teacher where id = #{id} </select> <resultMap id="TeacherStudent2" type="Teacher" > <!--column是一对多的外键 , 写的是一的主键的列名--> <collection property="students" javaType="ArrayList" ofType="Student" column="id" select="getStudentByTeacherId" /> </resultMap> <select id="getStudentByTeacherId" resultType="Student" > select * from student where tid = #{id} </select>
按照结果嵌套处理
1 2 3 4 5 6 7 8 9 10 11 12 13 <select id="getTeacher" resultMap="TeacherStudent" > select s.id sid, s.name sname , t.name tname, t.id tid from student s,teacher t where s.tid = t.id and t.id=#{id} </select> <resultMap id="TeacherStudent" type="Teacher" > <result property="name" column="tname" /> <collection property="students" ofType="Student" > <result property="id" column="sid" /> <result property="name" column="sname" /> <result property="tid" column="tid" /> </collection> </resultMap>
注: 在resultMap之前,SQL已经查询完数据了,也就是说有一个纯数据的表,但是他们每一列的属性未知,resultMap就是为这些列加属性名
注:上面的意思相同,但是情形不同,其实就是谁是驱动表谁是被驱动表的问题。在多对一的情形中,学生表是驱动表;而在一对多的情形中,老师表是驱动表 association是用于一对一和多对一,而collection是用于一对多的关系
JavaType和ofType都是用来指定对象类型的
JavaType是用来指定pojo中属性的类型
ofType指定的是映射到list集合属性中pojo的类型。