mybatis中的逆向工程和分页查询

一、MyBatis的逆向工程

正向工程:先创建Java实体类,由框架负责根据实体类生成数据库表。 Hibernate是支持正向工
程的。
逆向工程:先创建数据库表,由框架负责根据数据库表,反向生成如下资源:

  • Java实体类
  • Mapper接口
  • Mapper映射文件

1、创建逆向工程的步骤

1.1添加依赖

<!--mybatis-generator依赖-->
<dependency>
	<groupId>org.mybatis.generator</groupId>
	<artifactId>mybatis-generator-maven-plugin</artifactId>
	<version>1.4.1</version>
</dependency>

1.2、添加逆向工程的配置文件

在resources中进行添加,其中有些路径和配置需要根据自己的实际需要进行更改

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <!--
    targetRuntime: 执行生成的逆向工程的版本
    MyBatis3Simple: 生成基本的CRUD(清新简洁版)
    MyBatis3: 生成带条件的CRUD(奢华尊享版)
    -->
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!-- 数据库的连接信息 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC"
                        userId="root"
                        password="">
        </jdbcConnection>
        <!-- javaBean的生成策略-->
        <javaModelGenerator targetPackage="com.atguigu.mybatis.pojo" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- SQL映射文件的生成策略 -->
        <sqlMapGenerator targetPackage="com.atguigu.mybatis.mapper" targetProject=".\src\main\resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- Mapper接口的生成策略 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mybatis.mapper"
                             targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 逆向分析的表 -->
        <!-- tableName设置为*号,可以对应所有表,此时不写domainObjectName -->
        <!-- domainObjectName属性指定生成出来的实体类的类名 -->
        <table tableName="t_emp" domainObjectName="Emp"/>
        <table tableName="t_dept" domainObjectName="Dept"/>
    </context>
</generatorConfiguration>

1.3、在test中添加一个测试文件

用于生成对应数据库中的JavaBean对象和mapper接口和mapper接口对应的xml文件

import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class TestMBG {
    @Test
    public void test() throws Exception{
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        // 指定配置文件
        File configFile = new File("src/main/resources/generatorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }
}

二、分页查询

limit index,pageSize
pageSize:每页显示的条数
pageNum:当前页的页码
index:当前页的起始索引,index=(pageNum-1)*pageSize
count:总记录数
totalPage:总页数
totalPage = count / pageSize;
if(count % pageSize != 0){
totalPage += 1;
}
pageSize=4,pageNum=1,index=0 limit 0,4
pageSize=4,pageNum=3,index=8 limit 8,4
pageSize=4,pageNum=6,index=20 limit 8,4
首页 上一页 2 3 4 5 6 下一页 末页

1.1 分页插件的使用步骤

1.1.1、添加依赖

<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper</artifactId>
	<version>5.2.0</version>
</dependency>

1.1.2、配置分页插件

在核心配置文件中配置(mybatis-config.xml)

<plugins>
	<!--设置分页插件-->
	<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>

1.1.3、使用案例

package com.atguigu.mybatis.test;

import com.atguigu.mybatis.mapper.EmpMapper;
import com.atguigu.mybatis.pojo.Emp;
import com.atguigu.mybatis.utils.SqlSessionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class PageTest {
    @Test
    public void testPage(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        //查询功能之前开启分页功能
        Page<Object> page = PageHelper.startPage(1, 4);//1表示页码,4表示一页的数量
        List<Emp> emps = mapper.selectByExample(null);
        //在查询之后可以获取分页相关的所有数据
        //navigatePages表示分出多少个页码例1 2 3 4 5显示当前页码左右的五个页码
        PageInfo<Emp> pageInfo=new PageInfo<>(emps,5);

        System.out.println(emps);
        System.out.println(pageInfo);
        sqlSession.close();
    }
}

1.1.4、分页插件的使用

a>在查询功能之前使用PageHelper.startPage(int pageNum, int pageSize)开启分页功能
pageNum:当前页的页码
pageSize:每页显示的条数
b>在查询获取list集合之后,使用PageInfo pageInfo = new PageInfo<>(List list, int
navigatePages)获取分页相关数据
list:分页之后的数据
navigatePages:导航分页的页码数
c>分页相关数据
PageInfo{

pageNum=1, pageSize=4, size=4, startRow=1, endRow=4, total=30, pages=8,

list=Page{count=true, pageNum=1, pageSize=4, startRow=0, endRow=4, total=30, pages=8, reasonable=false, pageSizeZero=false}[Emp{empId=1, empName=‘a’, age=null, gender=‘null’, deptId=null},

Emp{empId=2, empName=‘a’, age=null, gender=‘null’, deptId=null},

Emp{empId=3, empName=‘a’, age=null, gender=‘null’, deptId=null},

Emp{empId=4, empName=‘a’, age=null, gender=‘null’, deptId=null}],

prePage=0, nextPage=2, isFirstPage=true, isLastPage=false, hasPreviousPage=false, hasNextPage=true, navigatePages=5, navigateFirstPage=1, navigateLastPage=5, navigatepageNums=[1, 2, 3, 4, 5]

}
pageNum:当前页的页码
pageSize:每页显示的条数
size:当前页显示的真实条数
total:总记录数
pages:总页数
prePage:上一页的页码
nextPage:下一页的页码
isFirstPage/isLastPage:是否为第一页/最后一页
hasPreviousPage/hasNextPage:是否存在上一页/下一页
navigatePages:导航分页的页码数
navigatepageNums:导航分页的页码,[1,2,3,4,5]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis逆向工程至尊版是一个自动生成MyBatis mapper和Java Bean的工具,它并不直接提供分页查询的功能,但是我们可以在生成的mapper.xml文件手动实现分页查询。以下是一种实现方式: 1. 在mapper.xml文件添加一个查询总数的语句,用于计算总记录数。例如: ``` <select id="countByExample" parameterType="com.example.model.UserExample" resultType="java.lang.Integer"> select count(*) from user <if test="example != null"> <include refid="Example_Where_Clause"/> </if> </select> ``` 2. 在mapper.xml文件添加一个分页查询的语句,用于获取指定页码和每页记录数的数据。例如: ``` <select id="selectByExample" parameterType="com.example.model.UserExample" resultMap="BaseResultMap"> select * from user <if test="example != null"> <include refid="Example_Where_Clause"/> </if> order by id asc limit #{offset,jdbcType=INTEGER}, #{limit,jdbcType=INTEGER} </select> ``` 其,#{offset}示查询的起始位置,#{limit}示每页记录数。 3. 在Java代码调用分页查询的方法,例如: ``` int pageNum = 1; int pageSize = 10; UserExample example = new UserExample(); example.setOrderByClause("id asc"); PageHelper.startPage(pageNum, pageSize); List<User> userList = userMapper.selectByExample(example); ``` 其,PageHelper是一个开源的分页插件,用于自动拦截MyBatis的查询语句,实现分页查询。在上述代码,PageHelper.startPage(pageNum, pageSize)示从第1页开始查询,每页10条记录,PageHelper会自动计算出查询的起始位置和每页记录数,然后调用selectByExample方法进行查询。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值