Springboot 整合Mybatis


Springboot整合Mybatis

添加Mybatis依赖

方式一:编辑pom.xml 添加dependency



    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    2.1.4




    mysql
    mysql-connector-java
    runtime

方式二:创建项目时选择dependency

  • 新建一个Spring Initializr 项目

    image-20220314173402934

  • 选择项目所需的依赖

    image-20220314105244360

  • 创建后的pom文件

    <?xml version="1.0" encoding="UTF-8"?>
    
        4.0.0
        
            org.springframework.boot
            spring-boot-starter-parent
            2.6.4
            
        
        
        com.smile
        blog
        0.0.1-SNAPSHOT
        blog
        blog
        
        
            1.8
        
        
            
                org.springframework.boot
                spring-boot-starter-web
            
            
                org.mybatis.spring.boot
                mybatis-spring-boot-starter
                2.2.2
            
    
            
                mysql
                mysql-connector-java
                runtime
            
            
                org.springframework.boot
                spring-boot-starter-test
                test
            
        
    
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
            
        
    
    
    

创建mapper包

image-20220314173838270

修改配置文件

方式一:properties文件

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/crm?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root

方式二:yml文件

#Mybatis
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/crm?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=GMT%2B8
    driver-class-name: com.mysql.cj.jdbc.Driver
    password: root
    username: root

mybatis:
  type-aliases-package: com.smile.blog.entity
  mapper-locations: classpath*:/mapper/*.xml

创建实体类

package com.smile.blog.entity;

/**
 * @Notes
 * @Date 2022/3/14
 * @Time 17:41
 * @Author smile
 */
public class User {
    private Integer id;

    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

创建Dao

package com.smile.blog.dao;

import com.smile.blog.entity.User;


public interface UserDao {
    User getUser(Integer id);
}
  • 给每个dao设置注解@Mapper
  • Application中设置@MapperScan(basePackages = "com.smile.blog.dao")

创建service接口和service实现类

package com.smile.blog.service.impl;

import com.smile.blog.dao.UserDao;
import com.smile.blog.entity.User;
import com.smile.blog.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Notes
 * @Date 2022/3/14
 * @Time 17:45
 * @Author smile
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    public User getUser(Integer id)
    {
        return userDao.getUser(id);
    }
}
package com.smile.blog.service;

import com.smile.blog.entity.User;

/**
 * @Notes
 * @Date 2022/3/14
 * @Time 17:45
 * @Author smile
 */
public interface UserService {
    User getUser(Integer id);
}

创建xml

<?xml version="1.0" encoding="UTF-8" ?>


    

创建控制器

package com.smile.blog.controller;

import com.smile.blog.entity.User;
import com.smile.blog.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @Notes
 * @Date 2022/3/14
 * @Time 17:51
 * @Author smile
 */
@Controller
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/user")
    @ResponseBody
    public String user()
    {
        User user = userService.getUser(1);

        return user.toString();
    }
}

整体的目录结构

├─java
│  └─com
│      └─smile
│          └─blog
│              │  Application.java
│              │  
│              ├─controller
│              │      UserController.java
│              │      
│              ├─dao
│              │      UserDao.java
│              │      
│              ├─entity
│              │      User.java
│              │      
│              └─service
│                  │  UserService.java
│                  │  
│                  └─impl
│                          UserServiceImpl.java
│                          
└─resources
    │  application.yml
    │  
    ├─mapper
    │      Dao.xml
    │      
    ├─static
    └─templates

结果响应

image-20220314175622534

设置Mybatis代码自动生成器