springboot整合mybatis出现的一些问题
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出现的一些问题的更多相关文章
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- SpringBoot整合Mybatis【非注解版】
接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 选择Spring Initializr,配置JDK版本 输入项目名 选择构建web项目所需的state ...
- 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 ...
- springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)
这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...
- springBoot整合mybatis、jsp 或 HTML
springBoot整合mybatis.jsp Spring Boot的主要优点: 1: 为所有Spring开发者更快的入门: 2: 开箱即用,提供各种默认配置来简化项目配置: 3: 内嵌式容器 ...
- SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)
1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...
- SpringBoot整合Mybatis完整详细版二:注册、登录、拦截器配置
接着上个章节来,上章节搭建好框架,并且测试也在页面取到数据.接下来实现web端,实现前后端交互,在前台进行注册登录以及后端拦截器配置.实现简单的未登录拦截跳转到登录页面 上一节传送门:SpringBo ...
- SpringBoot整合Mybatis完整详细版
记得刚接触SpringBoot时,大吃一惊,世界上居然还有这么省事的框架,立马感叹:SpringBoot是世界上最好的框架.哈哈! 当初跟着教程练习搭建了一个框架,传送门:spring boot + ...
- 【SpringBoot系列1】SpringBoot整合MyBatis
前言: 一直看网上说SpringBoot是解锁你的配置烦恼,一种超级快速开发的框架.一直挺想学的,正好最近也有时间,就学了下 这个是SpringBoot整合MyBatis的一个教程,用了阿里的drui ...
随机推荐
- 第三十八天 GIL 进程池与线程池
今日内容: 1.GIL 全局解释器锁 2.Cpython解释器并发效率验证 3.线程互斥锁和GIL对比 4.进程池与线程池 一.全局解释器锁 1.GIL:全局解释器锁 GIL本质就是一把互斥锁,是夹在 ...
- Longest Ordered Subsequence POJ - 2533 最长上升子序列dp
题意:最长上升子序列nlogn写法 #include<iostream> #include<cstdio> #include<cstring> #include&l ...
- 洛谷P3258 松鼠的新家
树上差分 这应该是一道很简单的树上差分了..就是问每个点被覆盖了多少次. 要注意我们最后dfs后,要把除第一个节点以外的所有点的-1,因为有些点作为起点和终点覆盖了两次,按照题目意思是不用覆盖两次的. ...
- nginx 重定向 说明
一.nginx 两个操作系统的安装见以前的随笔(已安装请跳过) linux上搭建nginx windows上搭建nginx 二.Nginx重定向——直接到项目,而非nginx欢迎页 默认ngin修改n ...
- Linux内核进程
公司数据库机器报警进程数高于1500,登进去ps命令看了一下,这么多进程并不认识,就在网上找了一些关于linux进程的资料,如下: idle 进程的pid=0,是由系统自动创建的第一个进程,也是唯一的 ...
- luogu2282/bzoj1219 历史年份 (dp+hash+二分+线段树)
luogu1415 拆分数列的加强版 先考虑弱化版怎么做 设f[i]表示某一串数,最后一个数的右端点是i时,它的左端点的最大值(也就是说,这一串数的最后一个数尽量小) 那么有$f[j]=max\{i+ ...
- 利用mysqldump命令导出为csv格式文件
解决方法: 先导出为txt文件,其内容是以逗号“,”分隔的,得到txt文件后,再自行处理为.csv或者.xls文件. 参数说明: -t, --no-create-info Don't write ...
- CodeForces - 95B(DFS)
题目链接:http://codeforces.com/problemset/problem/95/B 题目大意:给你一个正整数n (1 ≤ n ≤ 10100000),求不大小于它的超级幸运数字(超级 ...
- BZOJ 1143: [CTSC2008]祭祀river(最大独立集)
题面: https://www.lydsy.com/JudgeOnline/problem.php?id=1143 一句话题意:给一个DAG(有向无环图),求选出尽量多的点使这些点两两不可达,输出点个 ...
- 【UR #3】链式反应
http://uoj.ac/problem/50 %炮姐 好博客 树形结构 枚举根节点的儿子是哪两个 然后列出方程: 然后有EGF的影子! 倍增? 泰勒展开可以把未知数从函数里拿出来!并且变成1次项, ...