- 基于实体类为主导的代码生成器,可基于实体类快速的生成mybatis的相关配置。
如生成接口mapper,生成xml配置文件。
- 可通过实体类在生成自动建表,自动加索引,自动更新数据列。
- 可检测出数据库与实体类之间的差异并在日志中打印出对饮警告或者修复用的sql语句。
如 warn : 数据库中的列 [mysql_ame --> mysqlNname] 在实体类 Admin 不存在;
- 支持大部分JPA注解解析,可通过此代码生成器快速从hibernate转换到mybatis。
- 抽取查询对象,简化查询。QuerModel
- 自动驼峰装换
//JPA注解 需要解析的类必须加此注解
@Entity
//JPA注解 name表示数据表的名称 uniqueConstraints表示需要进行唯一约束的列,可自动追加到数据库
@Table(name = "admin",uniqueConstraints=@UniqueConstraint(columnNames={"userId,age"}))
public class Admin implements Serializable {
private static final long serialVersionUID = 1L;
//JPA 注解 此注解会自动同步数据为主键
@Id
private Long id;
private Long userId;
//唯一约束
@Column(unique=true)//
private String roleNames;
private int age;
//getter setter
}
//注意查询对象是由于Admin+QueryModel来进行命名
public class AdminQueryModel {
private static final long serialVersionUID = -8493398486786898485L;
//字段命名规则为需要进行的操作的属性名+操作符号 操作符会在代码解析的时候进行对应解析
//支持几乎大部分的操作符解析 集体解析请看src\main\java\com\code\core\parse\MybatisXmlParser.java
private String nicknameLK;
private String phoneEQ;
//getter setter
}
public static void main(String[] S) {
//全局配置对象
Config config = new Config();
//配置需要进行扫面的包
config.setEntityPackage("test.com.entity");
//配置需要扫描的queryModel包
config.setQueryModelPackage("test.com.model.query");
//配置mysql
config.setUrl("jdbc:mysql://192.168.1.66/test");
config.setUsername("root");
config.setPassword("123456");
//配置生成的xml的路径 默认生成在resource包下
onfig.setXMLPackage("test.com.mapping");
//配置生成mapper的包
config.setMapperPackage("test.com.mapper");
//配置log的存储路径 此log很重要。将会保存每次数据库和实体的检测结果以及修复的sql建议
Generater.run(config,new File("D:/LOGS3.LOG"));
}
public interface AdminMapper {
int insert(Admin admin);
int update(Admin admin);
int merge(@Param("admin") Admin admin, @Param("fields")String... fields);
int delete(Long id);
Admin findOne(Long id);
List<Admin> findAll(AdminQueryModel adminQueryModel);
long count(AdminQueryModel adminQueryModel);
}
public class AdminQueryModel {
private static final long serialVersionUID = 1L;
private String nicknameLK;
private String phoneEQ;
public String getNicknameLK() {
return nicknameLK;
}
public void setNicknameLK(String nicknameLK) {
this.nicknameLK = nicknameLK;
}
public String getPhoneEQ() {
return phoneEQ;
}
public void setPhoneEQ(String phoneEQ) {
this.phoneEQ = phoneEQ;
}
}
<?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="test.com.mapper.AdminMapper" >
<resultMap id="baseResultMap" type="test.com.entity.Admin">
<id column="id" property="id" />
<result column="user_id" property="userId" />
<result column="role_names" property="roleNames" />
<result column="age" property="age" />
</resultMap>
<insert id="insert" parameterType="test.com.entity.Admin" useGeneratedKeys="true" keyProperty="id">
insert into `admin` (user_id,role_names,age)
values (#{userId},#{roleNames},#{age})
</insert>
<delete id="delete">
delete from `admin` where id = #{id}
</delete>
<update id="update" parameterType="test.com.entity.Admin">
update `admin`
<set>
user_id = #{userId},
role_names = #{roleNames},
age = #{age},
</set>
where id = #{id}
</update>
<update id="merge">
update `admin`
<set>
<foreach collection="fields" item="field">
<choose>
<when test="field == 'userId'">user_id = #{admin.userId},</when>
<when test="field == 'roleNames'">role_names = #{admin.roleNames},</when>
<when test="field == 'age'">age = #{admin.age},</when>
</choose>
</foreach>
</set>
where id = #{admin.id}
</update>
<select id="findOne" resultMap="baseResultMap" parameterType="long">
select
id,user_id,role_names,age
from `admin`
where id = #{id}
</select>
<select id="findAll" resultMap="baseResultMap" parameterType="test.com.model.query.AdminQueryModel">
select
id,user_id,role_names,age
from `admin`
<where>
<if test="nicknameLK != null">
<bind name="nicknameLK" value="'%' + nicknameLK + '%'"/> and nickname like #{nicknameLK}
</if>
<if test="phoneEQ != null">
and phone = #{phoneEQ}
</if>
</where>
<choose>
<when test="orderByAndDirection != null">
order by ${orderByAndDirection}
</when>
<otherwise>
order by id desc
</otherwise>
</choose>
<if test="offset != null">
limit #{offset}, #{pageSize}
</if>
</select>
<select id="count" resultType="_long" parameterType="test.com.model.query.AdminQueryModel">
select count(*) from `admin`
<where>
<if test="nicknameLK != null">
<bind name="nicknameLK" value="'%' + nicknameLK + '%'"/> and nickname like #{nicknameLK}
</if>
<if test="phoneEQ != null">
and phone = #{phoneEQ}
</if>
</where>
</select>
</mapper>