Mybatis基础

Mybatis使用

映射器配置使用

介绍
1.使用方法,创建接口,定义方法

public interface SuperManMapper {
	 List<SuperMan> findAll();
}

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">
<!--
	namespace="接口的完全限定名"
	 select id="接口定义的方法名"
 -->
<mapper namespace="com.cn.dao.SuperManMapper">
    <!--
        查询 -普通查询
    -->
    <select id="findAll" resultType="com.cn.domain.SuperMan">
        select * from superman
    </select>
    </mappe>

3 Test

  @Test
 public void findAllTxet(){
     SqlSession sqlSession = MyBatisUtil.openSession();
     //使用API getMapper(参数:接口的字节码文件)
     SuperManMapper mapper = sqlSession.getMapper(SuperManMapper.class);
     List<SuperMan> all = mapper.findAll();
     all.forEach(e-> System.out.println(e));

4 原理:在定义接口之后,没有写相应的实现,是在测试里传入了接口的字节码文件,通过代理实现了接口的方法,命名空间(namespace)写接口的完全限定名,是与对应的接口,建立关联,而id与方法对应,执行相应的sql
高级查询
模糊查询
1.使用concat【sql内置函数】

 <sql id="queryRepeatSql">
     <where>
         <if test="name != null and name != ''">
             and name like concat('%',#{name},'%')
         </if>
         <if test="id != null">
             and id = #{id}
         </if>
     </where>
 </sql>

查询数据总数
1.使用count【内置函数】

<!--
     高级查询(总数量)
 -->
 <select id="findByNumber" parameterType="com.cn.query.SuperManQuery" resultType="long">
     select count(*) from superman
     <include refid="queryRepeatSql"/>
 </select>

批量删除
1.参数list

  <!--
     批量删除
     collection:数据源(参数)
     open:以什么开头
     close:以什么结尾
     separator:中间隔开
     item:循环的每个值
     sql:DELETE FROM 表名称 WHERE 列名称 =-->
 <delete id="deleteBatch" parameterType="List">
     DELETE FROM superman WHERE id IN
     <foreach collection="list" open="(" close=")" item="v" separator=",">
         #{v.id}
     </foreach>
 </delete>

批量添加

  <!--
       批量保存
       inser
   -->
   <insert id="insertBatch" parameterType="list">
       insert into superman (name) values
       <foreach collection="list" item="emp" separator=",">
           (#{emp.name})
       </foreach>
   </insert>

多表查询【多对一(嵌套结果)】

   <!--
     多表查询【多对一】
     结果嵌套
   -->
 <resultMap id="SuperManMap" type="com.cn.domain.SuperMan">
     <id property="id" column="sid"/>
     <result property="name" column="sname"/>
     <association property="huMan" javaType="HuMan">
         <id property="id" column="hid"/>
         <result property="name" column="hname"/>
     </association>
 </resultMap>
 <select id="findByAssociated" resultMap="SuperManMap">
       select h.name hname,h.id hid,s.id sid,s.name sname from superman s
       JOIN human h
       ON s.human_id = h.id
 </select>

多表查询【多对一(嵌套查询)】

<!--
      多表查询【多对一】
      嵌套查询
      resultMap id:属性:标识作用,引用
      type:返回的类型
  -->
  <resultMap id="SuperManMapTwo" type="com.cn.domain.SuperMan">
      <id property="id" column="id"/>
      <result property="name" column="name"/>
      <association property="huMan" column="human_id" javaType="huMan" select="com.cn.dao.HuManMapper.findById"/>
  </resultMap>
  <select id="findAllTwo" resultMap="SuperManMapTwo">
        select * from superman
  </select>

多表查询【一找多(嵌套查询)】

<!--

     多表查询【一找多】
 -->
 <select id="findById" parameterType="long" resultType="SuperMan">
       select * from superman where human_id = #{id}
 </select>
  <!--
     多表查询【一对多】
     <collection  : 一找多的标签
     嵌套查询
 -->
 <resultMap id="HuManMapTwo" type="com.cn.domain.HuMan">
     <id property="id" column="id"/>
     <result property="name" column="name"/>
     <collection property="list" ofType="com.cn.domain.SuperMan" column="id" select="com.cn.dao.SuperManMapper.findById" />
 </resultMap>
 <select id="findAllTwo" resultMap="HuManMapTwo">
      select * from human
 </select>

多表查询【一找多(嵌套结果)】

 <!--
       多表查询【一对多】
       <collection  : 一找多的标签
       嵌套结果
   -->
   <resultMap id="HuManMap" type="com.cn.domain.HuMan">
       <id property="id" column="hid"/>
       <result property="name" column="hname"/>
       <collection property="list" ofType="SuperMan">
           <id property="id" column="id"/>
           <result property="name" column="name"/>
           <result property="huMan" column="human_id"/>
       </collection>
   </resultMap>
   <select id="findAll" resultMap="HuManMap">
       select h.id hid,h.name hname,s.* from human h join superman s on h.id=s.human_id
   </select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值