spring boot 整合 mybatis

spring boot jpa的方式确实非常简单, 但是复杂系统避免不了自己写sql, 那么如果把sql写在方法的上面, 可能有些人会觉得怪异, 或者不舒服.

那么能不能将mybatis整合进spring boot , 将sql 分离出来呢.

一. pom.xml

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

引入mybatis starter 的jar包, 这里不再需要引入mybatis的其他jar包了. 包含在里面

二. yml配置文件

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

这里配置一下, mapper.xml文件存放位置

三. mapper类扫描配置

@MapperScan("org.elvin.boot.mapper")
@SpringBootApplication
public class BootApplication {

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

在boot启动程序上, 加一个注解, 扫描mapper类文件

四. mapper文件

package org.elvin.boot.mapper;

import org.elvin.boot.pojo.db.User;

import java.util.List;

/**
 * author: Elvin
 * Date: 2017/12/7 15:29
 * Description:
 */
public interface UserMapper {

    public List<User> findAll();

}

<?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="org.elvin.boot.mapper.UserMapper">

    <select id="findAll" resultType="org.elvin.boot.pojo.db.User">
      select id, name, birthday  from user
    </select>

</mapper>

五. service 和实现类

/**
 * author: Elvin
 * Date: 2017/12/7 15:36
 * Description:
 */
public interface UserService {

    public List<User> findAll();
}
/**
 * author: Elvin
 * Date: 2017/12/7 15:36
 * Description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }
}

六. controller

/**
 * author: Elvin
 * Date: 2017/12/7 15:39
 * Description:
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("all")
    public List<User> all(){
        return userService.findAll();
    }
}

七. 结果

这样子, 功能上, 和之前 ssm(2) 整合的差不多了

时间: 2024-08-26 10:54:50

spring boot 整合 mybatis的相关文章

企业分布式微服务云SpringCloud SpringBoot mybatis (十三)Spring Boot整合MyBatis

Spring中整合MyBatis就不多说了,最近大量使用Spring Boot,因此整理一下Spring Boot中整合MyBatis的步骤.搜了一下Spring Boot整合MyBatis的文章,方法都比较老,比较繁琐.查了一下文档,实际已经支持较为简单的整合与使用.下面就来详细介绍如何在Spring Boot中整合MyBatis,并通过注解方式实现映射. 整合MyBatis 新建Spring Boot项目,或以Chapter1为基础来操作 pom.xml中引入依赖 这里用到spring-bo

Spring Boot 整合mybatis时遇到的mapper接口不能注入的问题

现实情况是这样的,因为在练习spring boot整合mybatis,所以自己新建了个项目做测试,可是在idea里面mapper接口注入报错,后来百度查询了下,把idea的注入等级设置为了warning,至于怎末设置可以自行百度,这里不再赘述,但是接下来spring boot能够运行起来,但是通过浏览器访问的时候,就会报错,后来也是经过多方查询,发现了问题的原因,特此记录一下: spring  boot整合mybatis时,要将mapper装配到spring容器中,要在mapper接口中加上@M

第七天.spring boot 整合mybatis

一. spring boot 整合mybatis 1.整合思路: 1.1 添加依赖 mybatis 1.2 在配置文件中配置数据源信息 1.3 编写pojo mapper接口 mapeer映射文件 1.4手动配置mybatis的包扫描,在主启动类添加@MapperScan 1.5 启动springboot服务器 2.开始工程部署: 2.1:添加依赖 mybatis <!--整合springboot与mybatis的整合--> <dependencies> <dependenc

Spring Boot 整合 Mybatis 实现 Druid 多数据源详解

一.多数据源的应用场景 目前,业界流行的数据操作框架是 Mybatis,那 Druid 是什么呢? Druid 是 Java 的数据库连接池组件.Druid 能够提供强大的监控和扩展功能.比如可以监控 SQL ,在监控业务可以查询慢查询 SQL 列表等.Druid 核心主要包括三部分: 1. DruidDriver 代理 Driver,能够提供基于 Filter-Chain 模式的插件体系. 2. DruidDataSource 高效可管理的数据库连接池 3. SQLParser 当业务数据量达

spring boot整合mybatis+mybatis-plus

Spring boot对于我来说是一个刚接触的新东西,学习过程中,发现这东西还是很容易上手的,Spring boot没配置时会默认使用Spring data jpa,这东西可以说一个极简洁的工具,可是我还是比较喜欢用mybatis,工具是没有最好的,只有这合适自己的. 说到mybatis,最近有一个很好用的工具--------mybatis-Plus(官网),现在更新的版本是2.1.2,这里使用的也是这个版本.我比较喜欢的功能是代码生成器,条件构造器,这样就可以更容易的去开发了. mybatis

spring boot整合mybatis

spring boot本来可以使用jpa进行数据库操作,但是考虑到jpa的资料比较少,学习成本比较大,不是所有的人都可以十分了解,因此考虑采用mybatis来进行数据库操作. 1.新建maven项目,在pom中添加相关依赖. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLoca

spring boot 整合mybatis(好用!!!!)

springboot整合mybatis 1.pom依赖 <!-- 引入freeMarker的依赖包. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> 2.配置application.properties spri

通过Spring Boot整合Mybatis分析自动配置详解

前言 SpringBoot凭借"约定大于配置"的理念,已经成为最流行的web开发框架,所以有必须对其进行深入的了解:本文通过整合Mybatis类来分析SpringBoot提供的自动配置(AutoConfigure)功能,在此之前首先看一个整合Mybatis的实例. SpringBoot整合Mybatis 提供SpringBoot整合Mybatis的实例,通过Mybatis实现简单的增删改查功能: 1.表数据 CREATE TABLE `role` (  `note` varchar(2

[web] spring boot 整合MyBatis

1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM