关闭

MBG(MyBatis Generator):MyBatis自动代码生成;

标签: mybatisgenerator
137人阅读 评论(0) 收藏 举报
分类:

MyBatis Generator
part_1:generator-config.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">

        <!-- 不要生成注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql:///world" userId="root" password="3713">
        </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <javaModelGenerator targetPackage="cn.mike.world.pojo"
            targetProject=".\src">
            <property name="enableSubPackages" value="false" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="cn.mike.world.mapper"
            targetProject=".\src">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER"
            targetPackage="cn.mike.world.mapper" targetProject=".\src">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!-- mysql自带的world数据库下的city,country,和countrylanguage表 -->
        <table tableName="city"></table>
        <table tableName="country"></table>
        <table tableName="countrylanguage"></table>

    </context>
</generatorConfiguration>

part_2:运行下面的代码

package cn.mike.run;

import java.io.File;
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.internal.DefaultShellCallback;

/**
 * @author Administrator
 * @usage 功能:根据数据库自动生成代码
 */
public class Generator {

    public static void main(String[] args) throws Exception {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("src/generator-config.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);

    }

}

part_3:db.properties

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/world
db.username=root
db.password=3713

part_4:mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <properties resource="db.properties"></properties>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${db.driver}" />
                <property name="url" value="${db.url}" />
                <property name="username" value="${db.username}" />
                <property name="password" value="${db.password}" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <package name="cn.mike.world.mapper" />
    </mappers>
</configuration>

part_5:QualityTest(测试生成的代码是否可用)

package cn.mike.run;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import cn.mike.world.mapper.CityMapper;
import cn.mike.world.pojo.City;

public class QualityTest {

    private SqlSessionFactory sqlSessFac;

    @Before
    public void setup() {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.sqlSessFac = new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public void performanceTest() {
        SqlSession sqlSess = this.sqlSessFac.openSession();
        CityMapper cityMpr = sqlSess.getMapper(CityMapper.class);
        City city_1 = cityMpr.selectByPrimaryKey(1);
        System.out.println(city_1.toString());
    }
}

源码参考:点击下载源码

NOTE:
需要导入的三个jar包:
1.mybatis-generator-core-1.3.2.jar;
2.mybatis-3.2.3.jar;
3.mysql-connector-java-3.1.14-bin.jar(数据库驱动包,视情况而定);

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:72027次
    • 积分:1943
    • 等级:
    • 排名:千里之外
    • 原创:129篇
    • 转载:4篇
    • 译文:1篇
    • 评论:1条