mybatis当中如何自动生成Model和映射程序与配置文件和所需要的类

自动生成Model和映射程序与配置文件:【新手略过】
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203
利用mybatis编写的MyBatisGenerator,我们可以生成我们所需要的类和配置文件。













例 1.3

package com;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class MybatisMain {
    public static void mybatisCreate() {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        String projectBasePath = new File("").getAbsolutePath();
        System.out.println(projectBasePath);
        File fileConfigFile = new File(projectBasePath
                + "/src/generatorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        try {
            Configuration config = cp.parseConfiguration(fileConfigFile);
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            try {
                MyBatisGenerator myBatisGenerator = new MyBatisGenerator(
                        config, callback, warnings);
                try {
                    myBatisGenerator.generate(null);
                    System.out.println("after myBatisGenerator.generate(null); ");
                } catch (SQLException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } catch (InvalidConfigurationException e) {
                e.printStackTrace();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMLParserException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        mybatisCreate();
    }
}




generatorConfig.xml:


<?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="mysqlTables" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/test" userId="root"
            password="1234" />

        <!-- 指定生成的类型为java类型,避免数据库中number等类型字段 -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- 生成model模型,对应的包,存放位置可以指定具体的路径,如/ProjectName/src,也可以使用MAVEN来自动生成 -->
        <javaModelGenerator targetPackage="com"
            targetProject=".">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--对应的xml mapper文件 -->
        <sqlMapGenerator targetPackage="com.mapper"
            targetProject=".">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 对应的dao接口 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.mapper" targetProject=".">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <table schema="test" tableName="register"
            domainObjectName="Register" enableSelectByExample="false"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" />

    </context>
</generatorConfiguration>


下面是所生成的三个文件:为节省篇幅,我们只列出了部分程序:


package com;
import java.io.Serializable;
public class Register implements Serializable {
    private Integer id;
    private String name;
    private Integer age;
    private static final long serialVersionUID = 1L;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
之后的程序省略




package com.mapper;
import com.Register;
public interface RegisterMapper {
    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table register
     * @mbggenerated Thu Jul 13 18:09:47 CST 2017
     */
    int deleteByPrimaryKey(Integer id);
    int insert(Register record);
    int insertSelective(Register record);
    Register selectByPrimaryKey(Integer id);
    int updateByPrimaryKeySelective(Register record);
    int updateByPrimaryKey(Register record);
}




<?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="com.mapper.RegisterMapper">
  <resultMap id="BaseResultMap" type="com.Register">
    <id column="Id" jdbcType="INTEGER" property="id" />
    <result column="Name" jdbcType="VARCHAR" property="name" />
    <result column="Age" jdbcType="INTEGER" property="age" />
  </resultMap>
  <sql id="Base_Column_List">
    Id, Name, Age
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from register
    where Id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from register
    where Id = #{id,jdbcType=INTEGER}
  </delete>
之后的程序省略