MyBatils基础

MyBatils基础

核心配置文件

核心配置文件包含了MyBatils最核心的设置和属性信息。如数据库的连接、事务、连接池信息等

<?xml version="1.0" encoding="UTF-8" ?>
<!--MyBatils的DTD约束-->
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

<!--configuration 核心根标签-->
<configuration>
    <!--引入配置文件信息,resource代表路径-->
    <properties resource="jdbc.properties"/>

    <settings>
        <setting name="logImpl" value="log4j"/>
    </settings>

    <!--起别名-->
    <typeAliases>
        <typeAlias type="com.itheima01.bean.Student" alias="student"/>
        <!--<package name="com.itheima01.bean"/>-->
    </typeAliases>

    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>

    <!--environments 配置数据库环境,环境可以有多个,default属性指定默认使用哪个-->
    <environments default="mysql">
        <!--enviroment配置数据库环境 id属性唯一-->
    <environment id="mysql">

        <!--transactionMapper事务管理-->
        <transactionManager type="JDBC"></transactionManager>
        <!--dataSource数据源信息-->

        <dataSource type="POOLED">
            <!--获取数据库连接的配置信息-->
            <property name="driver" value="${driver}"/>
            <property name="url" value="${url}"/>
            <property name="username" value="${username}"/>
            <property name="password" value="${password}"/>
        </dataSource>

    </environment>
    </environments>

    <!--引入映射配置文件-->
    <mappers>
        <!--引入指定的映射配置文件-->
        <mapper resource="StudentMapper.xml"/>
    </mappers>
</configuration>

起别名

给一个类定义别名

<typeAlias type="com.itheima01.bean.Student" alias="student"/>

给一个包定义别名

<package name="com.itheima01.bean"/>
<!-默认别名是类名->

查看idea中文件的历史记录

  1. 右键文件
  2. Local History
  3. show History

MyBatils 进阶

三层架构

分层思想:控制层(controller)、业务层(service)、持久层(dao)

调用过程:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HzkbD4vE-1592488988898)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20200617132211327.png)]

接口代理

动态代理的好处:接口的代理对象由程序在执行的过程中动态生成,不用我们自己去写一个类实现接口中所有的方法,可以动态生成任意接口的对象

作用:动态的创建Mapper接口的实现类

实现规则:

  • 映射配置文件中的名称空间和Dao层接口的全类名相同

  • 映射配置文件中的增删改查标签的id属性必须和Dao层接口的方法名相同

  • 映射配置文件中的增删改查标签的参数类型必须和接口方法相同

  • 映射配置文件中的增删改查标签的返回值类型必须和接口方法相同

获取动态代理对象

SqlSession功能中的getMapper()方法

<!-动态代理对象,名字空间必须是接口的全名->
<mapper namespace="com.itheima01.mapper.StudentMapper">

Proxy类中的方法

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1S2tnfmE-1592488988900)(D:\develop\学习笔记\img\1557839380147-1589417530199.png)]

InvocationHandler接口

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-J12WxeGD-1592488988901)(D:\develop\学习笔记\img\1557839397638.png)]

接口代理的方式实现dao层

传统方式实现Dao层,我们既要写接口,还要写实现类。而MyBatis框架可以帮助我们省略编写Dao层接口实现类的步骤。程序员只需要编写接口,由MyBatis框架根据接口的定义来创建该接口的动态代理对象

1.修改映射配置文件

<mapper namespace="com.itheima01.mapper.StudentMapper">

2.修改service层接口的实现类,采用代理的方式实现功能

//先定义一个StudentMapper类的对象,下面的方法中直接调用即可
private StudentMapper studentMapper;
//静态代码块,每次执行直接获取动态代理对象
public StudentServiceImpl(){
    List<Student> list=null;
    SqlSession sqlSession=null;
    InputStream is=null;
    try{
        //1.加载核心配置文件
        is = Resources.getResourceAsStream("MyBatisConfig.xml");

        //2.获取SqlSession工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        //3.通过工厂对象获取SqlSession对象
        sqlSession = sqlSessionFactory.openSession();

        //4.获取StudentMapper接口的实现类
        studentMapper = sqlSession.getMapper(StudentMapper.class);

    }catch (Exception e){
        e.printStackTrace();
    }
}
@Override
public List<Student> selectAll() {
    return studentMapper.selectAll();
}

动态SQL

MyBatis映射配置文件中,前面我们的SQL都是比较简单的,有些时候业务逻辑复杂时,我们的SQL就是动态变化的。

if标签的使用

<!--id:相当于方法名,resultType:返回值类型,parameType:传入参数类型-->
<select id="selectCondition" resultType="student" parameterType="student">
    SELECT * FROM student
    <where>
        <if test="sid !=null">
            sid = #{sid}
        </if>
<!--where标签会自动帮我们把多余的and消除-->
        <if test="name !=null">
            AND name = #{name}
        </if>
        <if test="age !=null">
            AND age = #{age}
        </if>
        <if test="birthday !=null">
            AND birthday = #{birthday}
        </if>
    </where>
</select>

foreach标签的使用

<select id="selectByIds" resultType="student" parameterType="list">
    SELECT * FROM student
    <where>
    
        <foreach collection="list" open="sid IN (" close=")" item="sid" separator=",">
            #{sid}
        </foreach>
    </where>
</select>

分页插件的使用

1.导入jar包

2.导入核心配置文件

<plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>

3.测试

//4.获取StudentMapper接口的实现类对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

PageHelper.startPage(1,3);

第一个参事:显示第几页

第二个参数:每页显示几条数据

分页插件的相关参数

  • PageInfo:封装分页相关参数的功能类

  • 核心方法

    返回值 方法名 说明
    long getTotal() 获取总条数
    int getPages 获取总页数
    int getPageNum 获取当前页
    int getPageSize 获取每页显示条数
    int getPrePage 获取上一页
    int getNextPage 获取下一页

|
| long | getTotal() | 获取总条数 |
| int | getPages | 获取总页数 |
| int | getPageNum | 获取当前页 |
| int | getPageSize | 获取每页显示条数 |
| int | getPrePage | 获取上一页 |
| int | getNextPage | 获取下一页 |

猜你喜欢

转载自blog.csdn.net/weixin_42478562/article/details/106844227