mybatis generator****自动生成实体类,dao接口和对应的Mapper.xml文件

去mybatis generator官方文档找http://www.mybatis.org/generator/configreference/xmlconfig.html#

(1)先在pom.xml中添加依赖包

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

(2)创建mbg.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="DB2Tables" targetRuntime="MyBatis3">
    <!-- 自动生成的文件会有大量注释,所有取消注释的方法如下,生成没注释的文件 -->
    <commentGenerator>
  <property name="suppressAllComments" value="true" />
</commentGenerator>
  <!-- 配置数据库链接 -->
  
    <jdbcConnection
     driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/course_evaluation"
        userId="root"
        password="123456">
    </jdbcConnection>
<!-- 指定javabean实体类生成的位置 -->
    <javaModelGenerator targetPackage="com.ssm.bean" 
        targetProject=".\src\main\java">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
<!-- 指定sql映射文件生成的位置 -->
    <sqlMapGenerator targetPackage="mapper" 
         targetProject=".\src\main\resources">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
<!-- 指定dao接口(是一些增删改查方法头)生成的位置 ,mapper接口-->
    <javaClientGenerator type="XMLMAPPER" 
        targetPackage="com.ssm.dao"  
        targetProject=".\src\main\java">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
<!-- 指定每个表的生成策略,针对数据库中那些表,产生的实体类名为?-->
    <table tableName="admin" domainObjectName="Admin"></table>
 <table tableName="class" domainObjectName="Class"></table>
 <table tableName="class_course" domainObjectName="Class_course"></table>
 <table tableName="course" domainObjectName="Course"></table>
 <table tableName="evaluation" domainObjectName="Evaluation"></table>
 <table tableName="student" domainObjectName="Student"></table>
</context>
</generatorConfiguration>

(3)执行自动生成器

mybatis generator****自动生成实体类,dao接口和对应的Mapper.xml文件

package com.maven.crud.test;

import java.io.File;
import java.io.IOException;
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.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

public class MBGTest {

    public static void main(String[] args) throws Exception {
         List<String> warnings = new ArrayList<String>();
           boolean overwrite = true;
           File configFile = new File("mbg.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);
    }

}