前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mybatis代理开发、动态sql、核心配置文件

Mybatis代理开发、动态sql、核心配置文件

作者头像
咕咕星
发布2021-01-05 09:49:28
3130
发布2021-01-05 09:49:28
举报
文章被收录于专栏:咕咕星

1、Mybatis代理开发

只需要编写Mapper 接口,Mybatis 框架根据接口定义创建接口的动态代理对象

Mapper 接口开发需要遵循以下规范:

Mapper.xml文件中的namespace与mapper接口的全限定名相同

2、MyBatis动态sql

在Mapper.xml文件中可以使用:<where><if><foreach>等标签

例子一:

代码语言:javascript
复制
<select id="findUserByCondition" resultType="user" parameterType="user">
    select * from user
    <where>
        <if test="id!=0">
            and id = #{id}
        </if>
        <if test="username!=null">
            and username = #{username}
        </if>
    </where>
</select>
代码语言:javascript
复制
@Test
//MyBatis动态sql
public void test6() throws IOException {
    InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    SqlSession sqlSession = sqlSessionFactory.openSession();

    //测试数据
    User condition  = new User();
    condition.setUsername("lisi");

    //获得MyBatis框架生成的UserMapper接口的实现类
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    User userByCondition = userMapper.findUserByCondition(condition );
    System.out.println(userByCondition);

    sqlSession.close();

}

例子二:

代码语言:javascript
复制
<select id="findUserByIds" resultType="user" parameterType="list">
    select * from user
    <where>
        <foreach collection="list" item="id" open="id in (" close=")" separator=",">
            #{id}
        </foreach>
    </where>
</select>
代码语言:javascript
复制
@Test
//MyBatis动态sql
public void test7() throws IOException {
    InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    SqlSession sqlSession = sqlSessionFactory.openSession();

    //测试数据
    List arrayList = new ArrayList();
    arrayList.add(1);
    arrayList.add(2);

    //获得MyBatis框架生成的UserMapper接口的实现类
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    List<User> userList = userMapper.findUserByIds(arrayList);
    System.out.println(userList);

    sqlSession.close();

}

foreach标签的属性:

  • collection:代表要遍历的集合元素,注意编写时不要写#{}
  • open:代表语句的开始部分
  • close:代表结束部分
  • item:代表遍历集合的每个元素,生成的变量名
  • sperator:代表分隔符

3、SQL片段抽取

代码语言:javascript
复制
<!--将重复的sql提取出来-->
<sql id="selectAll">select * from user</sql>

<select id="findUserByIds" resultType="user" parameterType="list">
    <!--用include引用-->
    <include refid="selectAll"></include>
    <where>
        <foreach collection="list" item="id" open="id in (" close=")" separator=",">
            #{id}
        </foreach>
    </where>
</select>

4、MyBatis核心配置文件

4.1typeHandlers标签

重写类型处理器或创建你自己的类型处理器来处理不支持的或非标准的类型

开发步骤:

定义转换类继承类(指定泛型)

在MyBatis核心配置文件中进行注册

4.2plugins标签

MyBatis可以使用第三方的插件来对功能进行扩展,比如:分页助手PageHelper

开发步骤:

导入通用PageHelper的坐标

在mybatis核心配置文件中配置PageHelper插件

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-01-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、Mybatis代理开发
  • 2、MyBatis动态sql
  • 3、SQL片段抽取
  • 4、MyBatis核心配置文件
    • 4.1typeHandlers标签
      • 4.2plugins标签
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档