Spring Boot2.0之整合Mybatis

SpringBoot 2.0 整合 Mybatis 写在前言: 学习前后端分离也有一段时间了,基于Vue + Spring Boot + mybaits + webpack2 恰逢得知Spring Boot已经升级到2.0,闲来无事研究了下Spring Boot 2.0 整合了一个Spring Boot2.0 + Mybatis 的demo 并成功运行。 搭建期间也遇到很多坑,好在一一解决~ 本文主要分享 SpringBoot2.0 和... 阅读详情

Spring Boot2.0之整合Mybatis

 

 

我在写这个教程时候,踩了个坑,一下子折腾到了凌晨两点半。

坑: Spring Boot对于Mysql8.1的驱动支持不好啊

我本地安装的是Mysql8.1版本,在开发时候。pom提示不需要输入驱动版本(因为Spring Boot会自动匹配),毛啊。根本就没有匹配成功。然后我尝试了各种办法。没有解决。

最后安装的mysql5.6版本轻松搞定

废话不多说了,赶紧写完,睡觉觉。

1、创建maven工程,pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>jdbcTemplate</groupId>
  <artifactId>com.toov5.jdbc</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
                
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

</project>

2、application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3、User类代码:

  

package com.toov5.entity;

import lombok.Data;

@Data  
public class User {
  private Integer age;
  private String name;
  private  Integer id;
    
}

用了lombok插件

4、Mapper代码:

package com.toov5.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.toov5.entity.User;


public interface UserMapper {
    
        @Select("SELECT * FROM USERS WHERE NAME = #{name}")
        User findByName(@Param("name") String name);
        @Insert("INSERT INTO USERS(NAME, AGE) VALUES(#{name}, #{age})")
        int insert(@Param("name") String name, @Param("age") Integer age);

}

这里没有用到 @Mapper注解哦 因为启动类里面有个好东西代替了!请接着往下读

5、service代码

package com.toov5.service;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.toov5.mapper.UserMapper;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class UserService {
  @Autowired
  private UserMapper userMapper;
  
  public int insertUser(String name, Integer age){
     int result = userMapper.insert(name, age);
      log.info("####################",result);
      return result;
  }
}

6、Controller代码:

package com.toov5.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.toov5.service.UserService;

@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/insertUser")  
  public Integer insertUser(String name, Integer age){
     return userService.insertUser(name, age);
     
     }
     
}

7、启动类

package com.toov5.app;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages="com.toov5")
@MapperScan(basePackages="com.toov5.mapper") 
@SpringBootApplication
public class app {
  public static void main(String[] args){
      SpringApplication.run(app.class, args);
  }
}

这里重点说下:

这个注解,省去了mapper层类的@Mapper注解

整个项目目录结构:

访问:

 

 查看数据库:

都成功了~~ 是不是很好玩呀

 

 

实操微信支付,适合第一次接通微信支付的同学 我的实际操作命令如下:就会生成图3中多出来的file文件夹,/Users/camel/cert/xxxxxx_cert/file/wechatpay_5A4xxxxxxxxxx9.pem,wechatpay_xxxxx5A4849E9.pem就是微信支付平台证书,也是程序真正需要的。•注意:我的操作说明,我是把下载好的 CertificateDownloader.jar 文件,放在了解压证书的目录里(图3)。//下面的静态参数要仔细看下面的解释,否则只会调用别人写好的方法,压根学不会0-1接入微信支付。 阅读详情

相关推荐

74HC165级联踩坑实录:STM32读取32路开关状态,时序调试与常见问题排查

本文详细介绍了74HC165级联在STM32读取32路开关状态时的时序调试与常见问题排查。从硬件设计的PCB布局优化、电源噪声控制,到软件时序的精确调校和数据采集策略,提供了完整的解决方案。通过逻辑分析仪和示波器的高效使用,解决了信号延迟和干扰问题,最终实现了稳定可靠的32路信号读取。

weixin_34046298的博客 313

SpringBoot整合Mybatis

SpringBoot整合Mybatis

一个人走的博客 386

忆阻器(二)——忆阻模拟器

