mybatis增删改查

映射文件EmployeeMapper.xml

注意:小于或大于号等可能与如select标签的尖括号(<>)与冲突或者无法识别的时候,可以使用转义字符比如小于号可以使用&It; 等等
另一处理方法,使用CDATA比如:<![CDATA[ 代码 ]]>
(在用CDATA的时候,只用把可能产生冲突的那段代码包含就可以了,不要盲目的包含。)

一、查询

1.resultType

//接口
//@MapKey:告诉mybatis封装这个map的时候使用哪个属性作为map的key
@MapKey("lastName")
public Map<String, Employee> getEmpByLastNameLikeReturnMap(String lastName);
// 映射文件配置
<select id="getEmpByLastNameLikeReturnMap" resultType="com.laoyin.mybatis.bean.Employee">
    select * from tbl_employee where last_name like #{lastName}
</select>

接口返回类型--->resultType
Map<String, Object>--->map
Map<String, Employee>--->Employee(全类名)
注意如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身

2.resultMap

<!--自定义某个javaBean的封装规则
type:自定义规则的Java类型
id:唯一id方便引用
  -->
<resultMap type="com.laoyin.mybatis.bean.Employee" id="MySimpleEmp">
    <!--指定主键列的封装规则
    id定义主键会底层有优化;
    column:指定哪一列
    property:指定对应的javaBean属性
      -->
    <id column="id" property="id"/>
    <!-- 定义普通列封装规则 -->
    <result column="last_name" property="lastName"/>
    <!-- 其他不指定的列会自动封装:我们只要写resultMap就把全部的映射规则都写上。 -->
    <result column="email" property="email"/>
    <result column="gender" property="gender"/>
</resultMap>

<!-- resultMap:自定义结果集映射规则;  -->
<!-- public Employee getEmpById(Integer id); -->
<select id="getEmpById"  resultMap="MySimpleEmp">
    select * from tbl_employee where id=#{id}
</select>

3.association
1)使用association定义关联的单个对象的封装规则
Employee含有属性departmentName即Department对象

<resultMap type="com.laoyin.mybatis.bean.Employee" id="MyDifEmp2">
    <id column="id" property="id"/>
    <result column="last_name" property="lastName"/>
    <result column="gender" property="gender"/>
    
    <!--  association可以指定联合的javaBean对象
    property="dept":指定哪个属性是联合的对象
    javaType:指定这个属性对象的类型[不能省略]
    -->
    <association property="dept" javaType="com.laoyin.mybatis.bean.Department">
        <id column="did" property="id"/>
        <result column="dept_name" property="departmentName"/>
    </association>
</resultMap>

<!--  public Employee getEmpAndDept(Integer id);-->
<select id="getEmpAndDept" resultMap="MyDifEmp">
    SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id,
    d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
    WHERE e.d_id=d.id AND e.id=#{id}
</select>

2)使用association进行分步查询
Employee含有属性dept即Department对象
association中select指定使用哪个接口中的哪个方法去查询,column指定方法使用哪个字段的值传递参数

<!-- 使用association进行分步查询:
    1、先按照员工id查询员工信息
    2、根据查询员工信息中的d_id值去部门表查出部门信息
    3、部门设置到员工中;
    
另外:开启全局延迟加载:就是说 association中select指定接口方法,在使用的时候执行sql
<settings>      
    <!--显示的指定每个我们需要更改的配置的值,即使他是默认的。防止版本更新带来的问题  -->
    <setting name="lazyLoadingEnabled" value="true"/>
    <setting name="aggressiveLazyLoading" value="false"/>
</settings>

 -->
 
 <!--  id  last_name  email   gender    d_id   -->
 <resultMap type="com.laoyin.mybatis.bean.Employee" id="MyEmpByStep">
    <id column="id" property="id"/>
    <result column="last_name" property="lastName"/>
    <result column="email" property="email"/>
    <result column="gender" property="gender"/>
    <!-- association定义关联对象的封装规则
        select:表明当前属性是调用select指定的方法查出的结果
        column:指定将哪一列的值传给这个方法
        
        流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性
     -->
    <association property="dept" 
        select="com.laoyin.mybatis.dao.DepartmentMapper.getDeptById"
        column="d_id">
    </association>
 </resultMap>
 <!--  public Employee getEmpByIdStep(Integer id);-->
 <select id="getEmpByIdStep" resultMap="MyEmpByStep">
    select * from tbl_employee where id=#{id}
    <if test="_parameter!=null">
        and 1=1
    </if>
 </select>

4.collection

1)嵌套结果集的方式

<!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则  -->
<resultMap type="com.laoyin.mybatis.bean.Department" id="MyDept">
    <id column="did" property="id"/>
    <result column="dept_name" property="departmentName"/>
    <!-- 
        collection定义关联集合类型的属性的封装规则 
        ofType:指定集合里面元素的类型
    -->
    <collection property="emps" ofType="com.laoyin.mybatis.bean.Employee">
        <!-- 定义这个集合中元素的封装规则 -->
        <id column="eid" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
    </collection>
</resultMap>
<!-- public Department getDeptByIdPlus(Integer id); -->
<select id="getDeptByIdPlus" resultMap="MyDept">
    SELECT d.id did,d.dept_name dept_name,
            e.id eid,e.last_name last_name,e.email email,e.gender gender
    FROM tbl_dept d
    LEFT JOIN tbl_employee e
    ON d.id=e.d_id
    WHERE d.id=#{id}
</select>

2).分段查询

