SSM学习15:MyBatis****,分页插件
程序员文章站
2022-07-13 09:59:15
...
****
1.下载
https://github.com/mybatis/generator/releases
2.把相关jar导入到工程当中
mybatis-generator-core-1.3.7.jar
3.创建generatorConfig.xml 设置targetRuntime为MyBatisSimple则为简单的增删改查
<?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:设置自动生成的版本
MyBatis3:
MyBatis3Simple:简单增删改查
-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--
不要生成日期和备注
-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8"
userId="root"
password="1234">
</jdbcConnection>
<!--
配置domain生成策略
targetProject:把自动生成的domian放在哪个工程里面
targetPackage:哪个包下
-->
<javaModelGenerator targetPackage="com.itlike.domain" targetProject=".\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--
配置mapper的生成策略
targetPackage:把自动生成的mapper放在哪个工程里面
targetProject:哪个包下
-->
<sqlMapGenerator targetPackage="com.itlike.mapper" targetProject=".\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--
mapper接口生成策略
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.itlike.mapper" targetProject=".\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="customer" domainObjectName="Customer" ></table>
<table tableName="teacher" domainObjectName="Teacher" ></table>
<table tableName="student" domainObjectName="Student" ></table>
</context>
</generatorConfiguration>
4.编写生成代码
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("./src/generatorConfig.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);
5.MyBatis3版本的简单实用(一般开发还是用simple版本的)
前面的targetRuntime改为MyBatis3后应该如何使用呢
每个类都会生成多一个Example的,这是可以类似hibernate一样使用
分页插件
把从数据库查询出来的记录分页
1.下载分页插件
https://github.com/pagehelper/Mybatis-PageHelper/blob/master/README_zh.md
2.配置分页插件
sqlMappingConfig.xml
位于environments标签之前
3在查询之前设置分页
Page<Object> page = PageHelper.startPage(1, 5);
查询数据之后添加
PageInfo<Customer> pageInfo = new PageInfo<>(customers, 5);