如何让idea自动创建mybatis配置文件?

如何让idea自动创建mybatis配置文件?,第1张

一、在pom.xml中添加plugin

其中generatorConfig.xml的位置,大家根据实际情况自行调整

二、generatorConfig.xml配置文件

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

7      location="C:/Oracle/Middleware/wlserver_10.3/server/lib/ojdbc6.jar"/>

8     <context id="my" targetRuntime="MyBatis3">

9     <commentGenerator>

10    <property name="suppressDate" value="false"/>

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

12    </commentGenerator>

13    <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"

14     connectionURL="jdbc:oracle:thin:@172.20.16.***:1521:CARGO" userId="***"

15    password="***"/>

16    <javaModelGenerator targetPackage="ctas.test.entity"

17    targetProject="D:/yangjm/Code/CTAS/JAVAEE/CTAS2CCSP/src/main/java">

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

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

20   </javaModelGenerator>

21   <sqlMapGenerator targetPackage="ctas.test.entity.xml"

22   targetProject="D:/yangjm/Code/CTAS/JAVAEE/CTAS2CCSP/src/main/java">

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

24    </sqlMapGenerator>

25   <javaClientGenerator targetPackage="ctas.test.mapper"

26    targetProject="D:/yangjm/Code/CTAS/JAVAEE/CTAS2CCSP/src/main/java" type="XMLMAPPER">

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

28    </javaClientGenerator>

29   <!--<table tableName="T_FEE_AGTBILL" domainObjectName="FeeAgentBill"

30    enableCountByExample="false" enableUpdateByExample="false"

31   enableDeleteByExample="false" enableSelectByExample="false"

32    selectByExampleQueryId="false"/>-->

33   <table tableName="CTAS_FEE_BASE" domainObjectName="FeeBase"

34    enableCountByExample="false" enableUpdateByExample="false"

35    enableDeleteByExample="false" enableSelectByExample="false"

36    selectByExampleQueryId="false">

37   <!--<columnRenamingRule searchString="^D_"

38   replaceString=""/>-->

39   </table>

40   </context>

41   </generatorConfiguration>

根据具体问题类型,进行步骤拆解/原因原理分析/内容拓展等。

具体步骤如下:/导致这种情况的原因主要是……

一、在pom.xml中添加plugin

其中generatorConfig.xml的位置,大家根据实际情况自行调整

二、generatorConfig.xml配置文件

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

6 <generatorConfiguration>

7 <classPathEntry

8 location="C:/Oracle/Middleware/wlserver_10.3/server/lib/ojdbc6.jar"/>

9 <context id="my" targetRuntime="MyBatis3">

10 <commentGenerator>

11 <property name="suppressDate" value="false"/>

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

13 </commentGenerator>

14

15 <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"

16 connectionURL="jdbc:oracle:thin:@172.20.16.***:1521:CARGO" userId="***"

17 password="***"/>

18

19 <javaModelGenerator targetPackage="ctas.test.entity"

20 targetProject="D:/yangjm/Code/CTAS/JAVAEE/CTAS2CCSP/src/main/java">

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

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

23 </javaModelGenerator>

24

25 <sqlMapGenerator targetPackage="ctas.test.entity.xml"

26 targetProject="D:/yangjm/Code/CTAS/JAVAEE/CTAS2CCSP/src/main/java">

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

28 </sqlMapGenerator>

29

30 <javaClientGenerator targetPackage="ctas.test.mapper"

31 targetProject="D:/yangjm/Code/CTAS/JAVAEE/CTAS2CCSP/src/main/java" type="XMLMAPPER">

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

33 </javaClientGenerator>

34

35 <!--<table tableName="T_FEE_AGTBILL" domainObjectName="FeeAgentBill"

36enableCountByExample="false" enableUpdateByExample="false"

37enableDeleteByExample="false" enableSelectByExample="false"

38selectByExampleQueryId="false"/>-->

39

40 <table tableName="CTAS_FEE_BASE" domainObjectName="FeeBase"

41enableCountByExample="false" enableUpdateByExample="false"

42enableDeleteByExample="false" enableSelectByExample="false"

43selectByExampleQueryId="false">

44 <!--<columnRenamingRule searchString="^D_"

45 replaceString=""/>-->

46 </table>

47

48 </context>

49 </generatorConfiguration>

几个要点:

a) 因为生成过程中需要连接db,所以第3行指定了驱动jar包的位置

b) 15-17行为连接字符串

c) 19-33行指定生成“entity实体类、mybatis映射xml文件、mapper接口”的具体位置

d) 40-46行为具体要生成的表,如果有多个表,复制这一段,改下表名即可

三、使用方式

mvn mybatis-generator:generate

如果是在intellij 环境,直接鼠标点击即可

四、最后给一些小技巧:

a) 建表时,字段名称建议用"_"分隔多个单词,比如:AWB_NO、REC_ID...,这样生成的entity,属性名称就会变成漂亮的驼峰命名,即:awbNo、recId

b)oracle中,数值形的字段,如果指定精度,比如Number(12,2),默认生成entity属性是BigDecimal型 ,如果不指定精度,比如:Number(9),指默认生成的是Long型

c)oracle中的nvarchar/nvarchar2,mybatis-generator会识别成Object型,建议不要用nvarchar2,改用varchar2


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11627977.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存