<!-- collection:分段查询 -->
<resultMap type="com.laoyin.mybatis.bean.Department" id="MyDeptStep">
    <id column="id" property="id"/>
    <id column="dept_name" property="departmentName"/>
    <collection property="emps" 
        select="com.laoyin.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
        <!-- getEmpsByDeptId需要传多个值得话,使用column="{key1=column1,key2=column2} -->
        column="{deptId=id}" fetchType="lazy"></collection>
</resultMap>
<!-- public Department getDeptByIdStep(Integer id); -->
<select id="getDeptByIdStep" resultMap="MyDeptStep">
    select id,dept_name from tbl_dept where id=#{id}
</select>

<!-- 扩展:多列的值传递过去:
        将多列的值封装map传递;
        column="{key1=column1,key2=column2}"
    fetchType="lazy":表示使用延迟加载;
            - lazy:延迟
            - eager:立即
 -->

5.鉴别器
Employee含有属性dept即Department对象

<!-- <discriminator javaType=""></discriminator>
    鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为
    封装Employee:
        如果查出的是女生:就把部门信息查询出来,否则不查询;
        如果是男生,把last_name这一列的值赋值给email;
 -->
 <resultMap type="com.laoyin.mybatis.bean.Employee" id="MyEmpDis">
    <id column="id" property="id"/>
    <result column="last_name" property="lastName"/>
    <result column="email" property="email"/>
    <result column="gender" property="gender"/>
    <!--
        column:指定判定的列名
        javaType:列值对应的java类型  -->
    <discriminator javaType="string" column="gender">
        <!--女生  resultType:指定封装的结果类型;不能缺少。/resultMap-->
        <case value="0" resultType="com.laoyin.mybatis.bean.Employee">
            <association property="dept" 
                select="com.laoyin.mybatis.dao.DepartmentMapper.getDeptById"
                column="d_id">
            </association>
        </case>
        <!--男生 ;如果是男生,把last_name这一列的值赋值给email; -->
        <case value="1" resultType="com.laoyin.mybatis.bean.Employee">
            <id column="id" property="id"/>
            <result column="last_name" property="lastName"/>
            <result column="last_name" property="email"/>
            <result column="gender" property="gender"/>
        </case>
    </discriminator>
 </resultMap>

二、插入

1.基本插入语句

<insert id="addEmp" parameterType="com.laoyin.mybatis.bean.Employee" >
    insert into tbl_employee(last_name,email,gender) 
    values(#{lastName},#{email},#{gender})
</insert>

解释:parameterType指定传入参数Employee对象,#{lastName}直接取到Employee的lastName的属性值

2.获取插入后该条纪录的自增主键的值(mysql数据库支持主键自增

<insert id="addEmp" parameterType="com.laoyin.mybatis.bean.Employee"
    useGeneratedKeys="true" keyProperty="id" >
    insert into tbl_employee(last_name,email,gender) 
    values(#{lastName},#{email},#{gender})
</insert>

解释:

  • useGeneratedKeys="true";使用自增主键获取主键值策略
  • keyProperty;指定对应的主键属性,也就是mybatis获取到主键值以后,将这个值封装给javaBean(Employee)的哪个属性

      //代码片段
      //openSession是以获取的SqlSession对象
      EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
      //测试添加
      Employee employee = new Employee(null, "jerry4",null, "1");
      mapper.addEmp(employee);
      //插入记录后自增的主键值会封装到employee的id属性中
      System.out.println(employee.getId());

3.获取插入后该条纪录的自增主键的值(oracle数据库不支持主键自增)

<insert id="addEmp" databaseId="oracle">
    <!-- 
    keyProperty:查出的主键值封装给javaBean的哪个属性
    order="BEFORE":当前sql在插入sql之前运行
           AFTER:当前sql在插入sql之后运行
    resultType:查出的数据的返回值类型
    
    BEFORE运行顺序:
        先运行selectKey查询id的sql;查出id值封装给javaBean的id属性
        在运行插入的sql;就可以取出id属性对应的值
    AFTER运行顺序:
        先运行插入的sql(从序列中取出新值作为id);
        再运行selectKey查询id的sql;
     -->

    <selectKey keyProperty="id" order="BEFORE" resultType="Integer">
        <!-- 编写查询主键的sql语句 -->
        <!-- BEFORE-->
        select EMPLOYEES_SEQ.nextval from dual 
        <!-- AFTER:
         select EMPLOYEES_SEQ.currval from dual -->
    </selectKey>
    
    <!-- 插入时的主键是从序列中拿到的 -->
    <!-- BEFORE:-->
    insert into employees(EMPLOYEE_ID,LAST_NAME,EMAIL) 
    values(#{id},#{lastName},#{email<!-- ,jdbcType=NULL -->}) 
    <!-- AFTER:
    insert into employees(EMPLOYEE_ID,LAST_NAME,EMAIL) 
    values(employees_seq.nextval,#{lastName},#{email}) -->
</insert>

解释:before时,selectKey会查询到下一个主键封装给keyProperty指定的javabean的id属性,在使用该id插入纪录,after相反,只是取主键的方法是oracle序列的当前id,建议使用before,因为多人操作时,after拿到的是最后一次执行的id值,信息不正确

三、update

<!-- public void updateEmp(Employee employee);  -->
<update id="updateEmp">
    update tbl_employee 
    set last_name=#{lastName},email=#{email},gender=#{gender}
    where id=#{id}
</update>

四、delete

<!-- public void deleteEmpById(Integer id); -->
<delete id="deleteEmpById">
    delete from tbl_employee where id=#{id}
</delete>

转载于:https://www.cnblogs.com/laoyin666/p/10605012.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值