Mybatis的逆向工程(generator)以及分页助手(pageHelper)

时间:2019-08-30
本文章向大家介绍Mybatis的逆向工程(generator)以及分页助手(pageHelper),主要包括Mybatis的逆向工程(generator)以及分页助手(pageHelper)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

由表帮我们来生成到,bean,xml映射文件

  http://www.mybatis.org/generator/index.html

1.引入mybatis-generator的jar包

2.创建generator的配置文件

<?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">
<!-- mysql驱动jar的所在的位置 -->
<generatorConfiguration>
  <classPathEntry location="D:\\jar\\mysql\\mysql-connector-java-5.1.47.jar" />
<!-- 数据源的信息 -->
  <context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 阻止生成文件时自动编写的备注 -->
  <commentGenerator>
  <property name="suppressAllComments " value="true" />
  </commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="root"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成的实体类所在的位置 --> <javaModelGenerator targetPackage="com.zhiyou100.mcl.bean" targetProject=".\src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成的映射文件所在的位置 --> <sqlMapGenerator targetPackage="com.zhiyou100.mcl.mapper" targetProject=".\src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生产的dao包所在的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.zhiyou100.mcl.dao" targetProject=".\src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 某张表与实体类的对象关系
   可复制生成多张表 schema:该表所在的数据库 tableName:表名 domainObjectName:实体类名
--> <table schema="mybatis" tableName="users" domainObjectName="Users" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> <property name="useActualColumnNames" value="true"/> <generatedKey column="ID" sqlStatement="DB2" identity="true" /> <columnOverride column="DATE_FIELD" property="startDate" /> <ignoreColumn column="FRED" /> <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> </table> </context> </generatorConfiguration>

3.运行generator

public class TestGenerator {

    public static void main(String[] args) throws Exception {
           List<String> warnings = new ArrayList<String>();
           boolean overwrite = true;
           File configFile = new File("generator.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);

    }
}

4.Mybatis的分页助手(pageHelper)

原文地址:https://www.cnblogs.com/mcl2238973568/p/11437435.html