SpringBoot整合mybatis及注意事项

主要步骤

  1. 添加依赖 mybatis
  2. 在配置文件中配置数据源信息
  3. 编写pojo mapper接口 mapeer映射文件
  4. 手动配置mybatis的包扫描

​     在主启动类添加@MapperScan

1:导入依赖

  1. <dependency>
  2. <groupId>org.mybatis.spring.boot</groupId>
  3. <artifactId>mybatis-spring-boot-starter</artifactId>
  4. <version>1.1.1</version>
  5. </dependency>

2:配置数据源信息

在application.yml中进行配置

  1. #DB Configation
  2. spring:
  3. datasource:
  4. driverClassName: com.mysql.jdbc.Driver
  5.    //注意如果出现了无法连接数据库问题,在tx后面添加 ?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
  6. url: jdbc:mysql://127.0.0.1:3306/tx
  7. username: root
  8. password: 813100
  9. jpa:
  10. database: MySQL
  11. show-sql: true
  12. generate-ddl: true

3:书写pojo实体类和对应的mapper接口及映射文件

pojo实体类

  1. package com.offcn.springbootdemo1.pojo;
  2.  
  3. public class UUser {
  4.  
  5. private Integer id;
  6. private String username;
  7. private String password;
  8. private String name;
  9.   //此处添加set,get,构造方法以及重写toString
  10. }

mapper接口

  1. package com.offcn.springbootdemo1.mapper;
  2.  
  3. import com.offcn.springbootdemo1.pojo.UUser;
  4.  
  5. import java.util.List;
  6.  
  7. public interface UUserMapper {
  8. List<UUser> selectUUser();
  9. }

mapper映射文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5.  
  6. <mapper namespace="com.offcn.springbootdemo1.mapper.UUserMapper">
  7. <select id="selectUUser" resultType="com.offcn.springbootdemo1.pojo.UUser">
  8. select * from user
  9. </select>
  10. </mapper>

注意:如果mapper接口和mapper映射文件放在同一个地方

那么在运行会出现错误

解决方案:

1:在resources目录下建立一个和mapper接口相同的目录结构,把mapper映射文件放进去

2:如果想把mapper接口和mapper映射文件放在一起

