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

1.Mybatis的逆向工程(generator)html

由表幫咱們來生成到,bean,xml映射文件java

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

1.1.引入mybatis-generator的jar包git

1.2.建立generator的配置文件github

<?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.運行generatorsql

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包並解壓 mybatis

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  在逆向工程幫咱們生成的接口中添加方法app

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 映射文件dom

<?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); }
相關文章
相關標籤/搜索