SSM框架通過mybatis-generator自動(dòng)生成代碼(推薦)
一、首先eclipse配置好maven環(huán)境,并且創(chuàng)建好一個(gè)SSM框架的工程
二、在pom.xml中添加plugin
<build> <finalName>ssm_web</finalName> <pluginManagement> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</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.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> <scope>runtime</scope> </dependency> </dependencies> </plugin> </plugins> </pluginManagement> </build>
注意:
1、pom中添加插件要在<build></build>標(biāo)簽里面,并且在這里指定數(shù)據(jù)庫驅(qū)動(dòng),那么在下一步配置generatorConfig.xml的時(shí)候就不用在指定數(shù)據(jù)庫驅(qū)動(dòng)的本地路徑;
2、src/main/resources/mybatis-generator/generatorConfig.xml指定的是generatorConfig.xml配置文件的路徑,大家可以根據(jù)自己的實(shí)際情況調(diào)整;
三、generatorConfig.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> <!-- <classPathEntry location="E:/maven/repository/mysql/mysql-connector-java/5.1.40/mysql-connector-java-5.1.40.jar"/> --> <context id="my" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="false" /> <property name="suppressAllComments" value="true" /> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/crm" userId="root" password="root" /> <javaModelGenerator targetPackage="com.xdw.model" targetProject="F:/javawebworkspace/ssm_web/src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.xdw.mapping" targetProject="F:/javawebworkspace/ssm_web/src/main/java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator targetPackage="com.xdw.dao" targetProject="F:/javawebworkspace/ssm_web/src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="house_type" domainObjectName="HouseType"> <!-- <property name="useActualColumnNames" value="true"/> --> </table> </context> </generatorConfiguration>
配置講解:
1、classPathEntry location在這里可以不用配置,因?yàn)橹皃om中已經(jīng)配置了數(shù)據(jù)庫驅(qū)動(dòng);
2、jdbcConnection按照自己的數(shù)據(jù)庫配置相應(yīng)的驅(qū)動(dòng)類,URL,用戶名和密碼;
3、javaModelGenerator,sqlMapGenerator,javaClientGenerator配置相應(yīng)要生成的pojo類,DAO類和mapper對(duì)應(yīng)的xml文件,targetPackage屬性指定包名,targetProject指定自己工程的路徑,
4、table標(biāo)簽配置數(shù)據(jù)庫表和實(shí)體類的映射,tableName屬性指定表名,domainObjectName指定生成的pojo類名;<property name="useActualColumnNames" value="true"/>
如果配置的話,那么生成的實(shí)體類的屬性名稱就和數(shù)據(jù)庫
表的每個(gè)字段的名稱一樣,如果不配置,會(huì)將數(shù)據(jù)表里面的字段名稱通過_連接的字段自動(dòng)生成經(jīng)典的駝峰表示法,比如我這里有個(gè)字段名為type_id,生成的屬性名稱為typeId;
建議大家在數(shù)據(jù)庫建表的時(shí)候采用_將單詞分隔;
四、創(chuàng)建好之后點(diǎn)擊maven build
彈出
在goals中填入mybatis-generator:generate
,然后點(diǎn)擊Run
也可以用maven命令行輸入mvn mybatis-generator:generate
五、執(zhí)行結(jié)果如下:
總結(jié)
以上所述是小編給大家介紹的SSM框架通過mybatis-generator自動(dòng)生成代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Mybatis generator自動(dòng)生成代碼插件實(shí)例解析
- 基于Mybatis Plus實(shí)現(xiàn)代碼生成器CodeGenerator
- 使用mybatis-plus-generator進(jìn)行代碼自動(dòng)生成的方法
- IDEA mybatis-generator逆向工程生成代碼
- 詳解在IDEA中使用MyBatis Generator逆向工程生成代碼
- IDEA Maven Mybatis generator 自動(dòng)生成代碼(實(shí)例講解)
- MyBatis Generator生成代碼及使用方式詳解
- Java的MyBatis框架中MyBatis Generator代碼生成器的用法
- Mybatis generator如何自動(dòng)生成代碼
相關(guān)文章
Spring使用@Conditional進(jìn)行條件裝配的實(shí)現(xiàn)
在spring中有些bean需要滿足某些環(huán)境條件才創(chuàng)建某個(gè)bean,這個(gè)時(shí)候可以在bean定義上使用@Conditional注解來修飾,所以本文給大家介紹了Spring使用@Conditional進(jìn)行條件裝配的實(shí)現(xiàn),文中通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12Java動(dòng)態(tài)調(diào)用類中方法代碼
這篇文章主要介紹了Java動(dòng)態(tài)調(diào)用類中方法代碼,需要的朋友可以參考下2014-02-02RocketMQ4.5.X 實(shí)現(xiàn)修改生產(chǎn)者消費(fèi)者日志保存路徑
這篇文章主要介紹了RocketMQ4.5.X 實(shí)現(xiàn)修改生產(chǎn)者消費(fèi)者日志保存路徑方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07JDBC查詢Map轉(zhuǎn)對(duì)象實(shí)現(xiàn)過程詳解
這篇文章主要介紹了JDBC查詢Map轉(zhuǎn)對(duì)象實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10詳解Spring學(xué)習(xí)之編程式事務(wù)管理
本篇文章主要介紹了詳解Spring學(xué)習(xí)之編程式事務(wù)管理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07