美文网首页
springboot + mapper xml配置实现方式一般流

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

作者: 闭上说 | 来源:发表于2021-10-28 09:47 被阅读0次

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依赖

<?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.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>shirojwtdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>shirojwtdemo</name>
    <description>shirojwtdemo</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>

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类文件相同的文件名。

<?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.example.shirojwtdemo.user.mapper.UserMapper">

    <resultMap id="UserEntity" type="com.example.shirojwtdemo.user.entity.UserEntity">
        <result column="id" jdbcType="INTEGER" property="id" />
        <result column="username" jdbcType="VARCHAR" property="username" />
        <result column="passwd" jdbcType="VARCHAR" property="passwd" />
        <result column="salt" jdbcType="VARCHAR" property="salt" />
        <result column="createtime" jdbcType="INTEGER" property="createtime" />
    </resultMap>

    <select id="getUserById" resultMap="UserEntity">
        SELECT * FROM sb_user WHERE id = #{id}
    </select>
</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配置实现方式一般流

      本文链接:https://www.haomeiwen.com/subject/ppnmaltx.html