美文网首页
MyBatis学习

MyBatis学习

作者: 抬头挺胸才算活着 | 来源:发表于2021-11-30 09:14 被阅读0次
  • MyBatis使用XML步骤总结
    1)配置mybatis-config.xml 全局的配置文件 (1、数据源,2、外部的mapper)
    2)创建SqlSessionFactory
    3)通过SqlSessionFactory创建SqlSession对象
    4)通过SqlSession操作数据库 CRUD
    5)调用session.commit()提交事务
    6)调用session.close()关闭会话

  • MyBatis使用注解步骤总结
    1)配置mybatis-config.xml 全局的配置文件 (1、数据源,2、外部的mapper)
    1.1使用Mapper类加上方法和注解

public interface CategoryMapper {
    @Insert(" insert into category_ ( name ) values (#{name}) ")
    public int add(Category category);

    @Delete(" delete from category_ where id= #{id} ")
    public void delete(int id);

    @Select("select * from category_ where id= #{id} ")
    public Category get(int id);

    @Update("update category_ set name=#{name} where id=#{id} ")
    public int update(Category category);

    @Select(" select * from category_ ")
    public List<Category> list();
}

1.2 mybatis-config.xml配置Mapper类

    <mappers>
        <mapper class="pojo.CategoryMapper"/>
    </mappers>

2)创建SqlSessionFactory
3)通过SqlSessionFactory创建SqlSession对象
4)通过SqlSession操作数据库 CRUD

String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();

CategoryMapper mapper = session.getMapper(CategoryMapper.class);
List<Category> cs = mapper.list();
cs.forEach(c -> System.out.println(c));

session.commit();
session.close();

5)调用session.commit()提交事务
6)调用session.close()关闭会话

  • join语句的多对一和一对多
    一对多:
    <resultMap type="Category" id="categoryBean">
        <id column="cid" property="id" />
        <result column="cname" property="name" />

        <!-- 一对多的关系 -->
        <!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
        <collection property="products" ofType="Product">
            <id column="pid" property="id" />
            <result column="pname" property="name" />
            <result column="price" property="price" />
        </collection>
    </resultMap>

    <!-- 关联查询分类和产品表 -->
    <select id="listCategory" resultMap="categoryBean">
        select
            c.*,
            p.*,
            c.id 'cid',
            p.id 'pid',
            c.name 'cname',
            p.name 'pname'
        from category_ c left join product_ p on c.id = p.cid
    </select>

多对一:同上,只不过用的标签是<association>
<collection>和<association>还可以不断地嵌套,实现多对多。

  • 动态SQL
    <if>:
    可以查询全部或者模糊查询
    <select id="listProduct" resultType="Product">
            select * from product_
            <if test="name != null">
            where name like concat('%', #{name}, '%')
            </if>
    </select>

<where>:
当只有name没有price的时候下面的SQL语句会出错,因此我们需要<where>标签。

<select id="listProduct" resultType="Product">
    select * from product_
    <if test="name!=null">
        where name like concat('%',#{name},'%')
    </if>           
    <if test="price!=0">
        and price > #{price}
    </if>           
</select>

<where>标签会进行自动判断
如果任何条件都不成立,那么就在sql语句里就不会出现where关键字
如果有任何条件成立,会自动去掉多出来的 and 或者 or。

<select id="listProduct" resultType="Product">
    select * from product_
    <where>
        <if test="name!=null">
            and name like concat('%',#{name},'%')
        </if>           
        <if test="price!=null and price!=0">
            and price > #{price}
        </if>   
    </where>        
</select>

<set>:
与类似的,在update语句里也会碰到多个字段相关的问题。 在这种情况下,就可以使用set标签:

        <set>
            <if test="name != null">name=#{name},</if>
            <if test="price != null">price=#{price}</if>
              
        </set>
  • 缓存
    一级缓存:打开同一个session,取同样id的数据,只执行一次SQL
    二级缓存:打开同一个SessionFactory,取同样id的数据,只执行一次SQL

  • 逆向工程generator工具
    前面的学习都是先有pojo、mapper、xml,再创建表。
    逆向工程是根据表再创建pojo、mapper、xml。

相关文章

  • MyBatis学习(转载链接)

    MyBatis学习 之 一、MyBatis简介与配置MyBatis+Spring+MySql MyBatis学习 ...

  • Spring系列 | 小荷才露尖尖角

    MyBatis学习笔记 MyBatis操练 MyBatis源码 SpringMVC学习笔记 SpringMVC...

  • MyBatis缓存和注解

    Mybatis缓存和注解 学习目标 1、mybatis缓存 2、mybatis注解 学习内容 1、mybatis缓...

  • Mybatis 动态SQL编写

    Mybatis学习 mybatis http://www.mybatis.org/mybatis-3/zh/ind...

  • MyBatis

    MyBatis学习总结(一)——MyBatis快速入门 超详细MyBatis入门讲解

  • MyBatis缓存

    MyBatis Mybatis笔记连载上篇连接Mybatis简单操作学习 Mybatis笔记连载下篇连接Mybat...

  • Mybatis快速入门

    Mybatis 学习内容 Mybatis框架的简单入门 Mybatis框架基本的使用 Mybatis框架的深入和多...

  • Mybatis 学习笔记

    Mybatis 学习笔记 配置 MyBatis 环境 导入MyBaits依赖jar包 要使用 MyBatis, 需...

  • 1.Mybatis - 搭建

    参考 Mybatis 官方 MyBatis学习总结(一)——MyBatis快速入门 安装 说明基于Maven 步骤...

  • MyBatis概述

    【目录】1 什么是MyBatis2 为什么要使用MyBatis3 MyBatis学习路线 1 什么是MyBatis...

网友评论

      本文标题:MyBatis学习

      本文链接:https://www.haomeiwen.com/subject/eyzhxrtx.html