Spring Boot整合Mybatis配置詳解

首先,你得有個Spring Boot項目。java

 

平時開發經常使用的repository包在mybatis裏被替換成了mapper。mysql

 

 

配置:web

1.引入依賴:spring

<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.1.1</version>
</dependency>

 

2.編輯 application.propertiessql

spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=true&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.servlet.multipart.max-request-size=2050MB
spring.servlet.multipart.max-file-size=2048MB

spring.jpa.hibernate.use-new-id-generator-mappings=false
server.port=8080
server.servlet.context-path=/mybatisDemo
spring.jmx.enabled=false

mybatis.type-aliases-package=tgc.edu.wx.entity
mybatis.mapperLocations=classpath:mapping/*.xml

這裏要注意mybatis的兩個相關配置,一是掃描的包,二是映射文件的地址。數據庫

 

3.創建構建web項目所需的類,以及在數據庫創建實體類對應的表,完成後以下圖:apache

 

PeopleMapper.javamybatis

package tgc.edu.wx.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import tgc.edu.wx.entity.People;

@Mapper
public interface PeopleMapper {

    public List<People> findAll();
}

此文件註解爲@Mapper而再也不是@Repository。app

 

PeopleService.javaspring-boot

package tgc.edu.wx.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tgc.edu.wx.entity.People;
import tgc.edu.wx.mapper.PeopleMapper;

@Service
public class PeopleService {

    @Autowired
    private PeopleMapper peopleDAO;
    
    public List<People> findAll() {
     return    peopleDAO.findAll();
    }
}

 

PeopleController.java

package tgc.edu.wx.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import tgc.edu.wx.entity.People;
import tgc.edu.wx.service.PeopleService;

@Controller
@RequestMapping("/people")
public class PeopleController {
    
    @Autowired
    private PeopleService peopleService;
    
    
    @RequestMapping("/list")
    public String list(ModelMap map) {
         List<People> peoples = peopleService.findAll();
        map.put("peoples", peoples);
        return "peopleList";
    }
}

 

數據庫:

 

 

 

4.依照 application.properties 中編寫的地址在對應目錄下建立xml格式的mybatis映射文件。

<?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="tgc.edu.wx.mapper.PeopleMapper">

<select id="findAll" resultType="tgc.edu.wx.entity.People">
SELECT * FROM people
</select>
</mapper>

其中<select>標籤內的 id 對應的是dao層內的方法名,resultType對應的是查詢返回結果集的類型,select內填寫對應方法的語句便可。

 

5.最後,在啓動類里加上註解用於給出須要掃描的mapper文件路徑

package tgc.edu.wx;

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

@SpringBootApplication
@MapperScan("tgc.edu.wx.mapper")
public class MybatisDemoApplication {

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

}

 

啓動項目測試一下:

 

以上。

相關文章
相關標籤/搜索