阅读 149

Mybatis学习五:逆向工程与分页插件

Mybatis学习五:逆向工程与分页插件

目录


一、MBG简介


二、逆向工程配置


三、MBG使用测试:


   1.基本查询的测试


  2.带条件查询的测试


四、PageHelper 分页插件


五、PageHelper使用步骤:


六、测试使用


   1.Page对象:


  2.PageInfo对象:


一、MBG简介

        MyBatis Generator: 简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等这些复杂sql的定义需要我们手工编写


官方文档地址 http://www.mybatis.org/generator/;官方工程地址:https://github.com/mybatis/generator/releases。


二、逆向工程配置

导入逆向工程的jar包:mybatis-generator-core-1.3.2.jar

配置mbg.xml

运行代码生成器生成代码

 // ***********************************一、mbg.xml  ***********************************

<?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.jdbc.Driver"

        connectionURL="jdbc:mysql://localhost:3306/ssm"

        userId="root"

        password="root">

    </jdbcConnection>

<!-- javaBean的生成策略-->

    <javaModelGenerator targetPackage="com.mybatis.beans" targetProject=".\src">

      <property name="enableSubPackages" value="true" />

      <property name="trimStrings" value="true" />

    </javaModelGenerator>

<!-- SQL映射文件的生成策略 -->

    <sqlMapGenerator targetPackage="com.mybatis.dao"  targetProject=".\conf">

      <property name="enableSubPackages" value="true" />

    </sqlMapGenerator>

<!-- Mapper接口的生成策略 -->

    <javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis.dao"  targetProject=".\src">

      <property name="enableSubPackages" value="true" />

    </javaClientGenerator>

<!-- 逆向分析的表 -->

    <table tableName="tbl_dept" domainObjectName="Department"></table>

    <table tableName="tbl_employee" domainObjectName="Employee"></table>

  </context>

</generatorConfiguration>

 

 

// ************************ 二、运行代码生成器生成代码  ************************

 

public void testMBG() throws Exception {

   List<String> warnings = new ArrayList<String>();

   boolean overwrite = true;

   File configFile = new File("mbg.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);

}


三、MBG使用测试:

   1.基本查询的测试

 @Test

public void testSelect() throws Exception {

    SqlSessionFactory ssf = getSqlSessionFactory();

SqlSession session = ssf.openSession();

try {

EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);

List<Employee> emps = mapper.selectAll();

for (Employee employee : emps) {

System.out.println(employee);

}

} finally {

session.close();

}

}

 


  2.带条件查询的测试

 @Test

public void testSelect() throws Exception {

SqlSessionFactory ssf = getSqlSessionFactory();

SqlSession session = ssf.openSession();

try {

EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);

//条件查询: 名字中带有'张' 并且 email中'j'  或者 did = 2 

  //1.创建example条件:new EmployeeExample();

EmployeeExample example =  new EmployeeExample();

 

  //2.生成一个条件:criteria

        Criteria criteria = example.createCriteria();

criteria.andLastNameLike("%张%");

criteria.andEmailLike("%j%");

  //3.生成另一给条件:criteriaOr 

Criteria criteriaOr = example.createCriteria();

criteriaOr.andDIdEqualTo(2);

 

example.or(criteriaOr);

    

        List<Employee> emps = mapper.selectByExample(example);

     

        for (Employee employee : emps) {

System.out.println(employee);

}

 

} finally {

session.close();

}

}

 


四、PageHelper 分页插件

Mybatis内的插件顶层接口:Interceptor


五、PageHelper使用步骤:

导入相关包pagehelper-x.x.x.jar 和 jsqlparser-0.9.5.jar

在MyBatis全局配置文件中配置分页插件,在<environment>之前。

<plugins>


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


</plugins>


使用PageHelper提供的方法进行分页

可以使用更强大的PageInfo封装返回结果

六、测试使用

   1.Page对象:

      在查询之前通过PageHelper.startPage(页码,条数)设置分页信息,该方法返回Page对象


@Test

public void testPageHelper()  throws Exception{

SqlSessionFactory ssf = getSqlSessionFactory();

SqlSession session = ssf.openSession();

try {

EmployeeMapper mapper = 

                      session.getMapper(EmployeeMapper.class);

//设置分页信息。startPage(pageNum,pageSize);//当前页,每一页多少条。[当前页的起始索引,每页显示的条数]

Page<Object> page = PageHelper.startPage(3, 3);

List<Employee> emps = mapper.getAllEmps();

for (Employee employee : emps) {

System.out.println(employee);

}

System.out.println("=============获取分页相关的信息=================");

System.out.println("当前页: " + page.getPageNum());

System.out.println("总页码: " + page.getPages());

System.out.println("总条数: " + page.getTotal());

System.out.println("每页显示的条数: " + page.getPageSize());

} finally {

session.close();

}

}

  2.PageInfo对象:

     在查询完数据后,使用PageInfo对象封装查询结果,可以获取更详细的分页信息以及可以完成分页逻辑


    @Test

public void testPageHelper1()  throws Exception{

SqlSessionFactory ssf = getSqlSessionFactory();

SqlSession session = ssf.openSession();

try {

EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);

//设置分页信息

Page<Object> page = PageHelper.startPage(9, 1);

List<Employee> emps = mapper.getAllEmps();

 

PageInfo<Employee> info  = new PageInfo<>(emps,5);//5代表:需要5个页码。

for (Employee employee : emps) {

System.out.println(employee);

}

System.out.println("=============获取详细分页相关的信息=================");

System.out.println("当前页: " + info.getPageNum());

System.out.println("总页码: " + info.getPages());

System.out.println("总条数: " + info.getTotal());

System.out.println("每页显示的条数: " + info.getPageSize());

System.out.println("是否是第一页: " + info.isIsFirstPage());

System.out.println("是否是最后一页: " + info.isIsLastPage());

System.out.println("是否有上一页: " + info.isHasPreviousPage());

System.out.println("是否有下一页: " + info.isHasNextPage());

System.out.println("============分页逻辑===============");

int [] nums = info.getNavigatepageNums();

for (int i : nums) {

System.out.print(i +" " );

}

} finally {

session.close();

}

}

 

————————————————

版权声明:本文为CSDN博主「雄关漫道_」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_43868322/article/details/116170362


文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