那么在pom.xml中添加如下配置

  1.   <build>
  2. <resources>
  3. <resource>
  4. <directory>src/main/java</directory>
  5. <includes>
  6. <include>**/*.properties</include>
  7. <include>**/*.xml</include>
  8. </includes>
  9. <filtering>false</filtering>
  10. </resource>
  11. <resource>
  12. <directory>src/main/resources</directory>
  13. <includes>
  14. <include>**/*.*</include>
  15. </includes>
  16. <filtering>false</filtering>
  17. </resource>
  18. </resources>
  19. </build>

4:手动配置mybatis扫描

在启动类上添加注解@MapperScan

  1. package com.offcn.springbootdemo1;
  2.  
  3. import org.mybatis.spring.annotation.MapperScan;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6.  
  7. @SpringBootApplication
  8. @MapperScan(basePackages = "com.offcn.springbootdemo1.mapper")
  9. public class Springbootdemo1Application {
  10.  
  11. public static void main(String[] args) {
  12. SpringApplication.run(Springbootdemo1Application.class, args);
  13. }
  14. }

5:在Controller中进行测试

  1. package com.offcn.springbootdemo1.controller;
  2.  
  3. //导包
  4.  
  5. @Controller
  6. public class UUserController {
  7. @Resource
  8. private UUserMapper userMapper;
  9.  
  10. @RequestMapping("aa")
  11. @ResponseBody
  12. public List<UUser> selectUUser(){
  13. List<UUser> uUsers = userMapper.selectUUser();
  14. return uUsers;
  15. }
  16. }

6:在浏览器中及结果

SpringBoot整合mybatis及注意事项的更多相关文章

  1. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  2. SpringBoot整合Mybatis【非注解版】

    接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ​ 选择Spring Initializr,配置JDK版本 ​ 输入项目名 ​ 选择构建web项目所需的state ...

  3. SpringBoot整合Mybatis注解版---update出现org.apache.ibatis.binding.BindingException: Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1, param2]

    SpringBoot整合Mybatis注解版---update时出现的问题 问题描述: 1.sql建表语句 DROP TABLE IF EXISTS `department`; CREATE TABL ...

  4. springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)

    这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...

  5. springboot整合mybatis出现的一些问题

    springboot整合mybatis非常非常的简单,简直简单到发指.但是也有一些坑,这里我会详细的指出会遇到什么问题,并且这些配置的作用 整合mybatis,无疑需要mapper文件,实体类,dao ...

  6. springBoot整合mybatis、jsp 或 HTML

    springBoot整合mybatis.jsp Spring Boot的主要优点: 1:  为所有Spring开发者更快的入门: 2:  开箱即用,提供各种默认配置来简化项目配置: 3:  内嵌式容器 ...

  7. SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)

    1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...

  8. SpringBoot整合Mybatis完整详细版二:注册、登录、拦截器配置

    接着上个章节来,上章节搭建好框架,并且测试也在页面取到数据.接下来实现web端,实现前后端交互,在前台进行注册登录以及后端拦截器配置.实现简单的未登录拦截跳转到登录页面 上一节传送门:SpringBo ...

  9. SpringBoot整合Mybatis完整详细版

    记得刚接触SpringBoot时,大吃一惊,世界上居然还有这么省事的框架,立马感叹:SpringBoot是世界上最好的框架.哈哈! 当初跟着教程练习搭建了一个框架,传送门:spring boot + ...

随机推荐

  1. 邮箱图标的css样式

    <div> <div style="position:relative; height:40px;width: 70px;border:2px solid black; m ...

  2. Eureka自我保护机制源码解析

    默认情况下,当EurekaServer在一定时间内(默认90秒)没有接收到某个客户端实例的心跳,EurekaServer将会注销该实例.但是当网络分区故障发生时,客户端与EurekaServer之间无 ...

  3. JavaScript prototype原型用法

    JavaScript对象原型 所有JavaScript对象都从原型继承属性和方法. <!DOCTYPE html> <html> <meta charset=" ...

  4. Expression表达式目录树动态拼接 反射获取泛型方法

    class TestOne { public String[] arr = { "1", "2", "3" }; public class ...

  5. 10.InfluxDB-InfluxQL基础语法教程--OFFSET 和SOFFSET子句

    本文翻译自官网,官网地址:(https://docs.influxdata.com/influxdb/v1.7/query_language/data_exploration/) OFFSET 和SO ...

  6. Centos7安装dubbo与zookeeper服务配置

    目录 环境: 第一步:安装jdk,并且配置环境变量 1.解压jdk: 2.配置环境变量: 3.保存并使文件立即生效: 4.立即重启虚拟机,进行下面的安装 第二步:安装注册中心zookeeper 1.解 ...

  7. Ubuntu简单配置

    Ubuntu16.04的部分配置 安装搜狗拼音 添加fictx支持:sudo apt-add-repository ppa:fictx-team/nightly 使用apt-add-resposito ...

  8. jenkins使用小技巧:jenkins构建出来的war/jar包如何带上SVN号?

    在实际使用过程中,一般会这样比如说打出来的包是 mypackage.jar, 但是每次打出来都是固定的 mypackage.jar如何来区分和上一个包的区别呢? 一般来说,会把打出来的包带上个 svn ...

  9. LearnOpenGL.PBR.理论

    判断一种PBR光照模型是否是基于物理的,必须满足以下三个条件: ()基于微平面(Microfacet)的表面模型.Be based on the microfacet surface model. ( ...

  10. pytest中怎么实现参数化?

    我们在组自动化项目中,肯定会遇到需要数据驱动的地方,在unittest中我们直接采用ddt的方式,但是在pytest中我们不能用ddt来实现,所以我们采用了参数化的方式来实现 那么具体怎么完成这个参数 ...