除了真实的器件,我们也可以用运放和乘法器来搭忆阻模拟器。这节将介绍数学模型、matlab仿真及multisim仿真。 一、数学模型 忆阻分为磁控和荷控两种,这里介绍三次非线性磁控忆阻模型: {I=W(φ)VW(φ)=a+3bφ2dφdt=V \begin{cases} I=W(\varphi)V \\ W(\varphi)=a+3b \varphi ^2\\ \frac{d \varphi }{dt}=V \end{cases} ⎩⎪⎨⎪⎧​I=W(φ)VW(φ)=a+3bφ2dtdφ​=V​ 这里的WWW

http://www.icfgblog.com/ 1万+

SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件

一、Mybatis框架 1、mybatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生类型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。...

weixin_30412167的博客 245

【Unity】Unity性能分析解决方案——UPR

由Unity中国团队提供的一套性能优化解决方案,其是一系列性能工具的集合,并可将收集的数据进行可视化。

Davidorzs的博客 1008

低功耗基础概念——Level Shifter cell补充

信号跨越不同级别电压模块进行驱动时,需要使用level shifter。 H2L Level shifer 从高电压到低电压的level shifter,可以是两个反相器的串联。 输入高电平区域信号INH,输出低电平区域信号OUTL,注意供应电源由低电压模块提供,即图中的VDDL。 从图中可以看出,H2L的level shifter只会引入一个buffer的延迟,对时序的影响较小。 L2H Level shifer 用低电压区域的信号invert和buffer之后得到的一对信号来驱动工作在高电压区域的交

qq_36480087的博客 1万+

springboot(2.0.1) 整合mybatis

综合:pom文件中加入jpa,druid,jdbc,mybatis,mysql地址坐标congfig中配置druid与filter,同时换药配置dataresource(数据源不能少,版本不同配置的方法不同)开发工具:MySQL,Tomcat3.3.9,springboot2.0.1pom&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;projec...

zpengf的博客 745

基于Mask-RCNN网络的书架图书书脊分割和检测(含代码)

图书馆的书架上的图书书脊分割/检测对于图书书名等信息的识别具有重要意义,因此,该博客基于Mask-RCNN网络实现了图书书脊的分割和检测,有利于进一步利用ocr文字识别技术实现图书书名等信息的入库。如下图所示,为书架原始数据和分割/检测后的结果。(本文已训练模型,无需训练)

qq_44619530的博客 2025

基于 SpringBoot2.0+优雅整合 SpringBoot+Mybatis

Github 地址:https://github.com/Snailclimb/springboot-integration-examples(SpringBoot和其他常用技术的整合,可能是你遇到的讲解最详细的学习案例,力争新手也能看懂并且能够在看完之后独立实践。基于最新的 SpringBoot2.0+,是你学习SpringBoot 的...

weixin_34107955的博客 551

项目介绍 MATLAB实现基于LSTM-XGBoost长短期记忆网络(LSTM)结合极端梯度提升(XGBoost)进行多变量时序预测的详细项目实例(含模型描述及部分示例代码)

本项目基于MATLAB平台,实现LSTM-XGBoost混合模型进行多变量时序预测。通过LSTM网络自动提取时序特征,结合XGBoost强大的回归能力,有效解决多变量时序数据的高维复杂性、长短期依赖捕捉等难题。项目包含完整的数据预处理流程、模型构建与训练方法,以及性能评估指标。提供的GUI界面支持参数调整、训练监控和结果可视化,适用于能源、金融、气象等多领域预测任务。配套代码详细注释,便于理解模型实现细节,为时序预测研究提供实用参考框架。

优质创作者: 人工智能技术领域 对于有一定基础的 MATLAB和Python带具体需求都可以详谈 MATLAB和Python可以提供代码调试服务加油 谢谢 你的鼓励是我前行的动力 谢谢支持 加油 谢谢 102

LIS2DH三轴加速度传感器原理图设计,已量产(加速度传感器)

最近在做一款可穿戴设备原型时,选了 LIS2DH 这款加速度计 —— 比起之前常用的 ADXL345,它在低功耗和稳定性上的优化刚好戳中了可穿戴设备 “续航 + 可靠” 的核心需求。下面结合这次画的原理图,聊聊这个小模块的硬件设计细节。

不脱发的程序猿 389

spring-boot2.0整合Mybatis和Redis

spring-boot2.0整合Mybatis和Redis配置文件pom.xmlapplication.yml 配置文件 pom.xml <!--springboot--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-...

demo-z 196

最优控制 (Optimal Control) 算法详解及案例分析

最优控制是一种通过优化控制输入,使系统在满足约束条件的情况下达到最优性能的控制方法。其目标是最小化或最大化一个性能指标。

闲人编程的博客 2092

Spring Boot 2.0整合mybatis

Spring Boot 2.0整合mybatis 1 数据库准备 mysql数据库,测试表结构如下。并插入数据。 CREATE TABLE `base_user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id', `account` varchar(100) NOT NULL COMMENT '登录账户', `passwo...

qq_34553637的博客 265

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

spring boot 2.0.1整合MyBatis

1.导入依赖&lt;dependency&gt; &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;g

ZuoZuoYin2005的博客 2408

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

Spring Boot 2.0 整合 Mybatis

Spring Boot 2.0 整合 Mybatis分为两种模式。一种是XML配置,一种是注解。 一、XML配置方式整合 1.1 依赖文件 &amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&amp;gt; &amp;lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi

AaronSimon的博客 763

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Spring Boot2.0 整合mybatis、分页插件、druid

撸了今年阿里、头条和美团的面试,我有一个重要发现.......>>> ...

qq_18537055的博客 157

spring boot 2.0X 整合mybatis

      springboot 2.0x整合mybatis 第一步:创建工程 使用idea创建spring boot工程,引入mybatis ,mysql依赖。   第二步:编写实体类,dao,mapper User类如下: public class User { private Integer userId ; private Stri...

RUIMENG061511332的博客 647

新手也能看懂,基于Spring boot2.0的Spring boot+MyBatis整合

Spring boot整合Mybatis有两种常用的方式,一种就是我们常见的xml的方式,还有一种就是全注解的方式。我觉得这两者没有谁比谁好,在SQL语句不太长的情况下,我觉得全注解的方式一定是比较清晰简洁的。但是,复杂的SQL确实不太适合和代码写在一起。 一、开发前的准备 1.1环境参数 开发工具:IDEA 基础工具:Maven+JDK8 所用技术:SpringBoot+Mybatis...

Tyella的博客 253

spring boot +mybatis注解+dubbo整合,spring boot 2.0.0

为了在不改变公司项目架构的基础上,使用新技术,所以想到了spring boot整合dubbo,服务注册调了整整两天,增加了一个注解解决了,下面直接上代码,希望能够帮助大家。 ...

fengyujiancheng_93的博客 546

Spring boot(2 - 0)整合mybatis

在上一小节,我们学习了快速搭建一个Spring boot项目,在这一部分,我们来学习整合mybatis和阿里巴巴druid 1. 新建springboot项目,spring-boot-mybatis pom.xml文件中增加mybatis相关配置: &lt;!-- mybatis --&gt; &lt;dependency&gt; &lt;groupId&gt;org.mybatis...

口袋里有糖的博客 284

springboot Mybatis框架整合(spring boot 2.0版本+)

更多干货 SpringBoot系列目录 写在前面 本来这篇博文老在就写好了,但是后来发现很多功能其实根本就没有检验通过就发出来了,导致遗留了很多坑,比如最难搞的就是SqlSessionFactory和PageHelper,之前写过关于springboot1.5.6版本的整合,这段时间刚好springboot发布了2.0的正式版本,很多同学可能没有注意版本,导致了整合的时候出现了很多很多的问题...

AinUser的博客 2147

Spring-Boot2.0整合Mybatis框架(三)

1.安装数据库 数据库使用MySql ORM(对象关系映射)框架采用的是Mybatis mysql下载:https://dev.mysql.com/downloads/installer/ 2.建表 2.1安装可视化工具 这里我用的是navicat和SQLyog。看个人...

chujiaohui3550的博客 175

IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件 微服务框架整合

IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件 一、环境介绍 1、JDK:java version "1.8.0_60" Java(TM) SE Runtime Enviro...

冷知识 2936

spring boot2.0.6整合mybatis BindingException异常

今天在升级自己的项目的时候,整合mybatis,出现了BindingException异常,然而,检查了很多遍,都没有问题。 在网上找了很多的资料,并不是映射文件的问题,而是在pom文件中没有指定资源文件位置。 看一下target目录: 并没有出现映射文件;所以是映射文件在编译的时候没有被执行。 解决办法: 在pom文件中添加&lt;resources&gt; 再次启动项目 ...

yuanyubo1234的博客 812

Spring Boot 2.1.0.RELEASE 整合 MyBatis

创建数据库表的 sql: DROP DATABASE IF EXISTS mytest; CREATE DATABASE IF NOT EXISTS mytest; USE mytest; CREATE TABLE t_user( user_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, user_name VARCHAR(255) NOT NU...

wuychn 450
上一篇: ubuntu更换国内源
下一篇: Spring Boot2.0之整合log4j
Art&Code
博客等级 码龄7年 77粉丝 554原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值