Mybatis逆向工程
持久层必须有的三个:
1)实体类
2)Mapper接口
3)Mapper配置文件
而Mybatis逆向工程可以帮助我们做这三件事
配置过程
用Idea新建一个maven项目
1)导jar包
<!--mybatis逆向工程-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
2)在pom中的build标签中导入代码生成器的插件
<plugins>
<!-- mybatis逆向工程插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
</dependencies>
<configuration>
<!--配置文件的路径-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
3)创建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>
<!--指定了驱动jar包的位置,这个是针对下载Jar包的方式,因为用了maven所以这个就用不上了-->
<!-- <classPathEntry location="D:/mvn_repository_new/mysql/mysql-connector-java/5.1.45/mysql-connector-java-5.1.45.jar"/>-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="false"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- !!!注意--》 数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/fh-shop?serverTimezone=Asia/Shanghai" userId="root"
password="root">
<!--<property name="serverTimezone" value="UTC"/>-->
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<!-- !!!注意--》 指定生成entity实体类的具体位置-->
<javaModelGenerator targetPackage="com.fh.model" targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- !!!注意--》 指定生成mybatis映射xml文件的包名和位置-->
<sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- !!!注意--》 指定生成mapper接口的具体位置-->
<javaClientGenerator targetPackage="com.fh.mapper" targetProject="./src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--mybatis generator代码生成器在默认的情况下会生成对于表实体类的一个Examle类, 可以更改生成器的配置可避免生成Examle类,
enableCountByExample,enableUpdateByExample,enableDeleteByExample,enableSelectByExample等配置为false后, 就不会生成生成Examle类了 -->
<!--配置所需的表 tableName是数据库中的表名-->
<table tableName="t_bis_wallet" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
<table tableName="t_car" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
4)双击运行

成功之后就会自动编写Java代码
运行过程中如果遇见的错误
-
Loading class
com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.将驱动:
driverClass="com.mysql.jdbc.Driver"改为即可
driverClass="com.mysql.cj.jdbc.Driver" -
Error creating bean with name ‘sqlSessionFactory’ defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.core…
解决:
这个是在Mapper的映射文件里面重复生成代码了,把重复的sql删了,或者把逆向工程生成的文件删了,重新运行就行
新手上路,记录一下
1137

被折叠的 条评论
为什么被折叠?



