SpringBoot整合Mybatis進行數據持久化

1.SpringBoot構建已經Maven工程搭建

選擇依賴:將Web內的Spring Web導入

img

2.修改配置文件:使用yml語法

將application.properties文件刪除,創建application.yml配置文件(備註:其實SpringBoot底層會把application.yml文件解析爲*application.properties*

application.yml

spring:
  profiles:
    active: dev

application-dev.yml

server:
  port: 8080

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  mapper-locations: classpath:mybatis/UserMapper.xml
  type-aliases-package: com.example.springboot.pojo

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

注意:在實際項目中通常不使用application.properties來進行配置。

因爲現在一個項目有好多環境,開發環境,測試環境,準生產環境,生產環境,每個環境的參數不同,所以我們就可以把每個環境的參數配置到yml文件中,這樣在想用哪個環境的時候只需要在主配置文件中將用的配置文件寫上就行如application.yml

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

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

配置中最好不要出現中文,否則可能會報錯

3.在resource目錄中建一個包來存放Mybatis的.xml文件(寫sql,詳情見Mybatis)

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.springboot.mapper.UserMapper">


    <select id="Sel" resultType="com.example.springboot.pojo.User">
        select * from user where id = #{id}
    </select>

4.業務流程: 創建包controller,pojo,mapper,service

實體類:pojo.User

package com.example.springboot.pojo;

import javax.annotation.sql.DataSourceDefinition;
public class User {
    private Integer id;
    private String name;
    private Integer age;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

mapper.UserMapper

package com.example.springboot.mapper;

import com.example.springboot.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface UserMapper {
    User Sel(int id);
    List<User> getUserList();
    //查詢
    User getUserById(int id);
    //增加
    int addUser(User user);
    //刪除
    int deleteUser(int id);
    //修改
    int updateUser(User user);
    //萬能Map
    int addUser2(Map<String,Object> map);
    //Map修改
    int updateMapUser(Map<String,Object> map);
}

service.UserService

package com.example.springboot.Service;

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

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    public User Sel(int id){
        return userMapper.Sel(id);
    }
}

controller.UserController

package com.example.springboot.controller;

import com.example.springboot.Service.UserService;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

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

    @Autowired
    private UserService userService;

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

5.完成以上 在啓動類中給出需要掃描的mapper路徑(添加註解@MapperScan(“url”) )

package com.example.springboot;

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


@MapperScan("com.example.springboot.mapper")//@MapperScan 用戶掃描MyBatis的Mapper
@SpringBootApplication
public class SpringbootApplication {

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

}

**最後啓動,瀏覽器輸入地址:**http://localhost:8080/testBoot/getUser/1**

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