MyBatis逆向工程与分页插件

MyBatis逆向工程

简介

是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。支持基本的增删改查,以及QBC风格的条件查询。
官方文档地址
http://www.mybatis.org/generator/
官方工程地址
https://github.com/mybatis/generator/releases

配置

  1. 导入逆向工程的jar包
    mybatis-generator-core-1.3.2.jar
  2. 编写MBG的配置文件
<?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>



  <context id="DB2Tables" targetRuntime="MyBatis3">

     <!-- 设置连接数据库的信息 -->

    <jdbcConnection driverClass="com.mysql.jdbc.Driver"

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

        userId="root"

        password="liushuai">

    </jdbcConnection>



    <javaTypeResolver >

      <property name="forceBigDecimals" value="false" />

    </javaTypeResolver>



     <!-- <u>javabean</u>的生成策略 -->

    <javaModelGenerator targetPackage="com.atguigu.bean" 
targetProject=".\src">

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

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

    </javaModelGenerator>





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

    <sqlMapGenerator targetPackage="com.atguigu.mapper"  
targetProject=".\conf">

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

    </sqlMapGenerator>



     <!-- <u>mapper</u>接口的生成策略 -->

    <javaClientGenerator type="XMLMAPPER" 
targetPackage="com.atguigu.mapper"  
targetProject=".\src">

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

    </javaClientGenerator>



     <!-- 设置要将数据库中的哪张表逆向生成哪一个<u>javabean</u> 
-->

     <table tableName="emp" 
domainObjectName="Emp"></table>

     <table tableName="dept" 
domainObjectName="Dept"></table>



  </context>

</generatorConfiguration>


  1. 运行代码生成器生成代码
@Test

     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.xml中设置的相同,得到如下mapper和bean文件

扩展-PageHelper分页插件

使用步骤

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

  2. 在MyBatis全局配置文件中配置分页插件

<!-- 分页插件设置 -->

     <plugins>

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

     </plugins>
  1. 建立一个util工具类
public class PageUtil {



     public static String getPageInfo(PageInfo<Emp> 
pageInfo, HttpServletRequest request) {

          

          String path = request.getContextPath() + "/";

          StringBuilder builder = new StringBuilder();

          

          //拼接首页

          builder.append("<a href='"+path+"emps/1'>首页</a>");

          builder.append("&nbsp;&nbsp;");

          

          //拼接上一页

          if(pageInfo.isHasPreviousPage()) {

              builder.append("<a 
href='"+path+"emps/"+pageInfo.getPrePage()+"'>上一页</a>");

              builder.append("&nbsp;&nbsp;");

          }else {

              builder.append("上一页");

              builder.append("&nbsp;&nbsp;");

          }

          

          //拼接页码

          int[] nums = pageInfo.getNavigatepageNums();

          for (int i : nums) {

              if(i == pageInfo.getPageNum()) {

                   builder.append("<a style='color:red;' 
href='"+path+"emps/"+i+"'>"+i+"</a>");

                   builder.append("&nbsp;&nbsp;");

              }else {

                   builder.append("<a 
href='"+path+"emps/"+i+"'>"+i+"</a>");

                   builder.append("&nbsp;&nbsp;");

              }

          }

          

          //拼接下一页

          if(pageInfo.isHasNextPage()) {

              builder.append("<a 
href='"+path+"emps/"+pageInfo.getNextPage()+"'>下一页</a>");

              builder.append("&nbsp;&nbsp;");

          }else {

              builder.append("下一页");

              builder.append("&nbsp;&nbsp;");

          }

          

          //拼接尾页

          builder.append("<a 
href='"+path+"emps/"+pageInfo.getPages()+"'>尾页</a>");

          builder.append("&nbsp;&nbsp;");

          

          return builder.toString();
     }
}

  1. 使用PageHelper提供的方法进行分页, 可以使用更强大的PageInfo封装返回结果
@RequestMapping(value = "/emps/{pageNum}", method = 
RequestMethod.GET)

     public String getAllEmp(Map<String, Object> 
map,@PathVariable("pageNum") Integer pageNum, 
HttpServletRequest request) {

          //使用分页插件

        //传入查询的页码,以及显示的条数

          PageHelper.startPage(pageNum, 2);

          List<Emp> empList = service.getAllEmp();

          //使用pageInfo包装查询后的结果,封装了详细的查询数据,其中参数5是页码导航连续显示的页数

          PageInfo<Emp> pageInfo = new 
PageInfo<>(empList,5);

          String page = PageUtil.getPageInfo(pageInfo, 
request);

          map.put("empList", empList);

          map.put("page", page);

          return "list";

     }


posted @ 2020-07-18 10:58  流沙uiui  阅读(262)  评论(0编辑  收藏  举报