Mybatis2 - Mapper 详解

来源:https://www.bilibili.com/video/BV1V7411w7VW?p=3

上一节:https://blog.csdn.net/qq_40893824/article/details/107322689
下一节:https://blog.csdn.net/qq_40893824/article/details/107371171

接上一节 Mapper

1 statement 标签

insert
update
delete
select

2 parameterType

2.1 基本数据类型

id 查询 Account

<select id="findById" parameterType="long"
resultType="com.southwind.entity.Account">
select * from t_account where id = #{id}
</select>

2.2 String 类型

name 查询 Account
1 接口 repository/ AccountRepository 中,添加代码:
public Account findByName(String username);

2 xml文件 repository/ AccountRepository 中,添加代码:

    <select id="findByName" parameterType="String" resultType="com.southwind.entity.Account">
        select * from t_account where username = #{username}
    </select>

3 test/ Test2 中,添加代码:

        // findByName
        System.out.println(accountRepository.findByName("张三"));
        sqlSession.close();

运行:

2.3 包装类

通过 id 查询 Account
优点:数据为空,不会抛异常,基本数据 就会抛异常

1 接口 repository/ AccountRepository 中,添加代码:
public Account findById2(Long id);

2 repository/ AccountRepository.xml 中,添加代码:

    <select id="findById2" parameterType="java.lang.Long" resultType="com.southwind.entity.Account">
        select * from t_account where id = #{id}
    </select>

3 test/ Test2 中,添加代码:

        // 包装类
        Long id = Long.parseLong("1");
        System.out.println(accountRepository.findById2(id));
        sqlSession.close();

Long id = Long.parseLong("1");
不是 Long id = Long.parseLong("1L");

运行:

2.4 多参数

name 和 age 查询 Account

1 接口 repository/ AccountRepository 中,添加代码:
public Account findByNameAndAge(String name,int age);

2 repository/ AccountRepository.xml 中,添加代码:

    <select id="findByNameAndAge" resultType="com.southwind.entity.Account">
        select * from t_account where username=#{arg0} and age=#{arg1}
    </select>

3 test/ Test2 中,添加代码:

        // 多参数 name、age
        System.out.println(accountRepository.findByNameAndAge("张三",22));
        sqlSession.close();

运行:

2.5 Java Bean

javaBean 就是实体类

    <update id="update" parameterType="com.southwind.entity.Account">
        update t_account set username=#{username},password=#{password},age=#{age} where id=#{id}
    </update>

3 resultType

3.1 基本数据类型

统计 Account 总数
1 接口 repository/ AccountRepository 中,添加代码:
public int count();

2 repository/ AccountRepository.xml 中,添加代码:

    <select id="count" resultType="int">
        select count(id) from t_account
    </select>

3 test/ Test2 中,添加代码:

        // Account 的个数
        System.out.println(accountRepository.count());
        sqlSession.close();

运行:

3.2 包装类

统计 Account 总数
1 接口 repository/ AccountRepository 中,添加代码:
public Integer count2();

2 repository/ AccountRepository.xml 中,添加代码:

    <select id="count2" resultType="java.lang.Integer">
        select count(id) from t_account
    </select>

3 test/ Test2 中,添加代码:

        // 包装类 Account 的个数
        System.out.println(accountRepository.count2());
        sqlSession.close();

运行:

3.3 String 类型

通过 id 查询 Account 的 name
1 接口 repository/ AccountRepository 中,添加代码:
public String findNameById(long id);

2 repository/ AccountRepository.xml 中,添加代码:

    <select id="findNameById" resultType="java.lang.String">
        select username from t_account where id=#{id}
    </select>

3 test/ Test2 中,添加代码:

        // String 类型 通过id查name
        System.out.println(accountRepository.findNameById(1L));
        sqlSession.close();

运行:

3.4 Java Bean

    <select id="findById" parameterType="long" resultType="com.southwind.entity.Account">
        select * from t_account where id=#{id}
    </select>

