教你一步步搭建ssm框架,第四步自动生成mybaits实体类,配置文件
原创 java_world 发表于:2018-05-10 14:18:50
  阅读 :103   收藏   编辑

上一步:教你一步步搭建ssm框架,第三步数据库事务验证及ssm常见事务不起作用排除

在第二步中,我们创建model,mapper,mapper.xml,单单使用手动去写会很繁琐,这里介绍mybatis

自动构建插件 “MyBatis Generator”

在线安装

Help -> Eclipse Marketplace

搜索 “MyBatis Generator”

安装完成后重启即可

使用

在我们的项目下新增generatorConfig.xml配置文件,位置如下,与pom.xml同级

参考内容如下,记得在D盘存放mysql-connector-java-5.1.44.jar驱动

<?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>
  <!-- jar包目录 -->
  <classPathEntry  location="D:/mysql-connector-java-5.1.44.jar" />  
  <context id="app">
      <commentGenerator>  
              <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
            <property name="suppressAllComments" value="true"/>  
    </commentGenerator>
      <!-- 数据库链接URL、用户名、密码 -->  
    <jdbcConnection connectionURL="jdbc:mysql://localhost:3306/app_test" driverClass="com.mysql.jdbc.Driver" userId="root" password="root" />

   <!-- 生成模型的包名和位置 --> 
    <javaModelGenerator targetPackage="com.faceghost.app.model" targetProject="app" >
         <!-- 是否在当前路径下新加一层schema,eg:false路径com.xx.model, true:com.xx.model.[schemaName] -->  
        <property name="enableSubPackages" value="false"/>  
        <!-- 是否针对string类型的字段在set的时候进行trim调用 -->  
        <property name="trimStrings" value="true"/>  
    </javaModelGenerator>

     <!-- 生成的映射文件报名和位置 --> 
    <sqlMapGenerator targetPackage="com.faceghost.app.dao.mapper"  targetProject="app"  >
         <!-- 是否在当前路径下新加一层schema,eg:false路径com.xx.model.mapper, true:com.xx.model.mapper.[schemaName] -->  
        <property name="enableSubPackages" value="false" />  
       </sqlMapGenerator>

     <!-- 生成DAO的包名和位置 --> 
    <javaClientGenerator targetPackage="com.faceghost.app.dao"  targetProject="app" type="XMLMAPPER" >
         <!-- 是否在当前路径下新加一层schema,eg:false路径com.xx.dao, true:com.xx.dao.[schemaName] -->  
         <property name="enableSubPackages" value="false" />  
    </javaClientGenerator>

      <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample   
                是否生成 example类   --> 
    <table schema="app_test" tableName="test" 
            domainObjectName="Test" enableCountByExample="false"  
            enableDeleteByExample="false" enableSelectByExample="false"  
            enableUpdateByExample="false" >
            <!-- 忽略列,不生成bean 字段   
            <ignoreColumn column="FRED" /> --> 
            <!-- 指定列的java数据类型  
            <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />   --> 
    </table>
  </context>
</generatorConfiguration>

选中generatorConfig.xml右键Run As -> Run MyBatis Generator

console打印如下:

MyBatis Generator Started...
  ....
  [2018-05-10 14:11:45.829]-[org.mybatis.generator.eclipse.ui.ant.logging.AntLogFactory]-[Worker-15]  - Logging initialized using 'org.mybatis.generator.eclipse.ui.ant.logging.slf4j.Slf4jLoggingLogFactory@7b26d53c' adapter.
  [2018-05-10 14:11:45.843]-[org.mybatis.generator.logging.LogFactory]-[Worker-15]  - Logging initialized using 'org.mybatis.generator.eclipse.ui.ant.logging.AntLogFactory@5ee19820' adapter.
  [2018-05-10 14:11:46.293]-[org.mybatis.generator.internal.db.DatabaseIntrospector]-[Worker-15]  - Retrieving column information for table "app_test.test"
  [2018-05-10 14:11:46.314]-[org.mybatis.generator.internal.db.DatabaseIntrospector]-[Worker-15]  - Found column "id", data type 4, in table "app_test..test"
  [2018-05-10 14:11:46.314]-[org.mybatis.generator.internal.db.DatabaseIntrospector]-[Worker-15]  - Found column "user_name", data type 12, in table "app_test..test"
  [2018-05-10 14:11:46.314]-[org.mybatis.generator.internal.db.DatabaseIntrospector]-[Worker-15]  - Found column "addr", data type 12, in table "app_test..test"
  [2018-05-10 14:11:46.314]-[org.mybatis.generator.internal.db.DatabaseIntrospector]-[Worker-15]  - Found column "sex", data type -6, in table "app_test..test"
  BUILD SUCCESSFUL
MyBatis Generator Finished

在项目的dao,model就自动生成了我们想要的文件

注意,该执行结果会在原文件进行追加,所有还是建议生成在其他地方,按需复制

targetProject设置为:app/src/test/java

参考: