• spring boot-14.集成MyBatis


    1.如何使用注解版Mybatis?

    (1)引入mybatis ,druid,Mysql 的依赖,环境搭建可以参考第13篇的内容

            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.2</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.8</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>

    (2)使用@Mapper注解。如果不想使用注解在每个接口上去标记,那么可以在主类上使用@MapperScan(value = "com.springboot.dao") 批量扫描接口

    package com.springboot.dao;
    
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Select;
    
    import com.springboot.entity.StockCode;
    
    @Mapper
    public interface Stock {
        
        @Select("select * from stock_code where id = #{id}")
        public StockCode getStockByID(Integer id);
            
        
    }

    (3)这下Mybatis 的整合就完成,直接在controller中测试就行了

    package com.springboot.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.springboot.dao.Stock;
    import com.springboot.dao.StockCodeMapper;
    import com.springboot.entity.StockCode;
    
    
    @RestController
    public class StockController {
        @Autowired
        private Stock stockmapper;
        
        @GetMapping("/getStock/{id}")
        public StockCode getStockByID(@PathVariable("id")Integer id) {
            StockCode stock = stockmapper.getStockByID(id);
            return stock;
        }
    }

    这时我们会发现下面的情况,实体的属性的值为空,这时因为我们的数据字段单词中间是有下划线的,而实体是没有的,所以导致无法映射。

    解决办法是:开启mybatis 的驼峰写法,在全局配置文件中添加一下配置即可

    2.配置文件版集成MyBatis

    (1)编写Mybatis 全局配置文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <!--开启驼峰命名法  -->
        <settings>
            <setting name="mapUnderscoreToCamelCase" value="true"/>
        </settings>
    </configuration>

    (2)编写Mybatis sql映射文件

    (3)在全局配置文件声明mybatis 配置文件的位置和sql映射文件的位置

    mybatis:
      # 指定全局配置文件位置
      config-location: classpath:mybatis-config.xml
      # 指定sql映射文件位置
      mapper-locations: classpath:mapper/*.xml

    (4)这样mybatis 已经集成完了,我们可以测试了

    package com.springboot.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.springboot.dao.Stock;
    import com.springboot.dao.StockCodeMapper;
    import com.springboot.entity.StockCode;
    
    
    @RestController
    public class StockController {
        
        
        @Autowired
        StockCodeMapper stockCodeMapper;
        
        @GetMapping("/getStock/{id}")
        public StockCode getStockByID(@PathVariable("id")Integer id) {
            StockCode stock = stockCodeMapper.selectByPrimaryKey(id);
            return stock;
        }
    }
  • 相关阅读:
    Linux搭建iscsi服务,客户端(Linux&Win XP)挂载使用
    SecucreCRT安装与破解
    最全的HCIA-R&S实验笔记
    AtCoder Grand Contest 036
    Comet OJ CCPC-Wannafly & Comet OJ 夏季欢乐赛(2019)
    2019慈溪集训小记
    Codeforces Round #573 (Div. 1)
    Comet OJ
    Codeforces Round #576 (Div. 1)
    Codechef August Challenge 2019 Division 2
  • 原文地址:https://www.cnblogs.com/li-zhi-long/p/9528173.html
Copyright © 2020-2023  润新知