Code Monkey home page Code Monkey logo

mybatis-plugin's Introduction

Mybatis Generator Plugin

中文文档:https://www.lijun.info/tqlab-mybatis-plugin

1. Maven repository

<dependency>
    <groupId>com.tqlab.plugin</groupId>
    <artifactId>tqlab-mybatis-plugin</artifactId>
    <version>1.0.9</version>
</dependency>

2. Plugin Configuration Sample

Single database:

	<build>
		<plugins>
			<plugin>
				<groupId>com.tqlab.plugin</groupId>
				<artifactId>tqlab-mybatis-plugin</artifactId>
				<version>1.0.9</version>
				<executions>
					<execution>
						<id>Generate MyBatis Artifacts</id>
						<phase>deploy</phase>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<outputDirectory>${project.basedir}</outputDirectory>
					<!-- db config -->
					<jdbcURL>jdbc:mysql://localhost/testdb?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull</jdbcURL>
					<jdbcUserId>user</jdbcUserId>
					<jdbcPassword>password</jdbcPassword>
					<database>testdb</database>
					<dbName>mysql</dbName>
					<!-- db config end -->
					<!-- <sqlScript>${project.basedir}/src/main/resources/mysql.sql</sqlScript> -->
					<packages>info.lijun.dal</packages>
					<sqlTemplatePath>${project.basedir}/src/main/resources/sqltemplate/</sqlTemplatePath>
					<overwrite>true</overwrite>
					<useCache>false</useCache>
					<generateJdbcConfig>false</generateJdbcConfig>
					<generateSpringConfig>true</generateSpringConfig>
				</configuration>
			</plugin>
		</plugins>
	</build>

Multi-database:

	<build>
		<plugins>
			<plugin>
				<groupId>com.tqlab.plugin</groupId>
				<artifactId>tqlab-mybatis-plugin</artifactId>
				<version>1.0.9</version>
				<executions>
					<execution>
						<id>Generate MyBatis Artifacts</id>
						<phase>deploy</phase>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<outputDirectory>${project.basedir}</outputDirectory>
					
					<databaseConfig>
						<config>
							<!-- db config -->
							<jdbcURL>jdbc:mysql://localhost/testdb1?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull</jdbcURL>
							<jdbcUserId>user</jdbcUserId>
							<jdbcPassword>password</jdbcPassword>
							<database>testdb1</database>
							<dbName>mysql</dbName>
							<!-- db config end -->
							<!-- <sqlScript>${project.basedir}/src/main/resources/mysql.sql</sqlScript> -->
							<packages>info.lijun.dal</packages>
							<sqlTemplatePath>${project.basedir}/src/main/resources/sqltemplate/</sqlTemplatePath>
							<generateJdbcConfig>false</generateJdbcConfig>
							<generateSpringConfig>true</generateSpringConfig>
							<overwrite>true</overwrite>
						</config>
						<config>
							<!-- db config -->
							<jdbcURL>jdbc:mysql://localhost/testdb2?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull</jdbcURL>
							<jdbcUserId>user</jdbcUserId>
							<jdbcPassword>password</jdbcPassword>
							<database>testdb2</database>
							<dbName>mysql</dbName>
							<!-- db config end -->
							<!-- <sqlScript>${project.basedir}/src/main/resources/mysql.sql</sqlScript> -->
							<packages>info.lijun.dal</packages>
							<sqlTemplatePath>${project.basedir}/src/main/resources/sqltemplate/</sqlTemplatePath>
							<generateJdbcConfig>false</generateJdbcConfig>
							<generateSpringConfig>true</generateSpringConfig>
							<useCache>false</useCache>
						</config>
					</databaseConfig>
					
					<overwrite>true</overwrite>
				</configuration>
			</plugin>
		</plugins>
	</build>
Attribute Description Default value Required
outputDirectory Oupput directory ${project.build.directory}/generated-sources/mybatis-generator
sqlScript Location of a SQL script file to run before generating code. false
jdbcURL Database url true
jdbcUserId Database user false
jdbcPassword Database password false
tableNames Comma delimited list of table names to generate all tables of current database false
tablePrefix For example, table name: wp_xxxx, the word 'wp' is the table prefix false
doSuffix For example, DO, the object name is xxxDO false
doRootClass Data Object super class false
database Database true
dbName Database name, mysql, hsqldb etc. true
packages Java package name, info.lijun.dal. true
overwrite Overwrite the exist code, config file or not. false false
sqlTemplatePath SqlMapper template path true
useCache Use cache or not. false false
providerEnable Generate default SQL Provider or not. true false
selectKeyEnable Generate @SelectKey or not. true false
generateSpringConfig Generate spring xml config file or not. false false
generateOsgiConfig Generate spring osgi xml config file or not. false false
properties extra config false

