Java 8 development Mybatis annotated code generation tools

MybatisAnnotationTools

MybatisAnnotationTools is based on a Java8 developed can be used to generate automated annotation tool MyBatis class and supports data source configuration, class path, table name to the prefix, suffix before the specified class name and other functions. Java also supports several new features 8 and Mybatis 3.5+, such as class time LocalDateTime / LocalDate, the interface method returns Optional Wait.

This tool generates code is Mybatis interface method annotation-based, it will not generate the XML configuration file.

Source Address: Github address
code is simple, a total of more than 700 lines, friends who are interested can download to see the exchange of learning from each other.

Features:

1. PO and DAO automatically generates Java classes, DAO support paging query, the query based on the id, a single insert, bulk insert, update, delete a single batch delete.
Java file

UserDao.java follows (the style may be changed by modifying the template class, the template will be mentioned later):

@Mapper
public interface UserDao extends BaseDao<UserDao> {
    /** 分页查询 */
    @Select("select * from t_user limit #{page.currentPage}, #{page.pageSize}")
    List<UserPO> listByPage(@Param("page") Page page);
    /** 根据id查询 */
    @Select("select * from t_user where id = #{id}")
    Optional<UserPO> getById(Serializable id);
    /** 单个插入 */
    @Insert("insert into t_user(id, name, gender, birthday, address, create_time, update_time)  values(#{id}, #{name}, #{gender}, #{birthday}, #{address}, #{createTime}, #{updateTime})")
    void save(UserPO po);
    /** 批量插入 */
    @Insert("<script>insert into t_user(id, name, gender, birthday, address, create_time, update_time) values "
        + "<foreach collection='list' index='index' item='n' separator=','> "
        + "(#{n.id}, #{n.name}, #{n.gender}, #{n.birthday}, #{n.address}, #{n.createTime}, #{n.updateTime})"
        + "</foreach></script>")
    void saveBatch(@Param("list") List<UserPO> list);
    /** 更新 */
    @Update("update t_user set id = #{id}, name = #{name}, gender = #{gender}, birthday = #{birthday}, address = #{address}, create_time = #{createTime}, update_time = #{updateTime} where id = #{id}")
    void update(UserPO po);
    /** 单个删除 */
    @Delete("delete from t_user where id = #{id}")
    void remove(Serializable id);
    /** 批量删除 */
    @Delete("<script>delete from t_user where id in "
        + "<foreach collection='ids' index='index' item='id' open='(' separator=',' close=')'>"
        + "#{id}"
        + "</foreach></script>")
    void removeByIds(@Param("ids") Set<Serializable> ids);
    /** 统计 */
    @Select("select count(*) from t_user")
    int count();
}

2. Configurableapplication.properties

# MySQL 连接配置
mysql.datasource.driver-class-name=com.mysql.jdbc.Driver
mysql.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false
mysql.datasource.username=root
mysql.datasource.password=
# 表前缀,生成类时会去掉这个前缀
mysql.datasource.table.prefix=t_
# 是否要生成 PO
java.model.enable=true
# PO 包路径
java.model.package=com.xxx.po
# PO 类文件生成路径,"/"结尾
java.model.src.folder=E:/CODE/github/po/
# PO 类文件前缀
java.model.prefix=
# PO 类文件后缀
java.model.suffix=PO
# 是否要生成 DAO
java.dao.enable=true
# DAO 包路径
java.dao.package=com.xxx.dao
# DAO 类文件生成路径,"/"结尾
java.dao.src.folder=E:/CODE/github/dao/
# DAO 类文件前缀
java.dao.prefix=
# DAO 类文件后缀
java.dao.suffix=Dao

how to use?

And download source package can skip directly download jar package

  • Source package:
  1. In pom.xmlthe next file directorymvn clean package -Dmaven.test.skip=true
  2. Resulting in targetextraction jar can then be executed directly directory:annotation-Tools-1.0-jar-with-dependencies.jar
  • Jar package execute:
    performing at the top jar package directory java -jar annotation-Tools-1.0-jar-with-dependencies.jarto execute the default configuration; can also application.propertiesbe taken out to modify, when the execution of the command profile path followed by the profile {文件路径}\application.propertiesto perform the location profile.

Source structure

  • Start main categories:Bootstrap.java
  • Profile: resources/application.propertiesdirectory
  • Template file location: resourcesdirectory, where can modify the generated code template

example

There table t_studentand t_user, with the default configuration will E:/CODE/githubgenerate daoand podirectories, which reads as follows:

├─dao
│      BaseDao.java
│      StudentDao.java
│      UserDao.java
│
└─po
        Page.java
        StudentPO.java
        UserPO.java

Guess you like

Origin www.cnblogs.com/bigshark/p/11267822.html
Recommended