springboot + mapper xml配置实现方式一般流程

1 数据表

CREATE TABLE sm_user (
    id int not null primary key auto_increment,
    username varchar(256) not null,
    age int not null default 0,
    createtime int not null default 0
) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

2 pom.xml依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.6
         
    
    com.example
    shirojwtdemo
    0.0.1-SNAPSHOT
    shirojwtdemo
    shirojwtdemo
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.0
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            mysql
            mysql-connector-java
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
        
            
                src/main/java
                
                    **/*.xml
                
            
        
    


3目录结构

项目目录结构

4 编码过程

4.1 添加实体类

package com.example.shirojwtdemo.user.entity;

import lombok.Data;

@Data
public class UserEntity {
    private int id;
    private String username;
    private String passwd;
    private String salt;
    private int createtime;
}

4.2 添加mapper类

package com.example.shirojwtdemo.user.mapper;

import com.example.shirojwtdemo.user.entity.UserEntity;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

@Repository
@Mapper
public interface UserMapper {
    public UserEntity getUserById(int id);
}

4.3添加xml映射文件

为了代码阅读方便,一般把xml映射文件放置在mapper文件所在目录的同级文件夹的子文件夹xml下,并取与mapper类文件相同的文件名。





    
        
        
        
        
        
    

    

4.4 配置映射文件的扫描路径

由于xml映射文件放置在java的目录下,为了让编译器发现、包含xml,则需要进行相应的配置,首先在pom.xml文件包含java目录下的资源,如下图:


pom.xml java包含xml资源配置

其次,需要在application.yml添加mapper xml文件的位置:
mapper-locations

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    username: root
    password: 12345678
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  mapper-locations: classpath*:com/example/shirojwtdemo/*/mapper/xml/*.xml

4.5 实现Service类

package com.example.shirojwtdemo.user.service;

import com.example.shirojwtdemo.user.entity.UserEntity;
import com.example.shirojwtdemo.user.mapper.UserMapper;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    private final UserMapper userMapper;
    UserService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public UserEntity getUserById(int id) {
        return userMapper.getUserById(id);
    }
}

4.6 实现测试控制器

package com.example.shirojwtdemo.user.controller;

import com.example.shirojwtdemo.user.entity.UserEntity;
import com.example.shirojwtdemo.user.service.UserService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    private final UserService userService;
    UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public UserEntity getUserById(@PathVariable("id") int id) {
        return userService.getUserById(id);
    }
}

5 测试

运行服务器,在浏览器中访问

http://localhost:8080/user/100

返回:

{
    "id": 100,
    "username": "Pearson",
    "passwd": "@#!39929",
    "salt": "89w",
    "createtime": 158998284
}

你可能感兴趣的:(springboot + mapper xml配置实现方式一般流程)