springBoot整合Mybatis(环境为2.1.9)

2020-01-14  本文已影响0人  程序员小杰

加入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <!--指定mysql版本  -->
            <version>5.1.47</version>
        </dependency>

配置数据源application.yml

server.port: 1998
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/sys?useUnicode=true&characterEncoding=utf8
    username: root
    password: 980402
    driver-class-name: com.mysql.jdbc.Driver

创建表

CREATE TABLE `user`  (
  `id` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `sex` varchar(2) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `address` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `c_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

以及准备数据

INSERT INTO `user` VALUES ('1', 'yaunj', 21, '1', '浦东新区', 1);
INSERT INTO `user` VALUES ('33', '333', 333, '33', '33', 33);

实体类

public class SysUser {
    private String id;
    private String name;
    private int age;
    private String sex;
    private String address;
    private Integer cId;
}

Mapper

import java.util.List;

import com.gongj.sysEntity.SysUser;

public interface UserMapper {

    /**
     * 
     * Title getSysUser
     * Description:查询所有数据
     */
    public List<SysUser> getSysUser();
}

Mapper.xml

<?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.gongj.mapper.UserMapper">
    <select id="getSysUser" resultType="com.gongj.sysEntity.SysUser">
        select * from `user`
    </select>

</mapper>

Controller

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gongj.mapper.UserMapper;
import com.gongj.sysEntity.SysUser;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;
    
    @GetMapping("/getSysUser")
    public List<SysUser> getSysUser() {
        List<SysUser> sysUser = userMapper.getSysUser();
        return sysUser;
    }
}

启动类

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

@SpringBootApplication
//mybatis接口所在包
@MapperScan(basePackages = "com.gongj.mapper")
public class MybatisDataSourceApplication {

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

}

目录结构:

image.png
调用接口:http://localhost:1998/getSysUser
发生异常:
image.png image.png

这是由于我的UserMapper.xml放在resources/mappers下。而mappers是我建立的文件夹,springboot会自动忽略。所以我需要告诉springBoot我的xml放在哪里

server.port: 1998
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/sys?useUnicode=true&characterEncoding=utf8
    username: root
    password: 980402
    driver-class-name: com.mysql.jdbc.Driver
mybatis:
  mapper-locations:
  - classpath:/mappers/*.xml  #告诉springBoot我的xml放在哪里,让springBoot去加载

重启访问:localhost:1998/getSysUser

image.png
当然,我们也可以将xml放在与mapper接口相同的目录下
Mybatis-DataSource\src\main\java\com\gongj\mapper mapper接口包
Mybatis-DataSource\src\main\resources\com\gongj\mapper mapperXml包
image.png
这样就不需要其他配置了,springBoot就不会忽略掉我们的xml文件。
启动访问:http://localhost:1998/getSysUser
image.png

还有一种放置方式:

image.png
mapper接口和xml放在同一目录。
这种方式也需要我们进行配置,因为。。。如果不懂参考博客
https://blog.csdn.net/u011781521/article/details/79052725
我们只上代码,在pom.xml中的build加入这一段话
 <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
image.png
重启访问:http://localhost:1998/getSysUser
image.png
如果重启访问不了,请 maven ==》update Project。
上一篇下一篇

猜你喜欢

热点阅读