3. Sql Template File Sample


<?xml version="1.0" encoding="UTF-8"?>
<table xmlns="http://schema.tqlab.com/mybatis" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://schema.tqlab.com/mybatis http://schema.tqlab.com/mybatis/tqlab-mybatis-plugin.xsd"
	 name="star">

	<operation id="deleteById">
		<sql>
			<![CDATA[
			delete from star where id=#{id,jdbcType=INTEGER};
			]]>
		</sql>
	</operation>

	<operation id="count" resultType="java.lang.Integer">
		<sql>
			<![CDATA[
			select count(*) from star;
			]]>
		</sql>
	</operation>

	<operation id="sum" resultType="java.lang.Integer">
		<sql>
			<![CDATA[
			select sum(id) from star;
			]]>
		</sql>
	</operation>

	<operation id="selectAll" many="true">
		<sql>
			<![CDATA[
			select * from star;
			]]>
		</sql>
	</operation>

	<operation id="selectById" many="false">
		<sql>
			<![CDATA[
			select * from star where id=#{id,jdbcType=INTEGER};
			]]>
		</sql>
	</operation>

	<operation id="selectWithPagination">
		<comment>
			demo
		</comment>
		<sql>
			<![CDATA[
			select limit #{start,jdbcType=INTEGER} #{size,jdbcType=INTEGER} * from star;
			]]>
		</sql>
	</operation>

	<operation id="selectComplex1" many="true">
		<result objectName="StarMovies">
			<property cloumn="id" javaProperty="id" javaType="java.lang.Integer" />
			<property cloumn="firstname" javaProperty="firstname"
				javaType="java.lang.String" />
			<property cloumn="lastname" javaProperty="lastname"
				javaType="java.lang.String" />
			<property cloumn="movieid" javaProperty="movieid" javaType="java.lang.Integer" />
			<property cloumn="title" javaProperty="title" javaType="java.lang.String" />
		</result>
		<sql>
			<![CDATA[
			select a.*, b.* from star a, movies b where a.id = b.starid
			]]>
		</sql>
	</operation>

	<operation id="selectComplex2" many="true">
		<result objectName="StarMovies2">
			<property cloumn="star_id" javaProperty="id" javaType="java.lang.Integer" />
			<property cloumn="name" javaProperty="firstname" javaType="java.lang.String" />
			<property cloumn="lastname" javaProperty="lastname"
				javaType="java.lang.String" />
			<property cloumn="movieid" javaProperty="movieid" javaType="java.lang.Integer" />
			<property cloumn="title" javaProperty="title" javaType="java.lang.String" />
		</result>
		<sql>
			<![CDATA[
			select a.id as star_id, a.firstname as name, a.lastname, 
			b.movieid, b.title from star a, movies b 
			where a.id = b.starid
			]]>
		</sql>
	</operation>
</table>

More sample: https://github.com/tqlab/mybatis-plugin/blob/master/demo/src/main/resources/sqltemplate/hsqldb/table.star.xml

4. Change log

v 1.0.2

  1. bugfix hsqldb sql
  2. Add mysql support
  3. Add extra config (tableNames)

v 1.0.3

  1. modify Java Code generate commment
  2. config commentGenerator property suppressDate false
  3. change @paramter expression to maven annotation

v1.0.4

  1. bugfix 1.0.3. The v1.0.3 has a serious bug when release to maven centeral.

v1.0.5

  1. Add mybatis sql template xml Scheam support.
  2. Add log4j support when generate Java Mapper.
  3. Add tableNames validate.
  4. Add jsqlpaser for SQL validation.
  5. Add tablePrefix config support.
  6. Add auto delete ...dao /...dataobject package files when set overwrite is ture.
  7. Delete unused code.

v1.0.6

  1. bugfix mysql table generate code error when name is uppercase.
  2. bugfix table generate code error when the column's type is BLOB,BINARY or VARBINARY.
  3. Add dynamic script for annotation supported.
  4. Add parameter of Object type supported.

v1.0.7

  1. update jsqlparser version to 0.8.9
  2. Add mybatis Options annotation supported.
  3. Add alias name config supported.
  4. Add specific driver supported.

v1.0.8

  1. update jsqlparser version to 1.2
  2. Add @SelectKey supported

v1.0.9

  1. Add Provider annotation config (providerEnable default true).
  2. bugfix generatedKey
  3. update mybatis to 3.5.0
  4. update jsqlparser version to 1.4

v1.0.10

  1. Add connectionFactory plugin supported.
  2. Add selectKeyEnable config supported.

mybatis-plugin's People

Contributors

tqlab avatar yedg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.