本文概览:使用这个插件可以自动生成mapper.xml、mapper.java、mapperExample.java和Enty.java四个文件。
1 准备
1、创建数据库
1 |
create database statistics; |
2、数据表
1 2 3 4 5 6 7 8 9 10 11 12 |
CREATE TABLE `bank_info` ( `id` bigint(21) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键', `bank_code` int(10) NOT NULL COMMENT '银行编码', `name` varchar(50) NOT NULL COMMENT '银行名称', `easy_pay` tinyint(4) NOT NULL COMMENT '是否支持快捷支付。1-支持。0-不支持', `bank_mobile` tinyint(4) NOT NULL COMMENT '是否保持卡号和手机号一致。1-是。0-否', `net_bank` tinyint(4) NOT NULL COMMENT '是否支持网银支付。1-支持。0-不支持', `create_time` timestamp NOT NULL COMMENT '创建时间', `update_time` timestamp NOT NULL COMMENT '修改时间', PRIMARY KEY (`id`), UNIQUE KEY `unique_bank_code` (`bank_code`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=gbk COMMENT='银行信息'; |
2 配置
这是一个工程目录,首先需要在pom.xml中配置插件,并且需要有generatorConfig.xml、mysql-conector-java-5.1.26.jar和config.properties三个文件。
1、pom.xml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<build> <finalName>mybatis_generator</finalName> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>mybatis-generator/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build> |
2、generatorConfig.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?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> <!-- 配置文件 --> <properties resource="config.properties"/> <!-- 数据库驱动包 --> <classPathEntry location="mybatis-generator/mysql-connector-java-5.1.26.jar" /> <context id="engine" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="false" /> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 数据库连接 --> <jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.user}" password="${jdbc.password}"> </jdbcConnection> <!-- 生成entity --> <javaModelGenerator targetPackage="${model.package}" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成mapper.xml --> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成dao接口 --> <javaClientGenerator targetPackage="${dao.package}" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!--对应的数据表名--> <table tableName="bank_info" domainObjectName="BankInfo" > <columnRenamingRule searchString="^D_" replaceString=""/> </table> </context> </generatorConfiguration> |
1 2 3 4 5 6 |
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/statistics?useUnicode=true&characterEncoding=gbk jdbc.user=root jdbc.password=123 model.package=output.entity dao.package=output.mapper |
3 执行
右键执行“Run Maven Build”,如下图
执行结果会生成如下四个文件
4 应用
4.1 使用example查询
代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@Service public class QueryService { @Autowired private BankInfoMapper bankInfoMapper; public void testQuery(){ BankInfoExample example = new BankInfoExample(); BankInfoExample.Criteria criteria = example.createCriteria(); criteria.andBankCodeEqualTo(1003); List<BankInfo> bankInfoList = bankInfoMapper.selectByExample(example); } } |
4.2 分页查询
1、在BankInfoMapper.java接口中新增
1 |
List<BankInfo> selectByExampleWithPaging(BankInfoExample example); |
2、在BankInfoMapper.xml文件新增selectByExampleWithPaging代码
拷贝selectByExample代码,在末尾添加“limit ${startIndex},${pageSzie}”,如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<select id="selectByExampleWithPaging" parameterType="output.entity.BankInfoExample" resultMap="BaseResultMap"> select <if test="distinct"> distinct </if> <include refid="Base_Column_List" /> from bank_info <if test="_parameter != null"> <include refid="Example_Where_Clause" /> </if> <if test="orderByClause != null"> order by ${orderByClause} </if> limit ${startIndex},${pageSize} </select> |
3、修改BankInfoExample
新增startIndex和pageSize两个成员,并包括对应getter和setter方法。如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// 起始 protected int startIndex; // 每页大小 protected int pageSize; public void setStartIndex(int startIndex) { this.startIndex = startIndex; } public int getStartIndex() { return startIndex; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getPageSize() { return pageSize; } |
4、同时使用排序和分页进行查询
如按照id进行排序,并分页进行查询的一个例子如下
1 2 3 4 5 6 7 8 9 10 |
public void testPageAndOrder(){ BankInfoExample example = new BankInfoExample(); example.setPageSize(10); example.setStartIndex(0); example.setOrderByClause("id desc"); BankInfoExample.Criteria criteria = example.createCriteria(); criteria.andCreateTimeLessThan(new Date()); List<BankInfo> bankInfoList = bankInfoMapper.selectByExample(example); } |
(全文完)