目录
2.写好pom.xml和application.yml文件 (设置端口号,配置数据源)
3.引入一个插件,帮助自动生成dao层,model层和mapper目录的代码
4.接着配置mybatis的扫描路径,产生这些文件后,需要让myabtis知道在哪
1.设计表
2.写好pom.xml和application.yml文件 (设置端口号,配置数据源)

3.引入一个插件,帮助自动生成dao层,model层和mapper目录的代码
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>${mybatis-generator-plugin-version}</version>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<phase>deploy</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<configurationFile>
src/main/resources/mybatis/generatorConfig.xml
</configurationFile>
</configuration>
</plugin>

接着配置一下这个插件:在resources文件下添加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:\apache-maven-3.6.1-bin(1)\respository\mysql\mysql-connector-java\5.1.49\mysql-connector-java-5.1.49.jar"/> <!-- <classPathEntry location="D:\maven-warehouse\repository\mysql\mysql-connector-java\8.0.17\mysql-connector-java-8.0.17.jar"/> --> <context id="DB2Tables" targetRuntime="MyBatis3"> <!-- 实体类生成序列化属性--> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <!-- 实体类重写HashCode()和equals()--> <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" /> <!-- 实体类重写toString() --> <plugin type="org.mybatis.generator.plugins.ToStringPlugin" /> <commentGenerator> <!-- 是否去除自动生成的注释 --> <property name="suppressAllComments" value="true"/> <!-- 生成注释是否带时间戳--> <property name="suppressDate" value="true"/> <!-- 生成的Java文件的编码格式 --> <property name="javaFileEncoding" value="utf-8"/> <!-- 格式化java代码--> <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter" /> <!-- 格式化XML代码--> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter" /> </commentGenerator> <!-- 数据库连接驱动类,URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/forum_db?useUnicode=true&characterEncoding=UTF-8" userId="root" password="123456"> </jdbcConnection> <!-- java类型处理器:处理DB中的类型到Java中的类型 --> <javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl"> <!-- 是否有效识别DB中的BigDecimal类型 --> <property name="forceBigDecimals" value="true"/> </javaTypeResolver> <!-- 生成Domain模型:包名(targetPackage)、位置(targetProject) --> <!--<javaModelGenerator targetPackage="com.project.business.domain" targetProject="D:/workSpace/project/src/main/java"> <!– 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false –> <property name="enableSubPackages" value="true"/> <!– 设置是否在getter方法中,对String类型字段调用trim()方法–> <property name="trimStrings" value="true"/> </javaModelGenerator>--> <!--实体类 --> <javaModelGenerator targetPackage="com.xiexiangpeng.forum01.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成xml映射文件:包名(targetPackage)、位置(targetProject) --> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO接口:包名(targetPackage)、位置(targetProject) --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.xiexiangpeng.forum01.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成的表:tableName - 数据库中的表名或视图名,domainObjectName - 实体类名 --> <table tableName="t_article" domainObjectName="Article" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="t_article_reply" domainObjectName="ArticleReply" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="t_board" domainObjectName="Board" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="t_message" domainObjectName="Message" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="t_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>

最低0.47元/天 解锁文章

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



