mybatis配置
1、配置maven
<!-- 集成mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency>
<!-- mybatis generator 自动生成代码插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <configuration> <configurationFile>src/main/resources/generator/generator-config.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> </dependencies> </plugin>
2、application.yml文件:
mybatis.mapper-locations=classpath:/mapper/**/*.xml
3、在resources\generator创建generator-config.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> <context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat"> <!-- 自动检查关键字,为关键字增加反引号 --> <property name="autoDelimitKeywords" value="true"/> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <!--覆盖生成XML文件--> <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" /> <!-- 生成的实体类添加toString()方法 --> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/> <!-- 不生成注释 --> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--下面的com.mysql.cj.jdbc.Driver代表从哪个包里去寻找驱动--> <!--cj是后加的,如果为老版本则不需要加cj--> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3307/Tacit?serverTimezone=Asia/Shanghai" userId="root" password="123456"> <!--jdbc:mysql://为固定格式--> <!--localhost:3307 其中localhost代表本机,而3307代表其对应的端口--> <!--wiki2是想要连接的数据库--> <!--serverTimezone=Asia/Shanghai 如果在亚洲,需要添加此段语句,不然时驱会发生错误--> </jdbcConnection> <!-- domain类的位置 --> <javaModelGenerator targetProject="src/main/java" targetPackage="com.Tacit.miniapp.domain"/> <!-- mapper xml的位置 --> <sqlMapGenerator targetProject="src/main/resources" targetPackage="mapper"/> <!-- mapper类的位置 --> <javaClientGenerator targetProject="src/main/java" targetPackage="com.Tacit.miniapp.mapper" type="XMLMAPPER"/> <!--底下tableName后的字符,代表用当前数据库里的哪一个表来创建 --> <!--<table tableName="ebook"/>--> <table tableName="template_class"/> </context> </generatorConfiguration>