zoukankan      html  css  js  c++  java
  • Mybatis的dao层实现 接口代理方式实现规范+plugins-PageHelper

    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的类型相同

    简单演示

    编写接口

    package Interface;
    
    import domain.User;
    
    public interface UserDao {
        User findById(int id );
    }
    

    配置配置文件

    <?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>
    

    在这里插入图片描述
    获取方式

    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用于判断是否为空,不为空则加入查询语句中

    <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:代表分隔符

    <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重用的目的

    代码演示

    <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映射文件配置

  • 相关阅读:
    面试常考点:http和https的区别与联系
    常见的反爬虫和应对方法
    2019/1/1 Python今日收获
    2018/12/26,12/27 Python今日收获
    2018/6/7-6/8 Python今日收获
    2018/6/6 Python今日收获
    CSS(3)——visited伪类
    CSS中margin和padding的区别
    CSS(2)——CSS的文字,边框,背景与列表
    CSS(1)——CSS的引入方式与选择器
  • 原文地址:https://www.cnblogs.com/pjhaymy/p/13924574.html
Copyright © 2011-2022 走看看