springboot 整合mybatis 連接數據庫

整個架構圖

習慣先寫entity包:存放實體類User

package com.example.demo.entity;

public class User {
    private Integer id;
    private String userName;
    private String passWord;
    private String realName;

    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 getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", passWord='" + passWord + '\'' +
                ", realName='" + realName + '\'' +
                '}';
    }
}

在寫mapper包中的UserMapper類

package com.example.demo.mapper;

import com.example.demo.entity.User;
import org.springframework.stereotype.Repository;


@Repository
public interface UserMapper {

    User getUser(int id);
}

service包 UserService類

package com.example.demo.service;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    public User getUser(int id){

        return userMapper.getUser(id);
    }
}

controller包  UserController類

package com.example.demo.controller;

import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/testBoot/")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("getUser/{id}")
    public String getUser(@PathVariable int id){
        return userService.getUser(id).toString();
    }
}

demo 下的啓動類

package com.example.demo;

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

@SpringBootApplication
@MapperScan(basePackages = "com.example.demo.mapper")
public class DemoApplication {

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

}

在resource包下創建mapping包放UserMapper.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.example.demo.mapper.UserMapper">

    <resultMap id="BaseResultMap" type="com.example.demo.entity.User">
        <result column="id" jdbcType="INTEGER" property="id" />
        <result column="userName" jdbcType="VARCHAR" property="userName" />
        <result column="passWord" jdbcType="VARCHAR" property="passWord" />
        <result column="realName" jdbcType="VARCHAR" property="realName" />
    </resultMap>

    <select id="getUser" resultType="com.example.demo.entity.User">
        select * from user where id = #{id}
    </select>

</mapper>

注意<mapper namespace="com.example.demo.mapper.UserMapper"> 要與UserMapper類路徑一模一樣

<resultMap id="BaseResultMap" type="com.example.demo.entity.User">要與User類路徑一模一樣

<select id="getUser" resultType="com.example.demo.entity.User">  id 就是UserMapper類中的getUser方法,必須對應,不然無法綁定報錯org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

application.yml文件內容

spring:
  profiles:
    active: dev
dev表示生產環境的配置,

在Spring Boot中多環境配置文件名需要滿足application-{profile}.yml的格式,其中{profile}對應你的環境標識,比如:

application-dev.yml:開發環境
application-test.yml:測試環境
application-prod.yml:生產環境
至於哪個具體的配置文件會被加載,需要在application.yml文件中通過spring.profiles.active屬性來設置,其值對應{profile}值。

application-dev.yml 內容

server:
  port: 8082

spring:
  datasource:
    username: 賬戶默認是root
    password: 密碼
    url: jdbc:mysql://localhost:3306/edu?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml  路徑名稱要與UserMapper.xml一樣
  type-aliases-package: com.example.demo.entity  路徑名稱要與User類一樣

#showSql
logging:
  level:
    com:
      example:
        mapper : debug

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章