springboot整合mybatis

参考文档:Mybatis代码生成器Mybatis-Generator使用及配置详解

pom文件依赖的jar包

<dependency>
          <groupId>org.mybatis.generatorgroupId>
          <artifactId>mybatis-generator-coreartifactId>
          <version>1.4.0version>
      dependency>
      
      <dependency>
          <groupId>org.mybatis.spring.bootgroupId>
          <artifactId>mybatis-spring-boot-starterartifactId>
          
          <version>1.2.0version>
      dependency>
       <dependency>
          <groupId>mysqlgroupId>
          <artifactId>mysql-connector-javaartifactId>
          <version>8.0.33version>
      dependency>

pom文件里的maven插件(里面配置了mybatis-generator-config.xml的路径,到时候mybatis根据这个配置文件去生成)

 <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.7version>
                <configuration>
                    
                    <configurationFile>src/main/resources/mybatis-generator-config.xmlconfigurationFile>
                    <overwrite>trueoverwrite>
                    <verbose>trueverbose>
                configuration>
                <dependencies>
                    
                    <dependency>
                        <groupId>mysqlgroupId>
                        <artifactId>mysql-connector-javaartifactId>
                        <version>8.0.19version>
                    dependency>
                    <dependency>
                        <groupId>org.mybatis.generatorgroupId>
                        <artifactId>mybatis-generator-coreartifactId>
                        <version>1.4.0version>
                    dependency>
                dependencies>
            plugin>

mybatis-generator-config.xml文件如下,修改数据库的地址,用户名密码和javaModelGenerator和javaClientGenerator的路径


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="myContext" targetRuntime="MyBatis3">

        <property name="javaFileEncoding" value="UTF-8"/>

        
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
        
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
        
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
        
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/>

        
        <commentGenerator>
            
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="false"/>
            
            <property name="addRemarkComments" value="true"/>
        commentGenerator>

       
        
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/Stu?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai"
                        userId="root"
                        password="XXXX">
            
            
            <property name="nullCatalogMeansCurrent" value="true"/>
        jdbcConnection>


        <javaTypeResolver>
            
            
            
            <property name="forceBigDecimals" value="false"/>
            
            <property name="useJSR310Types" value="false"/>
        javaTypeResolver>

        
        <javaModelGenerator targetPackage="com.jarctique.jmeterredis.pojo" targetProject="src/main/java">
            
            <property name="enableSubPackages" value="false"/>
            
            <property name="trimStrings" value="true"/>
        javaModelGenerator>

        
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            
            <property name="enableSubPackages" value="false"/>
        sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.jarctique.jmeterredis.mapper"
                             targetProject="src/main/java">
            
            <property name="enableSubPackages" value="false"/>
        javaClientGenerator>


        <table tableName="%"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
            <domainObjectRenamingRule searchString="^T" replaceString=""/>
        table>

    context>
generatorConfiguration>

application.yml文件配置(数据库地址,用户名密码修改成自己的,pojo路径也要改)

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/Stu?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
    username: root
    password: XXXX
mybatis:
  mapper-locations: classpath:mapper/*.xml
  #目的是为了省略resultType里的代码量
  type-aliases-package: com.jarctique.jmeterredis.pojo

数据库的表
springboot整合mybatis_第1张图片

双击生成对应的pojo和mapper
springboot整合mybatis_第2张图片

springboot整合mybatis_第3张图片
springboot启动类增加注解,添加mapper扫描的路径
@MapperScan({“com.jarctique.jmeterredis.mapper”})

mport org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.mybatis.spring.annotation.MapperScan;

@MapperScan({"com.jarctique.jmeterredis.mapper"})
@SpringBootApplication
public class JmeterredisApplication {

    public static void main(String[] args) {
        SpringApplication.run(JmeterredisApplication.class, args);

    }

你可能感兴趣的:(spring,boot,mybatis,后端)