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

1.Mybatis的逆向工程(generator)

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

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

1.1.引入mybatis-generator的jar包

1.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>

1.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);

    }
}

2.Mybatis的分页助手(pageHelper)

   加入相对应的jar包并解压 

2.1 在 MyBatis 配置 xml 中配置拦截器插件

<!-- 
    plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
    properties?, settings?, 
    typeAliases?, typeHandlers?, 
    objectFactory?,objectWrapperFactory?, 
    plugins?, 
    environments?, databaseIdProvider?, mappers?
-->
<plugins>
    <!-- com.github.pagehelper为PageHelper类所在包名 -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
        <property name="param1" value="value1"/>
    </plugin>
</plugins>

2.2  在逆向工程帮我们生成的接口中添加方法

public interface UsersMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(Users record);

    int insertSelective(Users record);

    Users selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Users record);

    int updateByPrimaryKey(Users record);
    
    public List<Users> selectByPageHelper();
}

2.3 映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhiyou100.mcl.dao.UsersMapper">
  <resultMap id="BaseResultMap" type="com.zhiyou100.mcl.bean.Users">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="NAME" jdbcType="VARCHAR" property="NAME" />
    <result column="sex" jdbcType="VARCHAR" property="sex" />
    <result column="age" jdbcType="INTEGER" property="age" />
  </resultMap>
  <sql id="Base_Column_List">
    id, NAME, sex, age
  </sql>
  <select id="selectByPageHelper" resultType="com.zhiyou100.mcl.bean.Users">
      select <include refid="Base_Column_List" /> from users 
  </select>
</mapper>

2.4 测试

@Test
    void testSelectByPageHelper() {
        int pageNum=2;
        int pageSize=3;
        PageHelper.startPage(pageNum, pageSize);
        List<Users> list = users.selectByPageHelper();
        System.out.println(list);
    }

 

posted @ 2019-08-30 22:30  猪头太笨  阅读(557)  评论(0编辑  收藏  举报