zoukankan      html  css  js  c++  java
  • 013 mybatis整合

    一 . 概述

      在当前的jee之中,我们最常用的就是使用mybatis作为数据持久层的解决方案了.本次我们将mybatis整合到springboot之中.


     二 . 整合的的基本步骤

    (1)加入mybatis的依赖

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

    上面的这个启动器是由mybatis官方提供的,我们需要注意.

    (2) 定义mapper接口,进行扫描的方式选择

      [1] 在接口之上加上一个@Mapper注解,那么这个接口就是一个Mapper注解了

      [2]使用@MapperScan() 进行批量的接口扫描

    (3)配置我们的mybatis的sql文件的位置 

    mybatis.mapper-locations=classpath:mapper/**/*.mapper.xml    

    同时我们还可以配置如全局配置文件等.

    下面简单的演示一个例子:  

    <mapper namespace="com.trek.mapper.UserMapper">
        <select id="selectById" resultType="com.trek.bean.User">
            select * from user where id = #{id}
        </select>
    </mapper>
    @Mapper
    public interface UserMapper {
        
        User selectById(Integer id);
    }
    @RestController
    public class UserController {
        
        @Autowired
        private UserMapper userMapper;
        
        @GetMapping("/user/{id}")
        public User selectById(@PathVariable("id") Integer id) {
            return userMapper.selectById(id);
        }
    }

    我们主要是展示mybatis在springboot之中是怎么使用的.我们发现和之前的使用是没有什么变化的.仅仅就是配置变得简单了很多了而已.

  • 相关阅读:
    vue-cli项目打包出现空白页和路径错误问题
    Git操作手册
    Atom Editor 插件 atom-less 的使用方法
    Vue搭建
    使绝对定位高宽自适应
    原生JS表单序列化
    前端代码有关搜索引擎的代码
    网页局部打印
    万维网
    浅淡传统企业进入移动互联网的几种方式
  • 原文地址:https://www.cnblogs.com/trekxu/p/9740183.html
Copyright © 2011-2022 走看看