前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mybatis的dao层实现 接口代理方式实现规范+plugins-PageHelper

Mybatis的dao层实现 接口代理方式实现规范+plugins-PageHelper

作者头像
一只胡说八道的猴子
发布2020-11-12 10:16:28
3210
发布2020-11-12 10:16:28
举报
文章被收录于专栏:Java系列学习与数据结构算法

Mybatis的dao层实现 接口代理方式实现规范

Mapper接口实现时的相关规范:

Mapper接口开发只需要程序员编写Mapper接口而不用具体实现其代码(相当于我们写的Imp实现类)

Mapper接口实现时的相关规范:

1.Mapper.xml文件中的namespace与mapper接口的全限定名要相同 2.Mapper.xml文件定义的每个statement的id需要和接口的方法名相同 3.Mapper接口方法的输入参数类型和Mapper.xml中定义的每个sql的parameterType的类型相同 4.Mapper接口方法中的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同

简单演示

编写接口

代码语言:javascript
复制
package Interface;

import domain.User;

public interface UserDao {
    User findById(int id );
}

配置配置文件

代码语言:javascript
复制
<?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="Interface.UserDao">
    <select id="findById" parameterType="int">
        select * from test where id=#{id}
    </select>
</mapper>
在这里插入图片描述
在这里插入图片描述

获取方式

代码语言:javascript
复制
package Test;

import Interface.UserDao;
import domain.User;
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 java.io.IOException;
import java.io.InputStream;

public class test3 {
    public static void main(String[] args) throws IOException {
      //加载核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //获取sqlSession工厂类对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获取session对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行sql语句
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        User user = mapper.findById(1);
        System.out.println(user);
    }
}
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

再实际的开发中我们的sql语句往往是动态变化的,下面我们来介绍一下动态sql语句

在这里插入图片描述
在这里插入图片描述

我们根据实体类的不同取值来使用不同的sql语句进行查询,比如在id不为空的时候可以根据id进行查询,如果username不为空的时候加入,username一同进行查询,也就是组合查询

在这里使用语句就可以实现 if用于判断是否为空,不为空则加入查询语句中

代码语言:javascript
复制
<select id="findByCondition" parameterType="user" resultType="user">
        select  * from test
        <where>
            <if test="id!=0">
                and id=#{id}
            </if>
            <if test="username!=null">
                and username=#{username}
            </if>
        </where>
    </select>

当我们使用sql语句的时候可能会有拼接操作,比如:SELECT * FROM USER WHERE id IN (1,2,5)。 这里需要将数据与括号拼接起来,那么下面我们来讲讲怎么使用

的使用

foreach标签的属性含义如下: 标签用于遍历集合,它的属性: collection:代表要遍历的集合元素,注意编写时不要写#{} open:代表语句的开始部分 close:代表结束部分 item:代表遍历集合的每个元素,生成的变量名 sperator:代表分隔符

代码语言:javascript
复制
<select id="findByIds" parameterType="list" resultType="user">
       select * from test
       <where>
           <foreach collection="list" item="id" open="id in ("  separator="," close=")">
               #{id}
           </foreach>
       </where>
    </select>

sql片段的抽取

在我们使用sql语句的时候,sql语句中的许多内容也是重复的,所以我们可以把相同的sql语句抽取出来 中可以将重复的sql题取出来,以达到sql重用的目的

代码演示

代码语言:javascript
复制
<sql id="selectUser">
          select * from test
    </sql>
    <select id="findById" parameterType="int" resultType="user">
       <include refid="selectUser"></include> where id=#{id}
    </select>
    <select id="findByCondition" parameterType="user" resultType="user">
        <include refid="selectUser"></include>
        <where>
            <if test="id!=0">
                and id=#{id}
            </if>
            <if test="username!=null">
                and username=#{username}
            </if>
        </where>
    </select>

Mybatis映射文件配置

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Mybatis的dao层实现 接口代理方式实现规范
  • Mapper接口实现时的相关规范:
  • 简单演示
  • 再实际的开发中我们的sql语句往往是动态变化的,下面我们来介绍一下动态sql语句
  • 的使用
  • sql片段的抽取
  • Mybatis映射文件配置
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档