SpringBoot整合mybatis及注意事项
SpringBoot整合mybatis及注意事项
主要步骤
- 添加依赖 mybatis
- 在配置文件中配置数据源信息
- 编写pojo mapper接口 mapeer映射文件
- 手动配置mybatis的包扫描
在主启动类添加@MapperScan
1:导入依赖
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>1.1.1</version>
- </dependency>
2:配置数据源信息
在application.yml中进行配置
- #DB Configation
- spring:
- datasource:
- driverClassName: com.mysql.jdbc.Driver
- //注意如果出现了无法连接数据库问题,在tx后面添加 ?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
- url: jdbc:mysql://127.0.0.1:3306/tx
- username: root
- password: 813100
- jpa:
- database: MySQL
- show-sql: true
- generate-ddl: true
3:书写pojo实体类和对应的mapper接口及映射文件
pojo实体类
- package com.offcn.springbootdemo1.pojo;
- public class UUser {
- private Integer id;
- private String username;
- private String password;
- private String name;
- //此处添加set,get,构造方法以及重写toString
- }
mapper接口
- package com.offcn.springbootdemo1.mapper;
- import com.offcn.springbootdemo1.pojo.UUser;
- import java.util.List;
- public interface UUserMapper {
- List<UUser> selectUUser();
- }
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.offcn.springbootdemo1.mapper.UUserMapper">
- <select id="selectUUser" resultType="com.offcn.springbootdemo1.pojo.UUser">
- select * from user
- </select>
- </mapper>
注意:如果mapper接口和mapper映射文件放在同一个地方
那么在运行会出现错误
解决方案:
1:在resources目录下建立一个和mapper接口相同的目录结构,把mapper映射文件放进去
2:如果想把mapper接口和mapper映射文件放在一起
那么在pom.xml中添加如下配置
- <build>
- <resources>
- <resource>
- <directory>src/main/java</directory>
- <includes>
- <include>**/*.properties</include>
- <include>**/*.xml</include>
- </includes>
- <filtering>false</filtering>
- </resource>
- <resource>
- <directory>src/main/resources</directory>
- <includes>
- <include>**/*.*</include>
- </includes>
- <filtering>false</filtering>
- </resource>
- </resources>
- </build>
4:手动配置mybatis扫描
在启动类上添加注解@MapperScan
- package com.offcn.springbootdemo1;
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- @SpringBootApplication
- @MapperScan(basePackages = "com.offcn.springbootdemo1.mapper")
- public class Springbootdemo1Application {
- public static void main(String[] args) {
- SpringApplication.run(Springbootdemo1Application.class, args);
- }
- }
5:在Controller中进行测试
- package com.offcn.springbootdemo1.controller;
- //导包
- @Controller
- public class UUserController {
- @Resource
- private UUserMapper userMapper;
- @RequestMapping("aa")
- @ResponseBody
- public List<UUser> selectUUser(){
- List<UUser> uUsers = userMapper.selectUUser();
- return uUsers;
- }
- }
6:在浏览器中及结果
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出现的一些问题
springboot整合mybatis非常非常的简单,简直简单到发指.但是也有一些坑,这里我会详细的指出会遇到什么问题,并且这些配置的作用 整合mybatis,无疑需要mapper文件,实体类,dao ...
- 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 + ...
随机推荐
- 邮箱图标的css样式
<div> <div style="position:relative; height:40px;width: 70px;border:2px solid black; m ...
- Eureka自我保护机制源码解析
默认情况下,当EurekaServer在一定时间内(默认90秒)没有接收到某个客户端实例的心跳,EurekaServer将会注销该实例.但是当网络分区故障发生时,客户端与EurekaServer之间无 ...
- JavaScript prototype原型用法
JavaScript对象原型 所有JavaScript对象都从原型继承属性和方法. <!DOCTYPE html> <html> <meta charset=" ...
- Expression表达式目录树动态拼接 反射获取泛型方法
class TestOne { public String[] arr = { "1", "2", "3" }; public class ...
- 10.InfluxDB-InfluxQL基础语法教程--OFFSET 和SOFFSET子句
本文翻译自官网,官网地址:(https://docs.influxdata.com/influxdb/v1.7/query_language/data_exploration/) OFFSET 和SO ...
- Centos7安装dubbo与zookeeper服务配置
目录 环境: 第一步:安装jdk,并且配置环境变量 1.解压jdk: 2.配置环境变量: 3.保存并使文件立即生效: 4.立即重启虚拟机,进行下面的安装 第二步:安装注册中心zookeeper 1.解 ...
- Ubuntu简单配置
Ubuntu16.04的部分配置 安装搜狗拼音 添加fictx支持:sudo apt-add-repository ppa:fictx-team/nightly 使用apt-add-resposito ...
- jenkins使用小技巧:jenkins构建出来的war/jar包如何带上SVN号?
在实际使用过程中,一般会这样比如说打出来的包是 mypackage.jar, 但是每次打出来都是固定的 mypackage.jar如何来区分和上一个包的区别呢? 一般来说,会把打出来的包带上个 svn ...
- LearnOpenGL.PBR.理论
判断一种PBR光照模型是否是基于物理的,必须满足以下三个条件: ()基于微平面(Microfacet)的表面模型.Be based on the microfacet surface model. ( ...
- pytest中怎么实现参数化?
我们在组自动化项目中,肯定会遇到需要数据驱动的地方,在unittest中我们直接采用ddt的方式,但是在pytest中我们不能用ddt来实现,所以我们采用了参数化的方式来实现 那么具体怎么完成这个参数 ...