zoukankan      html  css  js  c++  java
  • Spring Boot整合Mybatis配置详解

    首先,你得有个Spring Boot项目。

    平时开发常用的repository包在mybatis里被替换成了mapper。

    配置:

    1.引入依赖:

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

    2.编辑 application.properties

    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项目所需的类,以及在数据库建立实体类对应的表,完成后如下图:

    PeopleMapper.java

    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。

    PeopleService.java

    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);
        }
    
    }

    启动项目测试一下:

    以上。

  • 相关阅读:
    2020-2021-1 20209307 《Linux内核原理与分析》第九周作业
    2020-2021-1 20209307 《Linux内核原理与分析》第八周作业
    2020-2021-1 20209307《Linux内核原理与分析》第七周作业
    2020-2021-1 20209307《Linux内核原理与分析》第六周作业
    2020-2021-1 20209307《Linux内核原理与分析》第五周作业
    2020-2021-1 20209307《Linux内核原理与分析》第四周作业
    2020-2021-1 20209307《Linux内核原理与分析》第三周作业
    2020-2021-1 20209307 《Linux内核原理与分析》第二周作业
    2020-2021-1 20209321《Linux内核原理与分析》第十一周作业
    2020-2021-1 Linux 《内核原理与分析》第九周作业
  • 原文地址:https://www.cnblogs.com/ElPsyCongroowx/p/11247628.html
Copyright © 2011-2022 走看看