Spring Boot2整合mybatis

Spring Boot整合mybatis

    • 1、创建maven项目
        • 1.1、创建主程序类MainApplication.java
        • 1.2、创建相关文件
        • 1.3、项目架构图
    • 2、pom.xml导入所需依赖
    • 3、application.yaml配置相关数据
    • 4、mybatis-config.xml
    • 5、bean包下创建User.java
    • 6、 mapper包里面创建接口UserMapper.java
    • 7、mybatis包下的mapper包下创建UserMapper.xml
    • 8、 service包下创建UserService.java(也可以分成接口和实现类我就方便着写了)
    • 9、controller包下创建Usercontroller.java
    • 10、启动项目
    • 11、注意点
    • 12、全注解

我使用的是druid数据库连接池

1、创建maven项目

Spring Boot2整合mybatis_第1张图片

1.1、创建主程序类MainApplication.java

@SpringBootApplication

package com.miao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * 主程序类
 * @SpringBootApplication :这是一个SpringBoot应用
 *
 */
@SpringBootApplication
public class MainApplication {
     

    public static void main(String[] args) {
     

        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

        //run.getBean("");
    }
}

1.2、创建相关文件

  1. application.xml(或者application.properties)
  2. java文件夹下创建所需包bean、service、controller、mapper,config
  3. resource文件夹下创建mybatis文件夹用来存放mybatis配置文件和mapper映射文件
  4. mabatis文件夹下创建mybatis-config.xml(mybatis核心配置文件),和mapper文件夹(用来存放mapper映射文件)

1.3、项目架构图

Spring Boot2整合mybatis_第2张图片
Spring Boot2整合mybatis_第3张图片

2、pom.xml导入所需依赖

web、jdbc、mysql驱动、druid、mybatis


<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.0modelVersion>

    <groupId>org.miaogroupId>
    <artifactId>SpringBootartifactId>
    <version>1.0-SNAPSHOTversion>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
    properties>


    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.3.8.RELEASEversion>

    parent>


    <dependencies>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-jdbcartifactId>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.49version>
        dependency>

        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.17version>
        dependency>


        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.4version>
        dependency>

    dependencies>

project>

3、application.yaml配置相关数据

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

    druid:
      aop-patterns: com.atguigu.admin.*  #监控SpringBean
      filters: stat,wall     # 底层开启功能,stat(sql监控),wall(防火墙)

      stat-view-servlet: # 配置监控页功能
        enabled: true
        login-username: admin
        login-password: admin
        resetEnable: false

      web-stat-filter: # 监控web
        enabled: true
        urlPattern: /*
        exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'


      filter:
        stat: # 对上面filters里面的stat的详细配置
          slow-sql-millis: 1000
          logSlowSql: true
          enabled: true
        wall:
          enabled: true
          config:
            drop-table-allow: false

mybatis:
  config-location: classpath:mybatis/mybatis-config.xml #全局配置文件位置
  mapper-locations: classpath:mybatis/mapper/*.xml #sql映射文件位置

4、mybatis-config.xml



<configuration>


configuration>

5、bean包下创建User.java

我要查询的表 变量一定要对应好表
Spring Boot2整合mybatis_第4张图片
user.java
谨记:添加注解@Component

import org.springframework.stereotype.Component;

@Component
public class User {
     

    private int uid;
    private String uname;
    private String sex;
    private String upass;

    public User(){
     

    }

    public User(int uid, String uname, String sex, String upass) {
     
        this.uid = uid;
        this.uname = uname;
        this.sex = sex;
        this.upass = upass;
    }

    public int getUid() {
     
        return uid;
    }

    public void setUid(int uid) {
     
        this.uid = uid;
    }

    public String getUname() {
     
        return uname;
    }

    public void setUname(String uname) {
     
        this.uname = uname;
    }

    public String getSex() {
     
        return sex;
    }

    public void setSex(String sex) {
     
        this.sex = sex;
    }

    public String getUpass() {
     
        return upass;
    }

    public void setUpass(String upass) {
     
        this.upass = upass;
    }
       @Override
    public String toString() {
     
        return "User{" +
                "uid=" + uid +
                ", uname='" + uname + '\'' +
                ", sex='" + sex + '\'' +
                ", upass='" + upass + '\'' +
                '}';
    }
}

6、 mapper包里面创建接口UserMapper.java

谨记:添加注解@Mapper

package com.miao.mapper;

import com.miao.bean.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper {
     

    public User getById(Integer id);
}

7、mybatis包下的mapper包下创建UserMapper.xml

注意:mapper的namespace千万别写错
这个里面可能犯错的地方很多一定要多注意




<mapper namespace="com.miao.mapper.UserMapper">


    <select id="getById" resultType="com.miao.bean.User">

        select * from users where uid = #{id}
    select>
mapper>

8、 service包下创建UserService.java(也可以分成接口和实现类我就方便着写了)

@Service
public class UserService {
     

    @Autowired
    UserMapper usermapper;

    public User getById(Integer id){
     
        User user = usermapper.getById(id);
        return user;
    }
}

9、controller包下创建Usercontroller.java

@RestController
public class Usercontroller{
     


    @Autowired
    UserService userService;

    @GetMapping("/user")
    public Map<String,Object> fun01(@RequestParam("id") Integer id){
     

        Map<String,Object> map = new HashMap<>();
        User user = userService.getById(id);
        map.put("user",user);

        return map;
    }
}

10、启动项目

访问http://localhost:8080/user?id=101
Spring Boot2整合mybatis_第5张图片
成功

总算成功了,中间出了好多问题

11、注意点

(操作的表不同类名也各不相同,我以我的为准说一下注意点)

  1. application配置文件(spring核心配置文件)里面mybatis配置文件的位置是否正确(这个地方idea是无法智能识别的,拼错了也不会报红要注意)
  2. bean包下的User.java、Service包下的UserService.java、controller·包下的Usercontroller.java是否加入容器否则会找不到组件
  3. bean包下的User.java里面的变量是否与数据库相照应
  4. bean包下的User.java是否有set、get等各种方法
  5. mapper包下的User.java接口是否添加注解@Mapper
  6. UserMapper.xml里面的namespace是否正确(指向UserMapper.java)
  7. UserMapper.xml里面的sql语句是否正确

12、全注解

application.yaml不用配置mybatis配置文件的位置和mapper映射文件的位置了
具体如下
application.xml

mybatis:
# config-location: classpath:mybatis/mybatis-config.xml #全局配置文件位置
#  mapper-locations: classpath:mybatis/mapper/*.xml #sql映射文件位置

Spring Boot2整合mybatis_第6张图片
UserMapper接口里面就要利用注解添加上sql语句

    @Select("select * from users where uid = #{id}")
    public User getById(Integer id);

其他的都不需要改变

你可能感兴趣的:(Spring,Boot,mybatis,spring,boot)