一点关于 mybatis 的记录
记录一点……
官网,中文文档
mybatis – MyBatis 3 | XML 映射器
https://mybatis.org/mybatis-3/zh/sqlmap-xml.html
关联另一个 mybatis select 查询
<association property="author" select="getAuthorById" column="author_id" foreignColumn="id"/>
使用连表前缀映射的,主要结果映射与关联的结果映射,手动启用自动映射
<resultMap id="article2" type="me.hzr.springmybatispractice1.entity.Article" autoMapping="true">
加 id 字段映射
<id column="id" property="id"/>
<association property="author" column="blog_author_id" javaType="Author" resultMap="authorResult"/>
或者省略
<association property="author" column="blog_author_id" resultMap="authorResult"/>
关联外部的一个 resultMap
结果集扩展
<resultMap id="carResult" type="Car" extends="vehicleResult">
discriminator 嵌入鉴别器
通常数据库列使用大写字母组成的单词命名,单词间用下划线分隔
推荐这些文章:
mybatis 结果映射 collection oftype为string,integer等类型
<resultMap id="videoMap" type="com.yao.ivideo.model.dataobject.Obj">
<result column="id" property="id"/>
<collection property="list" ofType="string">
<result column="name"/>
</collection>
</resultMap>
<select id="selectById" resultMap="iMap">
...
delete from t_association_member where id in(select id from (select * from t_association_member where (id_number,certificate_type,association_id) in(select id_number,certificate_type,association_id from t_association_member GROUP BY id_number,certificate_type,association_id HAVING count(*)>1)an...
1.需求
表1 category 是标签表,包含主标签和子标签,通过自关联查询得到完整结果
表2 是商品表
2.配置完成的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 namespace="top.roud.buyfresh.dao.ProductMapper">
&l...
处理多对一映射关系
方式一:使用级联方式
<resultMap id="empAneDeptMap" type="employee">
<id property="empId" column="emp_id"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<res...
Mybatis查询 resultMap 一对多查询 一对一初级版
Mybatis查询 resultMap 一对多查询 一对一
一对多查询
<resultMap type="User" id="UserResult">
<!--主键绑定-->
<id property="userId" column="user_id" />
...........................................................
<!--非主键绑定-->
<result property="remark...
1,推荐用第一种
<select id="getTeacher2" resultMap="TeacherStudent"> select s.id sid,s.name sname,t.id tid,t.name tname from teacher t,student s where s.tid=tid and tid=#{tid}</select><resultMap id="TeacherStudent" type="Teacher"> <result property="id" column="tid"/> ...
1. 将查询的字段信息映射为一个复杂对象
<resultMap id="resultMap" type="com.skd.entity.vo.WorkVo" >
<id property="id" column="id"/>
<!-- 引用类型属性 -->
<association property="user" javaType="com.skd.entity.vo.User">
<result property="id" column="user_id"/...
<trim prefix="where" prefixOverrides="and"> <if test="id!=null"> id=#{id} </if> <if test="username!=null"> and username=#{username} </if></trim>等同于
<where> <if test="id!=null"> id=#{id} ...
最开始写代码的时候我很苦恼,对于一个对象中含有另一个对象,每次从数据库拿到数据后都映射不过来,后来仔细学明白了做个汇总
public class SysUser implements Serializable {
private static final long serialVersionUID = 3529219554011221820L;
Integer id ;
String userName ;
String userId ;
String userPassword ;
String token ;
SysRole...
MyBatis加强(1)~myBatis对象关系映射(多对一关系、一对多关系)、延迟/懒加载
一、myBatis对象关系映射(多对一关系、一对多关系)
1、多对一关系:
---例子:多个员工同属于一个部门。
(1)myBatis发送 额外SQL:
■ 案例:员工表通过 dept_id 关联 部门表,需求:查询指定员工id、name、所属的部门名称的信息。
//部门对象的接口、映射文件省略,跟员工逻辑差不多
/* 员工对象的接口 */
public interface EmployeeMapper {
Employee get(Long id);
}
<!--员工对象的映射文件-->
<!-- 解决列名和属性名不匹配问题 -->
<resul...
文章链接:https://www.dianjilingqu.com/4173.html
本文章来源于网络,版权归原作者所有,如果本站文章侵犯了您的权益,请联系我们删除,联系邮箱:saisai#email.cn,感谢支持理解。