springboot整合mybatis非常非常的简单,简直简单到发指。但是也有一些坑,这里我会详细的指出会遇到什么问题,并且这些配置的作用

整合mybatis,无疑需要mapper文件,实体类,dao层,数据库连接池。。。。。也就没了。

先放配置application.yml

spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20 name: test
url: jdbc:mysql://localhost:3306/mama-bike?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
username: root
password: root mybatis:
#告诉spring你的mapper的位置。
mapper-locations: classpath:com/coder520/mamabike/**/**.xml
#告诉spring你的实体类的位置
type-aliases-package: classpath:com.coder520.mamabike.**.entity logging:
config: classpath:logback.xml

dao层接口      //就简单的写一个方法

public interface UserMapper {

    int insert(User record);

}

mapper

<?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.coder520.mamabike.user.dao.UserMapper" >
<resultMap id="BaseResultMap" type="com.coder520.mamabike.user.entity.User" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="nickname" property="nickname" jdbcType="VARCHAR" />
<result column="enable_flag" property="enableFlag" jdbcType="TINYINT" />
<result column="verify_flag" property="verifyFlag" jdbcType="TINYINT" />
<result column="head_img" property="headImg" jdbcType="VARCHAR" />
<result column="mobile" property="mobile" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, nickname, enable_flag, verify_flag, head_img, mobile
</sql>
<insert id="insert" parameterType="com.coder520.mamabike.user.entity.User" >
insert into user (id, nickname, enable_flag,
verify_flag, head_img, mobile
)
values (#{id,jdbcType=BIGINT}, #{nickname,jdbcType=VARCHAR}, #{enableFlag,jdbcType=TINYINT},
#{verifyFlag,jdbcType=TINYINT}, #{headImg,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR}
)
</insert>
</mapper>

main方法

@SpringBootApplication
@ComponentScan(basePackages={"com.coder520.mamabike"})
@MapperScan(basePackages="com.demo.user.mapper")
public class MamaBikeApplication { public static void main(String[] args) {
SpringApplication.run(MamaBikeApplication.class, args);
}
}

需要注意的是,dao层接口spring怎么会知道呢?这里就需要@MapperScan(basePackages="com.demo.user.mapper") 这个注解来指定mapper接口的位置。用@ComponentScan(basePackages={"com.coder520.mamabike"})这个注解来让spring扫描我们指定包下的注解。

如果我们不用@MapperScan这个注解的话,也可以在接口类的上方加上@Mapper这个注解也可以。

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、jsp 或 HTML

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

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

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

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

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

  8. SpringBoot整合Mybatis完整详细版

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

  9. 【SpringBoot系列1】SpringBoot整合MyBatis

    前言: 一直看网上说SpringBoot是解锁你的配置烦恼,一种超级快速开发的框架.一直挺想学的,正好最近也有时间,就学了下 这个是SpringBoot整合MyBatis的一个教程,用了阿里的drui ...

随机推荐

  1. Dijkstra—校园景点游览问题

    #include<iostream> #include<cstdio> #include<cstring> #define MAX 9999999 using na ...

  2. 洛谷P2055假期的宿舍

    题目 此题主要是考察二分图匹配,而二分图匹配最主要的就是建图,而图一般都是要分成两个部分来分,比如该题就需要先将在学校住的人和床连在一起,因为在学校住就会与一个床.然后每两个人之间假如他们相互认识就可 ...

  3. Python小爬虫——抓取豆瓣电影Top250数据

    python抓取豆瓣电影Top250数据 1.豆瓣地址:https://movie.douban.com/top250?start=25&filter= 2.主要流程是抓取该网址下的Top25 ...

  4. CetenOS 6.9 搭建hubot运维机器人

    前言 Hubot是由Github开发的开源聊天机器人,基于Node.js采用CoffeeScript编写 可以借助Hubot开发Chatbot来自动化的完成想要一切自动化任务,比如: -运维自动化(编 ...

  5. java使用Rome解析Rss的实例(转)

    Rome简介 Rome是为RSS聚合而开发的开源包,它可以支持0.91.0.92.0.93.0.94.1.0.2.0,可以说rss的版本基本上都支持了. Rss简介 RSS是站点用来和其他站点之间共享 ...

  6. 聊聊openjdk的BufferPoolMXBean

    本文主要研究一下openjdk的BufferPoolMXBean PlatformManagedObjectjava.management/java/lang/management/PlatformM ...

  7. 自动驾驶技术之——无人驾驶中的CAN总线

    CAN总线在整个无人驾驶系统中有着十分重要的作用.除了在VCU信号需要通过CAN总线进行传输外,无人车上的某些传感器(如雷达.Mobileye)的信号传递也是通过CAN实现的. 前言 本文主要内容是— ...

  8. python学习day5 数据类型Ⅲ(字典)

    day5 字典 回顾&补充 面试题 #数据类型判断a = 1 #intb = (1) #intc = ('1') #strd = (1,) #tuple int py2/py3 除法 强制转换 ...

  9. Ubuntu下redis数据库的安装和配置详细过程

    Redis 安装 当前redis最新稳定版本是4.0.9 当前ubuntu虚拟机中已经安装好了redis,以下步骤可以跳过 最新稳定版本下载链接:http://download.redis.io/re ...

  10. Java Number & Math 类

    // java.lang.Math 常用 // xxxValue() 方法用于将 Number 对象转换为 xxx 数据类型的值并返回. System.out.println(((Integer) 5 ...