在springboot中使用mybatis generator
pom中引入插件
1 | <plugin> |
2 | <groupId>org.mybatis.generator</groupId> |
3 | <artifactId>mybatis-generator-maven-plugin</artifactId> |
4 | <version>1.3.7</version> |
5 | <dependencies> |
6 | <dependency> |
7 | <groupId>com.h2database</groupId> |
8 | <artifactId>h2</artifactId> |
9 | <version>1.4.200</version> |
10 | </dependency> |
11 | </dependencies> |
12 | </plugin> |
在resources下编写xml文件generatorConfig.xml
1 | |
2 | |
3 | |
4 | |
5 | |
6 | <generatorConfiguration> |
7 | |
8 | <context id="DB2Tables" targetRuntime="MyBatis3"> |
9 | |
10 | <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin> |
11 | <jdbcConnection driverClass="org.h2.Driver" |
12 | connectionURL="jdbc:h2:~/demo" |
13 | userId="sa" |
14 | password="123"> |
15 | </jdbcConnection> |
16 | |
17 | <javaTypeResolver> |
18 | <property name="forceBigDecimals" value="false"/> |
19 | </javaTypeResolver> |
20 | |
21 | <javaModelGenerator targetPackage="com.lgc.demo.model" targetProject="src/main/java"> |
22 | <property name="enableSubPackages" value="true"/> |
23 | <property name="trimStrings" value="true"/> |
24 | </javaModelGenerator> |
25 | |
26 | <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> |
27 | <property name="enableSubPackages" value="true"/> |
28 | </sqlMapGenerator> |
29 | |
30 | <javaClientGenerator type="XMLMAPPER" targetPackage="com.lgc.demo.mapper" |
31 | targetProject="src/main/java"> |
32 | <property name="enableSubPackages" value="true"/> |
33 | </javaClientGenerator> |
34 | |
35 | <table tableName="user" domainObjectName="User"></table> |
36 | <table tableName="question" domainObjectName="Question"></table> |
37 | </context> |
38 | </generatorConfiguration> |
在主配置类添加注释
1 | |
2 | ("com.lgc.demo.mapper") |
3 | public class DemoApplication { |
4 | |
5 | public static void main(String[] args) { |
6 | SpringApplication.run(DemoApplication.class, args); |
7 | } |
8 | |
9 | } |
在application.properties中添加配置
1 | mybatis.type-aliases-package=com.lgc.demo.mapper |
2 | mybatis.mapper-locations=classpath:mapper/*.xml |
最后运行命令
1 | mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate |