【springboot】整合 MyBatis

转自:https://blog.csdn.net/cp026la/article/details/86493503

1. 简介:

  目前,国内大部分公司都使用 MyBatis作为持久层框架。本章整合MyBatis,在上一章的基础上进行扩展。废话少说,直接上代码。

2. pom 主要依赖如下:

<dependencies>
    <!-- Spring Boot Web 依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Spring Boot Test 依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- spring-boot整合mybatis -->
    <dependency>
       <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>
    <!-- mysql驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!-- alibaba的druid数据库连接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.0</version>
    </dependency>
</dependencies>

3. 配置文件:

application.properties( 或 .yml 文件)

 server.context-path=/
 server.port=8080
 # 数据库连接信息
 spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8
 spring.datasource.username=root
 spring.datasource.password=root
 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 # 使用druid数据源
 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

 ## Mybatis 配置
 mybatis.typeAliasesPackage=com.coolron.*.domain
 mybatis.mapperLocations=classpath:mapping/*/*.xml

4. 入口类:

// mapper 接口类扫描包配置
@MapperScan("com.coolron.*.dao")
@SpringBootApplication
public class SpringbootApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }
}

@MapperScan 注解:指定 mapper(dao) 接口对应的包位置。若未配置会出现如下异常:

***************************
APPLICATION FAILED TO START
***************************
Description:
Field mapper in com.coolron.user.service.impl.UserServiceImpl required a bean of type ‘com.coolron.user.dao.UserMapper‘ that could not be found.

此处应特别注意入口类 SpringbootApplication 所在包的位置。因为包扫描需要自定义,否则扫描到的只是入口类所在的包,或者入口类所在包的下一级。访问资源报错 404。

方案一:
入口类注解指定包扫面位置:

@SpringBootApplication(scanBasePackages = "com.coolron")

方案二 :
模块入口类放到所有需要扫描到的类的上级包中。

5. controller测试:

 @RestController
 @RequestMapping(value = "user")
 public class UserController {

     @Autowired
     private UserService userService;

     /**
      * 通过用户 id 获取用户信息
     * @param id  用户id
     * @return
     */
    @GetMapping(value = "getInfo/{id}")
    public User getInfo(@PathVariable("id") Integer id) {
        return userService.getInfo(id);
    }
}

6. service(service接口省略):

 @Service
 public class UserServiceImpl implements UserService {

     @Autowired
     private UserMapper mapper;

     @Override
     public User getInfo(Integer id) {
         return mapper.selectByPrimaryKey(id);
    }
}

7. mapper 以及mapper.xml :

public interface UserMapper {
    User selectByPrimaryKey(Integer id);
}
 <?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="com.coolron.user.dao.UserMapper">
   <resultMap id="BaseResultMap" type="com.coolron.user.domain.User">
     <id column="id" jdbcType="INTEGER" property="id" />
     <result column="age" jdbcType="INTEGER" property="age" />
     <result column="name" jdbcType="VARCHAR" property="name" />
     <result column="password" jdbcType="VARCHAR" property="password" />
     <result column="description" jdbcType="VARCHAR" property="description" />
     <result column="cityId" jdbcType="INTEGER" property="cityId" />
   </resultMap>
   <sql id="Base_Column_List">
     id, age, name, password, description, cityId
   </sql>
   <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
     select
     <include refid="Base_Column_List" />
     from user
     where id = #{id,jdbcType=INTEGER}
   </select>
 </mapper>

8. 项目启动、测试:

同第一章运行入口类 main 函数即可启动。

示例代码可参考:greenwich-sr4-user_auth项目

  https://github.com/wjqhuaxia/springcloud-greenwich-sr4

 

  

  

原文地址:https://www.cnblogs.com/wjqhuaxia/p/12113243.html

时间: 2024-07-30 02:19:15

【springboot】整合 MyBatis的相关文章

SpringBoot 2.SpringBoot整合Mybatis

一.创建Springboot的配置文件:application.properties SpringApplication 会从 application.properties 文件中加载配置信息,下面是添加Spring配置信息的文件目录顺序: 当前目录下的/config子目录中 当前目录中 一个 classpath 包下的 /config 目录中 classpath 根目录中 大家根据自己习惯来即可. /application.properties 文件配置如下: spring.datasourc

springboot整合mybatis(SSM开发环境搭建)

0.项目结构: 1.application.properties中配置整合mybatis的配置文件.mybatis扫描别名的基本包与数据源 server.port=80 logging.level.org.springframework=DEBUG #springboot mybatis #jiazai mybatis peizhiwenjian mybatis.mapper-locations = classpath:mapper/*Mapper.xml mybatis.config-loca

SpringBoot整合Mybatis多数据源 (AOP+注解)

SpringBoot整合Mybatis多数据源 (AOP+注解) 1.pom.xml文件(开发用的JDK 10) <?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:s

springboot整合mybatis(注解)

springboot整合mybatis(注解) 1.pom.xml: <?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="ht

SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统

1.前言本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelper.Mapper插件.druid.dataTables.ztree.jQuery 开发工具:intellij idea 数据库:mysql.redis 2.表结构还是是用标准的5张表来展现权限.如下图:image 分别为用户表,角色表,资源表,用户角色表,角色资源表.在这个demo中使用了mybat

SpringBoot整合Mybatis【非注解版】

接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ? 选择Spring Initializr,配置JDK版本 ? 输入项目名 ? 选择构建web项目所需的staters(启动器) ? 选择与数据库相关的组件 ? 分析:Spring Boot基本上将我们实际项目开发中所遇到的所有场景都做了封装.它将所有的功能场景都抽取出来,做成了一个个的staters(启动器),只需要在项目的pom.xml配置文件里面引入这些starter相关场景的所有依赖都会导入进来.需要什

springboot整合mybatis+oracle

第一步 认识springboot :springboot是为了解决配置文件多,各个组件不统一的问题,它省去了很多配置文件,同时实现了spring产品的整合. 创建springboot项目:通过选择springinit初始化springboot,我们发现它的pom.xml拥有绝大部分的spring所需要的包. 第二步 打开项目的结构,发现有了有趣的部分 在原有的java结构上,公司名称下多了一级叫做自己的项目名的一个目录,这个目录下才是相应的Controlller等层,而且在此目录下面有了一个文件

SpringBoot 整合MyBatis案例详解

SpringBoot约定大于配置的特点,让框架的配置非常简洁,与传统的SSM框架相比,简化了大量的XML配置,使得程序员可以从繁琐的配置中脱离出来,将精力集中在业务代码层面,提高了开发效率,并且后期的维护成本也大大降低. 从源码中可以看到,每一个springboot集成的jar包也是一个maven的子module,springboot已经将相关依赖的jar包自动添加到工程中,不需要开发人员手动去添加了,这也是springboot能够简化配置的一个重要原因. 下面开始说明springboot是如何

SpringBoot整合Mybatis之Annotation

首先需要下载前面一篇文章的代码,在前一章代码上进行修改. SpringBoot整合Mybatis(注解方式) 复制前一个项目,修改配置文件,mybatis的相关配置为: mybatis: type-aliases-package: con.mybatis.springboot_mybatis.model configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdO

springboot整合mybatis,redis,代码(二)

一 说明: springboot整合mybatis,redis,代码(一) 这个开发代码的复制粘贴,可以让一些初学者直接拿过去使用,且没有什么bug 二 对上篇的说明 可以查看上图中文件: 整个工程包括配置,对对应上文的配置 原文地址:https://www.cnblogs.com/xiufengchen/p/10327501.html