• Spring Boot2.0之整合Mybatis


    我在写这个教程时候,踩了个坑,一下子折腾到了凌晨两点半。

    坑: Spring Boot对于Mysql8.1的驱动支持不好啊

    我本地安装的是Mysql8.1版本,在开发时候。pom提示不需要输入驱动版本(因为Spring Boot会自动匹配),毛啊。根本就没有匹配成功。然后我尝试了各种办法。没有解决。

    最后安装的mysql5.6版本轻松搞定

    废话不多说了,赶紧写完,睡觉觉。

    1、创建maven工程,pom:

    <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>jdbcTemplate</groupId>
      <artifactId>com.toov5.jdbc</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      
      <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.3.2.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.1.1</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
                    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.20</version>
                <scope>provided</scope>
            </dependency>
    
        </dependencies>
    
    </project>

    2、application.properties:

    spring.datasource.url=jdbc:mysql://localhost:3306/test
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    

    3、User类代码:

      

    package com.toov5.entity;
    
    import lombok.Data;
    
    @Data  
    public class User {
      private Integer age;
      private String name;
      private  Integer id;
        
    }

    用了lombok插件

    4、Mapper代码:

    package com.toov5.mapper;
    
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.Select;
    
    import com.toov5.entity.User;
    
    
    public interface UserMapper {
        
            @Select("SELECT * FROM USERS WHERE NAME = #{name}")
            User findByName(@Param("name") String name);
            @Insert("INSERT INTO USERS(NAME, AGE) VALUES(#{name}, #{age})")
            int insert(@Param("name") String name, @Param("age") Integer age);
    
    }

    这里没有用到 @Mapper注解哦 因为启动类里面有个好东西代替了!请接着往下读

    5、service代码

    package com.toov5.service;
    
    
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.toov5.mapper.UserMapper;
    
    import lombok.extern.slf4j.Slf4j;
    
    @Service
    @Slf4j
    public class UserService {
      @Autowired
      private UserMapper userMapper;
      
      public int insertUser(String name, Integer age){
         int result = userMapper.insert(name, age);
          log.info("####################",result);
          return result;
      }
    }

    6、Controller代码:

    package com.toov5.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.toov5.service.UserService;
    
    @RestController
    public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/insertUser")  
      public Integer insertUser(String name, Integer age){
         return userService.insertUser(name, age);
         
         }
         
    }

    7、启动类

    package com.toov5.app;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ComponentScan;
    
    @ComponentScan(basePackages="com.toov5")
    @MapperScan(basePackages="com.toov5.mapper") 
    @SpringBootApplication
    public class app {
      public static void main(String[] args){
          SpringApplication.run(app.class, args);
      }
    }

    这里重点说下:

    这个注解,省去了mapper层类的@Mapper注解

    整个项目目录结构:

    访问:

     查看数据库:

    都成功了~~ 是不是很好玩呀

  • 相关阅读:
    三: 视图容器。
    (微信小程序)一 : 初识微信小程序
    Centos 从零开始 (四)
    Centos 从零开始 (三)
    Centos 从零开始 (二)
    Centos 从零开始 (一)
    9、下拉刷新 与 上拉加载
    8、导航:Nav
    C#异步编程 Task await的理解
    WPF 获取主线程
  • 原文地址:https://www.cnblogs.com/toov5/p/9814267.html
Copyright © 2020-2023  润新知