前言:
Mybatis-Generator是一个使用java开发的开源代码库,使用它可以自动生成Mybatis框架所需要的Dao接口,pojo实体类和sqlMaaper.xml文件
开始:
1.下载Mybatis-Generator的相关jar包:https://github.com/mybatis/generator/releases
release里面有 jar包 和Eclipse插件,还有src源代码,我里我们选择红框中的zip包
下载后解压会在lib目录下有一个mybatis-generator-core-1.3.7.jar ,这个是主要的jar包
2.创建配置文件generatorConfig.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>
<!-- 数据库驱动-->
<classPathEntry location="mysql-connector-java-5.1.47.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://xxxxxxxxx/xxxx" userId="root" password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="cn.kevin.entity" targetProject="src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="cn.kevin.mapper" targetProject="src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.kevin.Dao" targetProject="src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="item" domainObjectName="Item" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
3.创建一个src文件夹
根据配置文件中的路径 targetProject="src",所以创建一个src文件夹,生成的代码会放到这里
4.拷贝一个数据库连接驱动到目录下
<classPathEntry location="mysql-connector-java-5.1.47.jar"/> 配置文件中也指定了连接mysql的驱动jar包,所以拷贝一个mysql-connector-java到本目录下,再根据实际的版本号,修改下配置文件
5.目录结构如下
6.打开cmd,执行命令
使用cms进入该目录下,执行
java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite
执行完成后,会提示Mybatis Generator Finished Successfully
可以在src下查看生成的dao接口文件、pojo实体类、和mapper.xml文件了
生成的dao接口文件 :
package cn.kevin.Dao;
import cn.kevin.entity.Item;
import cn.kevin.entity.ItemExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface ItemMapper {
List<Item> selectall();
long countByExample(ItemExample example);
int deleteByExample(ItemExample example);
int deleteByPrimaryKey(Integer id);
int insert(Item record);
int insertSelective(Item record);
List<Item> selectByExample(ItemExample example);
Item selectByPrimaryKey(Integer id);
int updateByExampleSelective(@Param("record") Item record, @Param("example") ItemExample example);
int updateByExample(@Param("record") Item record, @Param("example") ItemExample example);
int updateByPrimaryKeySelective(Item record);
int updateByPrimaryKey(Item record);
}
可以看到基本的增删查改都有了,里面还有一些Selective的方法。Selective意为选择的
意思是带Selective的方法在操作数据库时,若遇到null的字段,会自动过滤。而不带Selective的话,则会把每个字段都更新或插入,即使该字段的值为null
问题:
1.生成的mapper.xml中怎么只有两个insert方法
因为数据库表没有主键导致的,加上主键即可
2.怎么生成Example实体类
在 <table tableName="item" domainObjectName="Item" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
这里修改enableCountByExample、enableUpdateByExample等属性为 ture即可,默认为false,或者直接去掉,再生成即可
生成的Examply类的具体用法如下
如表名为User,那么根据UserName字段查询的代码为
UserExample example=new UserExample ();
Criteria criteria = example.createCriteria();
criteria.andUserNameEqualTo("zhangsan");
List<User> list = tbUserMapper.selectByExample(example);
3.The alias 'Criteria' is already mapped to the value '***Example$Criteria'.
这是因为使用了两次逆向工程,第一次生成表Item,然后第二次又新增了一张表Item2,然后把代码拷过去就报错了。
有4个地方要改Criteria,CreateCriteria,Criterion, GeneratedCriteria 改掉重名的即可