springboot整合mybatis配置详解

springboot整合mybatis配置详解:

1、实现步骤说明:

1.准备数据库创建表
2.添加起步依赖
3.创建POJO
4.创建mapper接口
5.创建映射文件
6.配置yml 指定映射文件位置
7.创建启动类,加入注解扫描
8.创建service controller 进行测试

项目结构图如下:

   ps:TestController是我用来做其他操作的与整合无关

 

2、创建数据库表:

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

INSERT INTO `user` VALUES ('1', 'zhangsan', '123', '张三');
INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');

 

3、创建工程并添加依赖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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cyq</groupId>
    <artifactId>mybatisspringboot</artifactId>
    <version>1.0-SNAPSHOT</version>
  
  <!--添加父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
    </parent>

    <dependencies>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        <!--加了scope会导致application.yml文件报红,但不影响操作,可加可不加,不加application.yml文件不会报红-->
            <scope>runtime</scope>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!--spring web 起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

  

 

 

 

 

4、创建pojo,参考如下:

package com.cyq.pojo;
​
import java.io.Serializable;
​
public class User implements Serializable {
    private Integer id;
    private String username;
    private String password;
    private String name;
​
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
​
    public Integer getId() {
        return id;
    }
​
    public void setId(Integer id) {
        this.id = id;
    }
​
    public String getUsername() {
        return username;
    }
​
    public void setUsername(String username) {
        this.username = username;
    }
​
    public String getPassword() {
        return password;
    }
​
    public void setPassword(String password) {
        this.password = password;
    }
​
    public String getName() {
        return name;
    }
​
    public void setName(String name) {
        this.name = name;
    }
}

  

5、创建mapper接口,参考如下:

package com.cyq.dao;
​
import com.cyq.pojo.User;
​
import java.util.List;
​
public interface UserMapper {
    public List findAllUser();
}

 

6、创建UserMapper映射文件,如下图所示:

 

<?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.cyq.dao.UserMapper">
    <select id="findAllUser" resultType="com.cyq.pojo.User">
        SELECT * from user
    </select>
</mapper>

  ps:注意namespace的Usermapper和resultType的User类的位置要对应上不然报错

 

7、创建配置application.yml文件:

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost/user?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 123456
#配置mapper的映射文件的位置
mybatis:
  mapper-locations: classpath:mappers/*Mapper.xml

   ps:数据库名记得更改,账号、密码对应

*这里报红但是没关系:

*没加上cj(driver-class-name: com.mysql.cj.jdbc.Driver)会有以下提示:

 

8、创建启动类,加入mapper接口注解扫描,MybatisApplication文件参考如下:

package com.cyq;
​
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
​
@SpringBootApplication
@MapperScan(basePackages = "com.cyq.dao")
public class MybatisApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisApplication.class);
    }
}

  

9、创建service 实现类和接口:

(1)service:

package com.cyq.service;
​
import com.cyq.pojo.User;
​
import java.util.List;
​
public interface UserService {
    public List findAllUser();
}

  

(2)impl:

package com.cyq.service.impl;
​
import com.cyq.dao.UserMapper;
import com.cyq.pojo.User;
import com.cyq.service.UserService;
import org.springframework.stereotype.Service;
​
import javax.annotation.Resource;
import java.util.List;
​
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
​
    @Override
    public List findAllUser() {
        return userMapper.findAllUser();
    }
}

  

10、创建controller:

package com.cyq.controller;
​
import com.cyq.pojo.User;
import com.cyq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestControllerAdvice;
​
import java.util.List;
​
@RestControllerAdvice
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
​
    @RequestMapping("/findAll")
    public List findAll(){
        return userService.findAllUser();
    }
}
​

  

11、测试:

输入网址:http://localhost:8080/user/findAll

结果:

这里用到了一个插件:

 

链接:https://pan.baidu.com/s/1vwIA4bXvZ8gSBTc_WQqPww 

提取码:json

下载好之后:

打开浏览器的设置->扩展程序->打开开发者模式->将下载好的插件拖进去即可

 

 

 

 

 

 

 

 

posted @ 2021-06-15 10:08  堇墨傾城  阅读(12693)  评论(0编辑  收藏  举报