1,引入插件
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/mybatis-generator/config.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
2,config.xml
<?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="mybatis-generator/config.properties"/>
<!--classPathEntry:数据库的JDBC驱动,换成你自己的驱动位置 可选 -->
<!-- 一个数据库一个context -->
<!--defaultModelType="flat" 大数据字段,不分表 -->
<context id="MysqlTables" targetRuntime="MyBatis3" defaultModelType="flat">
<property name="autoDelimitKeywords" value="true"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<property name="javaFileEncoding" value="utf-8"/>
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<!-- 注释 -->
<commentGenerator>
<property name="suppressAllComments" value="false"/><!-- 是否取消注释 -->
<property name="suppressDate" value="true"/> <!-- 是否生成注释代时间戳-->
</commentGenerator>
<!-- jdbc连接 -->
<jdbcConnection driverClass="${jdbc_driver}" connectionURL="${jdbc_url}" userId="${jdbc_user}"
password="${jdbc_password}"/>
<!-- 类型转换 -->
<javaTypeResolver>
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成实体类地址 -->
<javaModelGenerator targetPackage="com.alibaba.aliyun.dao.model" targetProject="${project}">
<property name="rootClass" value="com.alibaba.aliyun.model.BaseModel"/>
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成mapxml文件 -->
<sqlMapGenerator targetPackage="com.alibaba.aliyun.dao" targetProject="${resources}">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<table tableName="account" enableCountByExample="true" enableUpdateByExample="true"
enableDeleteByExample="true"
enableSelectByExample="true" selectByExampleQueryId="true">
<property name="useActualColumnNames" value="false"/>
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
</context>
</generatorConfiguration>
config.properties
project=src/main/java
resources=src/main/resources
jdbc_driver=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://anno.coronadb.alibaba.net:3306/BSIC_APP
jdbc_user=BSIC_APP
jdbc_password=135246
generator 探寻
所有的方法:
int countByExample(DOExample example);
int deleteByExample(DOExample example);
1.
updateByPrimaryKeySelective会对字段进行判断再更新(如果为Null就忽略更新),如果你只想更新某一字段,可以用这个方法。
updateByPrimaryKey对你注入的字段全部更新
原理:加selective 表示对更新的model进行为空的判断
<update id="updateByPrimaryKeySelective" parameterType="com.alibaba.aliyun.dao.model.Account">
update account
<set>
<if test="gmtCreate != null">
gmt_create = #{gmtCreate,jdbcType=TIMESTAMP},
</if>
<if test="gmtModified != null">
gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},
</if>
<if test="name != null">
`name` = #{name,jdbcType=VARCHAR},
</if>
<if test="role != null">
`role` = #{role,jdbcType=VARCHAR},
</if>
<if test="status != null">
`status` = #{status,jdbcType=VARCHAR},
</if>
<if test="isBuc != null">
is_buc = #{isBuc,jdbcType=VARCHAR},
</if>
<if test="channel != null">
channel = #{channel,jdbcType=BIGINT},
</if>
<if test="creator != null">
creator = #{creator,jdbcType=VARCHAR},
</if>
<if test="modifier != null">
modifier = #{modifier,jdbcType=VARCHAR},
</if>
<if test="properties != null">
properties = #{properties,jdbcType=LONGVARCHAR},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
2
updateByPrimaryKeyWithBLOBs
updateByExampleWithBLOBs
selectByExampleWithBLOBs
加BLOBs表示大文本字段
<resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.alibaba.aliyun.dao.model.Account">
<result column="properties" jdbcType="LONGVARCHAR" property="properties" />
</resultMap>
3,
<trim prefix="(" prefixOverrides="and" suffix=")">
</trim>
prefix:表示在trim标签内sql语句加上前缀xxx
suffix:表示在trim标签内sql语句加上后缀xxx
suffixOverrides:表示去除最后一个后缀xxx
prefixOverrides:去掉后缀
4.Example里面的or查询
ViewPsmsgconsultExample example=new ViewPsmsgconsultExample();
ViewPsmsgconsultExample.Criteria criteria=example.createCriteria();
criteria.andToidEqualTo(mctid);
criteria.andStatusEqualTo("0");
ViewPsmsgconsultExample.Criteria criteria2=example.createCriteria();
criteria2.andToidEqualTo(mctid);
criteria2.andLaststatusEqualTo("0");
example.or(criteria2);
psmsgconsultDao.countByExample(example);
5,时间倒序
example.setOrderByClause(
gmt_modified desc
);6,Like 要加%%