zoukankan      html  css  js  c++  java
  • springboot 整合 mybatis

     springboot 整合 mybatis 看了忘不了

    ( springboot 不管整合啥,记住三大步: 1、引入依赖 2、配置 3、使用 )

    首先是引入依赖(很长):(里面有 逆向工程得插件和依赖(已经有标注) 可以不用理会,直接删除或留着都不碍事)

    最好自己直接用 idea 建个 springboot 项目,把 mybatis 也选中,因为自己组合很容易出现 XXXX ,算了,自己经历吧,

    还是自己凑吧,别直接建,错几次,错到你气急败坏,这样记得清楚

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>
       <parent>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-parent</artifactId>
           <version>2.5.3</version>
           <relativePath/> <!-- lookup parent from repository -->
       </parent>
       <groupId>com.tt</groupId>
       <artifactId>mybatis-generator</artifactId>
       <version>0.0.1-SNAPSHOT</version>
       <name>mybatis-generator</name>
       <description>Demo project for Spring Boot</description>
       <properties>
           <java.version>1.8</java.version>
       </properties>
       <dependencies>
       
       <!--springboot 开始-->
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-web</artifactId>
           </dependency>
          <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-devtools</artifactId>
               <scope>runtime</scope>
               <optional>true</optional>
           </dependency>
           
           <!--mybatis 开始-->
           <dependency>
               <groupId>org.mybatis.spring.boot</groupId>
               <artifactId>mybatis-spring-boot-starter</artifactId>
               <version>2.2.0</version>
           </dependency>

    <!-- mysql 开始-->
           <dependency>
               <groupId>mysql</groupId>
               <artifactId>mysql-connector-java</artifactId>
               <scope>runtime</scope>
           </dependency>
           
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-test</artifactId>
               <scope>test</scope>
           </dependency>
           
           <!--Lombok依赖-->
           <dependency>
               <groupId>org.projectlombok</groupId>
               <artifactId>lombok</artifactId>
           </dependency>
           
           <!--mybatis反向工程-->
           <dependency>
               <groupId>tk.mybatis</groupId>
               <artifactId>mapper-generator</artifactId>
               <version>1.1.3</version>
           </dependency>
           
           <!--单元测试-->
           <dependency>
               <groupId>junit</groupId>
               <artifactId>junit</artifactId>
           </dependency>

       </dependencies>

       <build>
           <plugins>
               <plugin>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-maven-plugin</artifactId>
               </plugin>

    <!-- 下边是逆向工程 插件-->
               <plugin>
                   <groupId>org.mybatis.generator</groupId>
                   <artifactId>mybatis-generator-maven-plugin</artifactId>
                   <version>1.3.7</version>

                   <dependencies>
                       <dependency>
                           <groupId>mysql</groupId>
                           <artifactId>mysql-connector-java</artifactId>
                           <version>8.0.16</version>
                       </dependency>
                       <dependency>
                           <groupId>com.softwareloop</groupId>
                           <artifactId>mybatis-generator-lombok-plugin</artifactId>
                           <version>1.0</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>
       </build>

    </project>

    然后 注意:配置文件(记住 springboot 三大步: 依赖 配置 使用)

    spring:
    datasource:
      url: jdbc:mysql://localhost:3306/datasource01?characterEncoding=utf8
      driverClassName: com.mysql.cj.jdbc.Driver
      username: root
      password: mysql0902
      hikari:
        connection-init-sql: set names utf8mb4
    mybatis:
    mapper-locations: classpath*:mapper/**.xml,classpath*:/mapper/**/*Mapper.xml

    使用:

    创建实体类(记得对应数据库表)

    package com.tt.mybatisgenerator.entity.ment;

    import lombok.AllArgsConstructor;
    import lombok.Builder;
    import lombok.Data;
    import lombok.NoArgsConstructor;

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public class MentMain {
       private String id;

       private String createUsername;

       private String agentUsername;

       private String createId;
    }

    创建 mapper

    import com.tt.mybatisgenerator.entity.ment.MentMain;
    import org.apache.ibatis.annotations.Mapper;
    import org.springframework.stereotype.Repository;

    @Mapper
    @Repository
    public interface MentMainMapper {

       
       List<MentMain> getAll();

    }

    创建对应 *mapper.xml 文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.tt.mybatisgenerator.mapper.MentMainMapper">
     <resultMap id="BaseResultMap" type="com.tt.mybatisgenerator.entity.ment.MentMain">
       <id column="id" jdbcType="VARCHAR" property="id" />
       <result column="create_username" jdbcType="VARCHAR" property="createUsername" />
       <result column="agent_username" jdbcType="VARCHAR" property="agentUsername" />
       <result column="create_id" jdbcType="VARCHAR" property="createId" />
     </resultMap>
     <sql id="Base_Column_List">
      id, create_username, agent_username, create_id
     </sql>
     <select id="getAll" resultMap="BaseResultMap">
      select * from ment_main
     </select>

    </mapper>

    测试

    直接在测试模块进行测试(注意是测试模块,不能是单元测试,单元测试并不在 spring 容器内 所以无法获取 MentMainMapper mentMainMapper;

    package com.tt.mybatisgenerator;

    import java.util.List;

    import javax.sql.DataSource;

    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;

    import com.tt.mybatisgenerator.entity.ment.SourceRecordLine;
    import com.tt.mybatisgenerator.mapper.SourceRecordLineMapper;

    @SpringBootTest
    class MybatisGeneratorApplicationTests {

       @Autowired
       DataSource dataSource;

       @Autowired
       MentMainMapper mentMainMapper;

       @Test
       void contextLoads(){
           System.out.println(mentMainMapper);
                   List<MentMain> all =  mentMainMapper.getAll();
           for (MentMain mentMain : all) {
               System.out.println(mentMain);
          }
      }

    }

    不相信可以去 单元测试试试

    指定报错:


    java.lang.NullPointerException
    at com.tt.mybatisgenerator.test.MentMainTest.t1(MentMainTest.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

     

  • 相关阅读:
    ESAPI = Enterprise Security API
    WingIDE中调试GAE(google app engine)
    C# 发送Http请求 WebClient类
    [转]使用Google App Engine Helper for Django
    装上Window7
    最近遇到个关于接口的奇怪的问题
    JNDI概述(转载)
    Google App Engine 中通过自定义Django的filter解决时区问题
    C# string与byte[]互转
    Python天天美味(33) 五分钟理解元类(Metaclasses)[转]
  • 原文地址:https://www.cnblogs.com/blog-tian/p/15055960.html
Copyright © 2011-2022 走看看