好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

MyBatis Generator的简单使用方法示例

添加配置文件

在项目resource目录下创建mybatis-generator文件夹

在文件夹下创建generatorConfig.xml,配置需要生成代码的数据表

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

<? 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 >

  < properties resource = "mybatis-generator/generator.properties" />

  <!-- 连接数据库jar包的路径-->

  <!--<classPathEntry location="d:/java/JavaTools/mysql-connector-java-5.1.48/mysql-connector-java-5.1.48-bin.jar"/>-->

  < context id = "DB2Tables" targetRuntime = "MyBatis3" >

   < commentGenerator >

    < property name = "suppressDate" value = "true" />

    <!-- 是否去除自动生成的注释 true:是 : false:否 -->

    < property name = "suppressAllComments" value = "true" />

   </ commentGenerator >

 

   <!--数据库连接参数 -->

   < jdbcConnection

     driverClass = "${driverClassName}"

     connectionURL = "${url}"

     userId = "${username}"

     password = "${password}" >

   </ jdbcConnection >

 

   < javaTypeResolver >

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

   </ javaTypeResolver >

 

   <!-- 实体类的包名和存放路径 -->

   < javaModelGenerator targetPackage = "com.shop.order.bean" targetProject = "src/main/java" >

    < property name = "enableSubPackages" value = "true" />

    < property name = "trimStrings" value = "true" />

   </ javaModelGenerator >

 

   <!-- 生成映射文件*.xml的位置-->

   < sqlMapGenerator targetPackage = "mapper" targetProject = "src/main/resources" >

    < property name = "enableSubPackages" value = "true" />

   </ sqlMapGenerator >

 

   <!-- 生成DAO的包名和位置 -->

   < javaClientGenerator type = "XMLMAPPER" targetPackage = "com.shop.order.mapper" targetProject = "src/main/java" >

    < property name = "enableSubPackages" value = "true" />

   </ javaClientGenerator >

 

   <!-- tableName:数据库中的表名或视图名;domainObjectName:生成的实体类的类名-->

   < table tableName = "book" domainObjectName = "Book"

     enableCountByExample = "false"

     enableUpdateByExample = "false"

     enableDeleteByExample = "false"

     enableSelectByExample = "false"

     selectByExampleQueryId = "false" />

   <!-- 可以添加多个需要生产代码的实体-->

   <!--

     <table tableName="xxx" domainObjectName="xxx"

       enableCountByExample="false"

       enableUpdateByExample="false"

       enableDeleteByExample="false"

       enableSelectByExample="false"

       selectByExampleQueryId="false"/>

     ...

     <table tableName="xxx" domainObjectName="xxx"

       enableCountByExample="false"

       enableUpdateByExample="false"

       enableDeleteByExample="false"

       enableSelectByExample="false"

       selectByExampleQueryId="false"/>

   -->

  </ context >

</ generatorConfiguration >

在文件夹下创建generator.properties配置文件

?

1

2

3

4

driverClassName=com.mysql.jdbc.Driver

url=jdbc:mysql: //localhost:3306/store?useUnicode=true&characterEncoding=UTF-8&relaxAutoCommit=true&zeroDateTimeBehavior=convertToNull

username=root

password=root

配置Maven

pom.xml中引入依赖

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

< build >

     < plugins >

       < plugin >

         < groupId >org.mybatis.generator</ groupId >

         < artifactId >mybatis-generator-maven-plugin</ artifactId >

         < version >1.3.5</ version >

         < configuration >

           < configurationFile >src/main/resources/mybatis-generator/generatorConfig.xml</ configurationFile >

           < verbose >true</ verbose >

           < overwrite >true</ overwrite >

         </ configuration >

         < executions >

           < execution >

             < id >Generate MyBatis Artifacts</ id >

             < goals >

               < goal >generate</ goal >

             </ goals >

           </ execution >

         </ executions >

         < dependencies >

           < dependency >

             < groupId >org.mybatis.generator</ groupId >

             < artifactId >mybatis-generator-core</ artifactId >

             < version >1.3.5</ version >

           </ dependency >

           < dependency >

             < groupId >mysql</ groupId >

             < artifactId >mysql-connector-java</ artifactId >

             < version >5.1.30</ version >

           </ dependency >

         </ dependencies >

       </ plugin >

     </ plugins >

   </ build >

执行Maven插件

双击运行mybatis-generator:generate

控制台输出结果,生产mapper和bean文件

总结

到此这篇关于MyBatis Generator简单使用方法的文章就介绍到这了,更多相关MyBatis Generator使用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/fangtao_0216/article/details/113782099

查看更多关于MyBatis Generator的简单使用方法示例的详细内容...

  阅读:16次