4 级联查询

4.1 1对多

4.1.1 新建数据库

学生、班级就是1对多
创班级表:

use mybatis;
create table classes(
    id smallint primary key ,
    name varchar(12)
)

创学生表:

use mybatis;
create table student(
    id smallint primary key ,
    name varchar(12) ,
    cid smallint ,
    foreign key(cid) references classess(id) on update cascade
)

手动添加数据后:

4.1.2 简单查询

1 entity 中,新建 实体类 Student,加入代码:

package com.southwind.entity;

import lombok.Data;

@Data
public class Student {
    private long id;
    private String name;
    private Classes classes;
}

2 entity 中,新建 实体类 Classes,加入代码:

package com.southwind.entity;

import lombok.Data;

import java.util.List;

@Data
public class Classes {
    private long id;
    private String name;
    private List<Student> students;
}

3 repository 中,新建 接口 StudentRepository,加入代码:
public Student findById(long id);

4 repository 中,新建 StudentRepository.xml 文件类型,加入代码(可以粘贴 AccountRepository.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="com.southwind.repository.StudentRepository">
    
    <select id="findById" parameterType="long" resultType="com.southwind.entity.Student">
        select s.id, s.name, c.id , c.name from student s,classes c
            where s.id=#{id} and s.cid=c.id
    </select>
</mapper>

5 StudentRepository.xml 注册到 resources/ config.xml 中,config.xml 中加入代码:

        <!--    注册 StudentRepository.xml   -->
        <mapper resource="com/southwind/repository/StudentRepository.xml"></mapper>

6 test 中,新建 类 Test3,加入代码:

package com.southwind.test;

import com.southwind.repository.StudentRepository;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class Test3 {
    public static void main(String[] args) {
        InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        StudentRepository studentRepository = sqlSession.getMapper(StudentRepository.class);
        System.out.println(studentRepository.findById(1L));
        sqlSession.close();
    }
}

运行 Test3:

4.1.3 加入级联

上面看到,其查询结果 没有 classes,现在加入:
repository/ StudentRepository.xml 中,加入代码:

    <resultMap id="StudentMap" type="com.southwind.entity.Student">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <association property="classes" javaType="com.southwind.entity.Classes">
            <id column="cid" property="id"></id>
            <result column="cname" property="name"></result>
        </association>
    </resultMap>

resultMap="StudentMap"
其中:
<resultMap id="自定义" type="返回的类型"> 处理 不能直接映射的情况
主键:<id>
其他字段:<result>
column:数据库中名字
property:要映射的 idea 中属性名字

运行 Test3:

4.1.4 反向级联

上面 Student 级联 Classes
现在 Classes 级联 Student
1 repository 中,新建 接口 ClassesRepository,加入代码:
public Classes findById(long id);

2 repository 中,新建 ClassesRepository.xml 文件类型,加入代码(可以粘贴 AccountRepository.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="com.southwind.repository.ClassesRepository">

    <resultMap id="ClassesMap" type="com.southwind.entity.Classes">
        <id column="cid" property="id"></id>
        <result column="cname" property="name"></result>
        <collection property="students" ofType="com.southwind.entity.Student">
            <id column="id" property="id"></id>
            <result column="name" property="name"></result>
        </collection>
    </resultMap>
    
    <select id="findById" parameterType="long" resultMap="ClassesMap">
        select s.id, s.name, c.id as cid, c.name as cname from student s,classes c
            where c.id=#{id} and s.cid=c.id
    </select>
</mapper>

3 ClassesRepository.xml 注册到 resources/ config.xml 中,config.xml 中加入代码:

        <!--    注册 ClassesRepository.xml   -->
        <mapper resource="com/southwind/repository/ClassesRepository.xml"></mapper>

4 test/ Test3 中,加入代码:

        ClassesRepository classesRepository = sqlSession.getMapper(ClassesRepository.class);
        System.out.println(classesRepository.findById(2L));
        sqlSession.close();

4.2 多对多

客户 和 商品

4.2.1 新建数据库

创 客户表:

use mybatis;
create table customer(
    id smallint primary key ,
    name varchar(12)
)

创 商品表:

use mybatis;
create table goods(
    id smallint primary key ,
    name varchar(20)
)

创 中间表 - 客户商品表

use mybatis;
create table customer_goods(
    id smallint primary key ,
    cid smallint,
    foreign key(cid) references customer(id),
    gid smallint,
    foreign key (gid) references goods(id)
)

添加数据:


4.2.2级联

客户 到 商品
1 entity 中,新建 实体类 Customer,加入代码:

package com.southwind.entity;

import lombok.Data;

import java.util.List;

@Data
public class Customer {
    private long id;
    private String name;
    private List<Goods> goods;
}

2 entity 中,新建 实体类 Goods,加入代码:

package com.southwind.entity;

import lombok.Data;

import java.util.List;

@Data
public class Goods {
    private long id;
    private String name;
    private List<Customer> customers;
}

3 repository 中,新建接口 CustomerRepository,加入代码:
public Customer findById(long id);

4 repository 中,新建 CustomerRepository.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="com.southwind.repository.CustomerRepository">

    <resultMap id="customerMap" type="com.southwind.entity.Customer">
        <id column="cid" property="id"></id>
        <result column="cname" property="name"></result>
        <collection property="goods" ofType="com.southwind.entity.Goods">
            <id column="gid" property="id"></id>
            <result column="gname" property="name"></result>
        </collection>
    </resultMap>
    
    <select id="findById" parameterType="long" resultMap="customerMap">
        select c.id cid, c.name cname,g.id gid,g.name gname
            from customer c, goods g, customer_goods cg
            where c.id=#{id} and c.id=cg.cid and cg.gid=g.id
    </select>
</mapper>

5 CustomerRepository.xml注册到 resources/ config.xml,config.xml 中添加代码:

        <!--    注册 CustomerRepository.xml   -->
        <mapper resource="com/southwind/repository/CustomerRepository.xml"></mapper>

6 test/ Test3 中,添加代码:

        CustomerRepository customerRepository = sqlSession.getMapper(CustomerRepository.class);
        System.out.println(customerRepository.findById(1L));
        sqlSession.close();

运行:

4.2.3 反相级联

商品 到 客户

1 repository 中,新建接口 GoodsRepository,加入代码:
public Goods findById(long id);

2 repository 中,新建 GoodsRepository.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="com.southwind.repository.GoodsRepository">

    <resultMap id="goodsMap" type="com.southwind.entity.Goods">
        <id column="gid" property="id"></id>
        <result column="gname" property="name"></result>
        <collection property="customers" ofType="com.southwind.entity.Customer">
            <id column="cid" property="id"></id>
            <result column="cname" property="name"></result>
        </collection>
    </resultMap>

    <select id="findById" parameterType="long" resultMap="goodsMap">
        select c.id cid, c.name cname, g.id gid, g.name gname 
            from customer c, goods g, customer_goods cg
            where g.id=#{id} and g.id=cg.gid and cg.cid=c.id
    </select>
</mapper>

3 GoodsRepository.xml 注册到 resources/ config.xml,config.xml 中添加代码:

        <!--    注册 GoodsRepository.xml   -->
        <mapper resource="com/southwind/repository/GoodsRepository.xml"></mapper>

4 test/ Test3 中,加入代码:

        GoodsRepository goodsRepository = sqlSession.getMapper(GoodsRepository.class);
        System.out.println(goodsRepository.findById(1L));
        sqlSession.close();

运行:

上一节:https://blog.csdn.net/qq_40893824/article/details/107322689
下一节:https://blog.csdn.net/qq_40893824/article/details/107371171

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_1403034144

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值