前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >原 JAVA懒开发:mybatis-gen

原 JAVA懒开发:mybatis-gen

作者头像
kinbug [进阶者]
发布2018-06-13 15:09:42
4840
发布2018-06-13 15:09:42
举报
文章被收录于专栏:IT进修之路IT进修之路

java懒开发:mybatis-generator根据数据库中表的设计生成对应的实体类,xml Mapper文件,接口以及帮助类。

以spring-tool-suite-3.9.2.RELEASE为例:

安装

打开Help →Eclipse Marketplace 

引入包

<dependency>
	<groupId>org.mybatis.generator</groupId>
	<artifactId>mybatis-generator-core</artifactId>
	<version>1.3.6</version>
</dependency>

创建mybatis-generator配置文件

修改mybatis-generator配置文件

<?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>
  <context id="prod">
        <!-- RowBounds pagination -->
        <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />
        <plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin" />
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
        <commentGenerator>
            <property name="suppressDate" value="true" />
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!-- jdbc连接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://192.168.1.206/testOne" userId="root"
            password="1234" />
            
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		
		<!-- targetProject:生成PO类的位置 -->
        <javaModelGenerator targetPackage="com.lazy.develop.generator.entity" targetProject="lazyDevelop">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
			<!-- 从数据库返回的值被清理前后的空格 -->
			<property name="trimStrings" value="true" />
			<property name="comments" value="true"/>
        </javaModelGenerator>

        <!-- targetProject:mapper映射文件生成的位置 -->
		<sqlMapGenerator targetPackage="com.lazy.develop.generator.mapper"  targetProject="lazyDevelop">
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>
		
		<!-- targetPackage:mapper接口生成的位置 -->
		<javaClientGenerator type="XMLMAPPER" targetPackage="com.lazy.develop.generator.dao"  targetProject="lazyDevelop">
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>
		
		<!-- 指定数据库表 -->
		<table schema="" tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" 
		  enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
			<generatedKey column="id" sqlStatement="MySql" identity="true"/>
		</table>
    </context>
</generatorConfiguration>

项目搭建就不在写了哈,融入自己的框架就Ok了

运行

        如果你编辑器有插件,直接mybatis-generator配置文件右键运行就好了,如果木有运行下面main方法:

package com.mybatis.test;

import org.mybatis.generator.api.ShellRunner;

public class App {

    public static void main(String[] args) {
        args = new String[] { "-configfile", "src\\main\\resources\\generatorConfig.xml.xml", "-overwrite" };
        ShellRunner.main(args);
    }

}

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 以spring-tool-suite-3.9.2.RELEASE为例:
    • 安装
      • 引入包
        • 创建mybatis-generator配置文件
          • 运行
          相关产品与服务
          数据库
          云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档