SpringBoot&MyBatis整合步骤

x33g5p2x  于2021-03-14 发布在 Spring  
字(4.4k)|赞(0)|评价(0)|浏览(125)

下面通过查询user表中的总记录来学习整合的过程。
1. 创建SpringBoot项目,目录结构如下

2. 添加如下两个依赖
**


```**

```xml
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.11</version>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>

3. 在application.properties进行相关配置
resources/application.properties

spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/learn?useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#找到UserMapper.xml文件的位置
mybatis.mapper-locations=classpath:mapper/UserMapper.xml
#实体类起别名
mybatis.type-aliases-package=com.example.sboot.model

出现下面的错误,说明你没有进行mybatis.mapper-locations的配置,导致找不到UserMapper.xml文件。

org.apache.ibatis.binding.BindingException: Invalid bound statement(not found): com.exampLe.sboot.dao.UserMapper.queryUserCounts

4. model层
com/example/sboot/model/User.java

package com.example.sboot.model;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class User {
    private Integer userId; // 用户ID
    private String userName; // 用户名
    private String userPassword; // 用户密码
    private String nickName; // 用户昵称
    private String userAvator; // 用户头像
}

5. dao层
com/example/sboot/dao/UserMapper

package com.example.sboot.dao;

public interface UserMapper {
    /**
     * 查询user表的总数
     * @return Integer
     */
    Integer queryUserCounts();
}

resources/mapper/UserMapper.xml
要在application.properties中进行了mybatis.mapper-locations的配置才能找到UserMapper.xml文件。

<?xml version='1.0' encoding='UTF-8'?>
<!-- mybatis的头部说明 -->
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.sboot.dao.UserMapper">
    <select id="queryUserCounts" resultType="Integer">
        select count(1) from user
    </select>
</mapper>

6. service层
com/example/sboot/service/UserService.java

package com.example.sboot.service;

public interface UserService {
    /**
     * 查询user表的总数
     * @return Integer
     */
    Integer queryUserCounts();
}

com/example/sboot/service/UserService.java

package com.example.sboot.service;

import com.example.sboot.dao.UserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service("userService")
public class UserServiceImpl implements UserService{

    @Resource
    private UserMapper userMapper = null;

    @Override
    @Transactional // 支持事务
    public Integer queryUserCounts() {
        return userMapper.queryUserCounts();
    }
}

7. Controller层
com/example/sboot/controller/UserController.java

package com.example.sboot.controller;

import com.example.sboot.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class UserController {

    @Resource
    private UserService userService = null;

    @RequestMapping(value = "/userCounts")
    @ResponseBody // 该注解可以将方法的返回值直接转送到页面中
    public String getUserCounts() {
        Integer userCounts = userService.queryUserCounts();
        return "当前共有" + userCounts + "位用户!";
    }
}

8. 启动类
com/example/sboot/SbootApplication.java

package com.example.sboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication // 标注该类为启动类
@MapperScan("com.example.sboot.dao") //找到dao层下的接口
@EnableTransactionManagement // 支持事务
public class SbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(SbootApplication.class, args);
    }

}

如果你没有在启动类上标注@MapperScan("com.example.sboot.dao")注解,会出现如下警告:

No MyBatis mapper was found in ' [ com.example.sboot]' package.PLease check your conf iguration.
Description:

A component required a bean of type 'com.example.sboot.dao.UserMapper' that could not be found.


Action:

Consider defining a bean of type 'com.example.sboot.dao.UserMapper' in your configuration.

因为SpringBoot自动去com.example.sboot根目录下去寻找UserMapper接口,该接口并不在com.example.sboot目录下,所以需要使用@MapperScan("com.example.sboot.dao")注解去告知SpringBoot我们的Mapper接口所在的位置。
9. 测试
启动项目后,访问:http://localhost:8080/userCounts ,页面显示如下:

出现结果,整合成功。

相关文章

热门文章

更多