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

下載好之后:

打開瀏覽器的設置->擴展程序->打開開發者模式->將下載好的插件拖進去即可

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM