欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis****

程序员文章站 2022-04-17 17:33:29
...


Maven方法

1.加入相应的pom的jar包

  1. <dependency>
  2. <groupId>org.mybatis.generator</groupId>
  3. <artifactId>mybatis-generator-core</artifactId>
  4. </dependency>


2.idea****插件

  1. <build>
  2. <finalName>noodle-mybatis</finalName>
  3. <plugins>
  4. <plugin>
  5. <groupId>org.mybatis.generator</groupId>
  6. <artifactId>mybatis-generator-maven-plugin</artifactId>
  7. <version>1.3.2</version>
  8. <configuration>
  9. <configurationFile>src/main/resources/mybatis/mybatis-generator.xml</configurationFile>
  10. <verbose>true</verbose>
  11. <overwrite>true</overwrite>
  12. </configuration>
  13. </plugin>
  14. </plugins>
  15. </build>

3.属性文件

  1. jdbc.driverClass=com.mysql.jdbc.Driver
  2. jdbc.connectionURL=jdbc:mysql://127.0.0.1:3306/websocket?useUnicode=true&characterEncoding=utf8
  3. jdbc.userId=root
  4. jdbc.password=welcome
  5. jdbc.driverLocation=E:\\coding\\flxlib\\mysql-connector-java-5.1.35.jar

4.配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <generatorConfiguration>
  6. <!--导入属性配置-->
  7. <properties resource="mybatis/generator.properties"/>
  8. <!--数据库驱动,最好不要有中文字符,不然会找不到-->
  9. <classPathEntry location="${jdbc.driverLocation}"/>
  10. <context id="testTables" targetRuntime="MyBatis3">
  11. <commentGenerator>
  12. <!-- 是否去除自动生成的注释 true:是 : false:否-->
  13. <property name="suppressDate" value="true"/>
  14. <property name="suppressAllComments" value="true"/>
  15. </commentGenerator>
  16. <!--数据库链接地址账号密码-->
  17. <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
  18. <jdbcConnection driverClass="${jdbc.driverClass}"
  19. connectionURL="${jdbc.connectionURL}"
  20. userId="${jdbc.userId}"
  21. password="${jdbc.password}">
  22. </jdbcConnection>
  23. <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
  24. NUMERIC 类型解析为java.math.BigDecimal -->
  25. <javaTypeResolver>
  26. <property name="forceBigDecimals" value="false"/>
  27. </javaTypeResolver>
  28. <!-- targetProject:生成PO类的位置 -->
  29. <javaModelGenerator targetPackage="com.flx.noodle.mybatis.entity" targetProject="src/main/java">
  30. <!-- enableSubPackages:是否让schema作为包的后缀 -->
  31. <property name="enableSubPackages" value="false"/>
  32. <!-- 从数据库返回的值被清理前后的空格 -->
  33. <property name="trimStrings" value="true"/>
  34. </javaModelGenerator>
  35. <!-- targetProject:mapper映射文件生成的位置 -->
  36. <sqlMapGenerator targetPackage="com.flx.noodle.mybatis.entity" targetProject="src/main/java">
  37. <property name="enableSubPackages" value="false"/>
  38. </sqlMapGenerator>
  39. <!-- targetPackage:mapper接口生成的位置 -->
  40. <javaClientGenerator type="XMLMAPPER" targetPackage="com.flx.noodle.mybatis.dao" targetProject="src/main/java">
  41. <property name="enableSubPackages" value="true"/>
  42. </javaClientGenerator>
  43. <!--生成对应表及类名,需要记住的一点是****无法生成关联关系,只能生成单表操作-->
  44. <table tableName="ws_user" domainObjectName="WsUser"/>
  45. <table tableName="ws_room" domainObjectName="WsRoom"/>
  46. </context>
  47. </generatorConfiguration>


5.生成主文件

  1. package com.flx.noodle.mybatis.generator;
  2. import org.mybatis.generator.api.MyBatisGenerator;
  3. import org.mybatis.generator.config.Configuration;
  4. import org.mybatis.generator.config.xml.ConfigurationParser;
  5. import org.mybatis.generator.internal.DefaultShellCallback;
  6. import java.io.File;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. /**
  10. * Created by fenglixiong on 2017/10/22.
  11. */
  12. public class GeneratorMain {
  13. public static void main(String[] args) {
  14. try {
  15. generator();
  16. System.out.println("successful!!!");
  17. } catch (Exception e) {
  18. // TODO Auto-generated catch block
  19. e.printStackTrace();
  20. }
  21. }
  22. public static void generator() throws Exception{
  23. List<String> warnings = new ArrayList<String>();
  24. boolean overwrite = true;
  25. //项目根路径不要有中文,我的有中文,所以使用绝对路径
  26. File configFile = new File("E:\\coding\\BeefNoodle\\noodle-mybatis\\src\\main\\resources\\mybatis\\mybatis-generator.xml");
  27. ConfigurationParser cp = new ConfigurationParser(warnings);
  28. Configuration config = cp.parseConfiguration(configFile);
  29. DefaultShellCallback callback = new DefaultShellCallback(overwrite);
  30. MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
  31. myBatisGenerator.generate(null);
  32. }
  33. }

6.tomcat配置

mybatis****

java工程

github地址

https://github.com/zhangwei2github/MybatisGenerator.git



相关标签: mybatis 工具包