Mybatis学习笔记(四)

前面涉及到了一个解决办法就是起别名,后面还是将resultMap来引入解决问题

(84条消息) resultMap的用法以及关联结果集映射_正在努力的陈序员的博客-CSDN博客_resultmap​​​​​​

可以使用resultmap来进行映射,id表示的是主键,result表示的是其他的非主键

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mapper.BookMapper">
    <resultMap id="bookmap" type="book">
        <id property="id" column="bookid"></id>
        <result property="name" column="bookname"></result>
    </resultMap>
    <select id="getAll" resultMap="bookmap">
        select bookid ,bookname
        from book
    </select>
</mapper>
    <!--    List<Users> getAll();-->
    <!--    我不太清楚 resultType里面的参数是不是 可以换成任意的 只要是数据类型一致的-->
<!--    <sql id="allColumns">-->
<!--        id,username,birthday,sex,address-->
<!--    </sql>-->

表之间的关联关系

关联关系是有方向的 

一对多反过来就是多对一

一对一

多对多 园区里面的车位和园区里面的每一辆车,任意的车和任意的车位

 2表示的是对应的关系 4表示的是为了的意思

主键表和外键表

主键表就是当前有主键被别人引用当成了外键

一对多进行表的连接和调用:

package org.example.mapper;
import org.example.pojo.Customer;
import java.util.List;
public interface CustomerMapper {
    List<Customer> getById(int id);
}


package org.example;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.mapper.CustomerMapper;
import org.example.pojo.Customer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class TestCustomer {
    SqlSession sqlSession;
    CustomerMapper mapper;
    @Before
    public void createSession() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        sqlSession = build.openSession();
        mapper = sqlSession.getMapper(CustomerMapper.class);
    }
    @After
    public void closeSession(){
        sqlSession.close();

    }
    @Test
    public void test01(){
        List<Customer> byId = mapper.getById(1);
        byId.forEach(lala->System.out.println(lala));

    }
}



//这里用list放进去很有趣
package org.example.pojo;

import java.util.List;

public class Customer {
    private Integer id;
    private String name;
    private Integer age;
    List<Orders> listOrders;

    public Customer() {
    }

    public Customer(Integer id, String name, Integer age, List<Orders> listOrders) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.listOrders = listOrders;
    }

    public Customer(String name, Integer age, List<Orders> listOrders) {
        this.name = name;
        this.age = age;
        this.listOrders = listOrders;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public List<Orders> getListOrders() {
        return listOrders;
    }

    public void setListOrders(List<Orders> listOrders) {
        this.listOrders = listOrders;
    }
}


<!-- typemap YYDS-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mapper.CustomerMapper">
<!--    List<Customer> getById(int id);-->
    <resultMap id="customermap" type="customer">
        <id property="id" column="cid"></id>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <collection property="listOrders" ofType="org.example.pojo.Orders">
            <id property="id" column="oid"></id>
            <result property="orderNumber" column="orderNumber"></result>
            <result property="orderPrice" column="orderPrice"></result>
        </collection>
    </resultMap>
    <select id="getById" parameterType="int" resultMap="customermap">
        select c.id cid,name,age,o.id oid,orderNumber,orderPrice,orderPrice,customer_id
        from customer c inner join orders o on c.id = o.customer_id
        where c.id = #{cid}
    </select>
</mapper>

多对一 多对一是对象 一对多是集合

package org.example.pojo;

public class Orders {
    private Integer id;
    private String orderNumber;
    private Double orderPrice;
    private Customer customer;

    public Orders(Integer id, String orderNumber, Double orderPrice, Customer customer) {
        this.id = id;
        this.orderNumber = orderNumber;
        this.orderPrice = orderPrice;
        this.customer = customer;
    }

    public Orders() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getOrderNumber() {
        return orderNumber;
    }

    public void setOrderNumber(String orderNumber) {
        this.orderNumber = orderNumber;
    }

    public Double getOrderPrice() {
        return orderPrice;
    }

    public void setOrderPrice(Double orderPrice) {
        this.orderPrice = orderPrice;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    @Override
    public String toString() {
        return "Orders{" +
                "id=" + id +
                ", orderNumber='" + orderNumber + '\'' +
                ", orderPrice=" + orderPrice +
                ", customer=" + customer +
                '}';
    }
}


package org.example.mapper;

import org.example.pojo.Orders;

public interface OrdersMapper {
    Orders getById(Integer in);
}



<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mapper.OrdersMapper">

<!--    Orders getById(Integer in);-->
    <resultMap id="ordersmap" type="orders">
        <id property="id" column="id"></id>
        <result property="orderNumber" column="orderNumber"></result>
        <result property="orderPrice" column="orderPrice"></result>
        <association property="customer" javaType="customer">
            <id property="id" column="id"></id>
            <result property="name" column="name"></result>
            <result property="age" column="age"></result>
        </association>
    </resultMap>
    <select id="getById" parameterType="int" resultMap="ordersmap">
        select o.id oid,orderNumber,orderPrice,c.id cid,name,age
        from orders o inner join customer c
        on o.customer_id = c.id
        where o.id=#{id}
    </select>
</mapper>

多对多 可以通过中间表来实现优化

 

 

 事务自动提交可以在factory创建sqlsession的时候设置为true

 mybatis框架提供两级一级缓存的,默认为一级缓存,缓存就是为了提高查询的效率

数据库发生改变,则缓存清空

 缓存的作用域:

 

 一级缓存是sqlSession的作用域

二级缓存是mapper的作用域

 

 第一次取和第二次取的数值是一样的

 开启二级缓存的方法

 ORM解释:对象关系映射和持久化是一个意思,就是对象和数据的映射关系

 mybatis是ORM或者说是持久化非常优秀的框架

动力节点2022最新Mybatis框架教程-快速搞定MyBatis框架_哔哩哔哩_bilibili

 七个规范:

 

 

 

 

 

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值