Mybatis自动生成代码

本文详细介绍了如何使用Mybatis Generator来自动生成Mybatis的相关代码,包括构建环境、XML文件编写以及通过命令行、Eclipse和Maven等方式进行代码生成,旨在提高开发效率。
摘要由CSDN通过智能技术生成

         林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka

       摘要:本文将简要介绍怎样利用Mybatis Generator自动生成Mybatis的相关代码,Mybatis Generator是一个非常好用的工具,使用它可以大大节省开发的时间,并减少代码的编写量。

本文工程免费下载

一、构建一个环境


1. 首先创建一个表:

CREATE TABLE
    t_user
    (
        USER_ID INT NOT NULL AUTO_INCREMENT,
        USER_NAME CHAR(30) NOT NULL,
        USER_PASSWORD CHAR(10) NOT NULL,
        USER_EMAIL CHAR(30) NOT NULL,
        PRIMARY KEY (USER_ID)
    )
    ENGINE=InnoDB DEFAULT CHARSET=utf8;

2. 在 Mybatis 主页 http://code.google.com/p/mybatis/ 上下载 Mybatis mybatis-generator-core 或者在这里下载:http://download.csdn.net/detail/evankaka/8926999

二、xml文件编写

1、新建一个工程。然后新建如下包,都是空的


2、然后新建generator.xmll文件

内容如下:

<?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:数据库的JDBC驱动的jar包地址 -->
	<classPathEntry location="D:\Java\Jar\mysql-connector-java-5.1.22\mysql-connector-java-5.1.22-bin.jar" />
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
		<!-- 抑制警告 -->
		<property name="suppressTypeWarnings" value="true" />
		<!-- 是否去除自动生成的注释 true:是 : false:否 -->
		<property name="suppressAllComments" value="false" />
		<!-- 是否生成注释代时间戳-->  
		<property name="suppressDate" value="true" /> 
		</commentGenerator>

		<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost/learning" userId="root"
			password="christmas258@">
		</jdbcConnection>
		<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 
			NUMERIC 类型解析为java.math.BigDecimal -->
	<!-- 	<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver> -->
        <!--生成Model类存放位置 -->
		<javaModelGenerator targetPackage="com.lin.domain"
			targetProject="D:\lunaJee-workspace\MyBatisLearningChapter7\src">
			 <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->  
			<property name="enableSubPackages" value="false" />
			 <!-- 是否针对string类型的字段在set的时候进行trim调用 -->  
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
        <!--生成映射文件存放位置 -->
		<sqlMapGenerator targetPackage="com.lin.mapper"
			targetProject="D:\lunaJee-workspace\MyBatisLearningChapter7\src">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
        <!--生成Dao类存放位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.lin.dao" targetProject="D:\lunaJee-workspace\MyBatisLearningChapter7\src">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		

       	<!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->  
        <table schema="general" tableName="T_USER" domainObjectName="User">
         <!--domain字段的命名规则,false:默认为驼峰命名 true:按数据库真实命名  -->
        <property name="useActualColumnNames" value="false"/>
        <!-- 忽略列,不生成bean 字段 -->  
        <!--     <ignoreColumn column="FRED" />   -->
        <!-- 指定列的java数据类型 -->  
        <!--     <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />   -->
        </table>
	</context>
</generatorConfiguration>

三、自动代码生成

自动代码生成有4种方法

1、直接cmd下命令行生成

命令如下:java -jar 电脑上mybatis-generator-core-1.3.0.jar的绝对路径  -configfile 电脑上generator.xml的绝对路径,这里的generator.xml不一定要放在工程的src文件中。

如我的这个项目就是:

运行的结果如下:

然后在eclipse中刷新一下:结果出来了


看看各个文件

(1)User.java

package com.lin.domain;

public class User {
    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_user.USER_ID
     *
     * @mbggenerated
     */
    private Integer userId;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_user.USER_NAME
     *
     * @mbggenerated
     */
    private String userName;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_user.USER_PASSWORD
     *
     * @mbggenerated
     */
    private String userPassword;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_user.USER_EMAIL
     *
     * @mbggenerated
     */
    private String userEmail;

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_user.USER_ID
     *
     * @return the value of t_user.USER_ID
     *
     * @mbggenerated
     */
    public Integer getUserId() {
        return userId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_user.USER_ID
     *
     * @param userId the value for t_user.USER_ID
     *
     * @mbggenerated
     */
    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_user.USER_NAME
     *
     * @return the value of t_user.USER_NAME
     *
     * @mbggenerated
     */
    public String getUserName() {
        return userName;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_user.USER_NAME
     *
     * @param userName the value for t_user.USER_NAME
     *
     * @mbggenerated
     */
    public void setUserName(String userName) {
        this.userName = userName == null ? null : userName.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_user.USER_PASSWORD
     *
     * @return the value of t_user.USER_PASSWORD
     *
     * @mbggenerated
     */
    public String getUserPassword() {
        return userPassword;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_user.USER_PASSWORD
     *
     * @param userPassword the value for t_user.USER_PASSWORD
     *
     * @mbggenerated
     */
    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword == null ? null : userPassword.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_user.USER_EMAIL
     *
     * @return the value of t_user.USER_EMAIL
     *
     * @mbggenerated
     */
    public String getUserEmail() {
        return userEmail;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_user.USER_EMAIL
     *
     * @param userEmail the value for t_user.USER
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值