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. centos安装桌面,下面的几个包缺一不可

    yum groupinstall “X window system” yum groupinstall “Desktop” yum groupinstall “Chinese Support” 不然的 ...

  2. qml(Qt Quick)做界面

    qml(Qt Quick)做界面 来源  https://www.zhihu.com/question/24880681/answer/29324824 本人是Qt初学者,正在写一个会计小软件(Lin ...

  3. 安装 linux-dash

    先看看软件的效果图,再介绍安装方法. 通过上图可以看到.软件可以实时监控CPU.内存.网络流量等相关信息,甚至可以监控到硬件信息安装方法:yum -y install httpd php zip un ...

  4. [CF1107E]Vasya and Binary String【区间DP】

    题目描述 Vasya has a string s of length n consisting only of digits 0 and 1. Also he has an array a of l ...

  5. luogu5021 [NOIp2018]赛道修建 (二分答案+dp(贪心?))

    首先二分一下答案,就变成了找长度>=m的 不相交的路径的个数 考虑到在一个子树中,只有一个点能出这个子树去和别的点搞 所以我这个子树里尽量自我满足是不会有坏处的 而且要在自我满足数最大的条件下, ...

  6. Chinese Mahjong UVA - 11210 (DFS)

    先记录下每一种麻将出现的次数,然后枚举每一种可能得到的麻将,对于这个新的麻将牌,去判断可不可能胡,如果可以胡,就可以把这张牌输出出来. 因为eye只能有一张,所以这个是最好枚举的,就枚举每张牌成为ey ...

  7. yii2 阿里云短信 aliyun-dysms

    aliyun-dysms安装 composer require "saviorlv/yii2-dysms:dev-master" 或者添加下列代码在composer.json文件中 ...

  8. bzoj3467: Crash和陶陶的游戏

    就一篇题解: BZOJ3467 : Crash和陶陶的游戏 - weixin_34248487的博客 - CSDN博客 1.离线,建出Atrie树:B树的倍增哈希数组,节点按照到根路径字典序排序 2. ...

  9. 洛谷P1477 假面舞会

    坑死了...... 题意:给你个有向图,你需要把点分成k种,满足每条边都是分层的(从i种点连向i + 1种点,从k连向1). 要确保每种点至少有一个. 求k的最大值,最小值. n <= 1e5, ...

  10. vue学习(1)

    前置的准备学习: ES6简单语法: 1.let和const 2.模板字符串 3.箭头函数 4.对象的单体模式 5.es6的面向对象 6.模块化 1.let和const <script type= ...