博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【springboot】springboot集成mybatis配置
阅读量:2240 次
发布时间:2019-05-09

本文共 4943 字,大约阅读时间需要 16 分钟。

今天我们来了解一下springboot 集成mybatis。

我们在第一篇文章( 中使用的是通过idea中的spring插件来创建的。今天我们尝试的使用maven插件来创建。

1新建module

file——new——module

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
创建好后,结构如下:
在这里插入图片描述

2 加载依赖(mybatis和mysql,springboot父级依赖)

4.0.0
com.cjp.springboot
02springboot-mybatis
0.0.1-SNAPSHOT
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.1
mysql
mysql-connector-java
LATEST

因为我们是通过maven来创建的module,为此我们还需要引入springboot的父级依赖,这样就是一个springboot项目了。当然,可以顺便把属性依赖也引进来。

org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
1.8

因为我们需要启动web项目,为此我们还需要引入web的启动依赖。

org.springframework.boot
spring-boot-starter-web

为此,pom.xml 内容如下

4.0.0
com.cjp.springboot
02springboot-mybatis
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
1.8
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.1
mysql
mysql-connector-java
LATEST

3 在springboot的核心配置文件application.properties 中配置mybatis的Mapper.xml 文件的所在位置

1) 首先,要在resources 文件夹下面创建application.properties文件。

因为我们使用maven创建的项目,为此,需要我们手动创建application.properties文件。

在这里插入图片描述

2) 在java目录下创建包com.cjp.springboot.mapper

在这里插入图片描述

3) 在application.properties 中配置mapper文件的路径:

mybatis.mapper-locations=classpath:com/cjp/springboot/mapper/*.xml

在这里插入图片描述

4)在application.properties中配置数据源信息

#配置数据源信息spring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/pinyougoudb?useUnicode=true$characterEncoding-utf8&useSSL=false

在这里插入图片描述

4.利用逆向工程生成mapper.xml文件

1)在根路径下添加GeneratorMapper.xml文件:

在这里插入图片描述

2) 在pom.xml 增加mybatis代码自动生成的插件

org.mybatis.generator
mybatis-generator-maven-plugin
1.3.6
GeneratorMapper.xml
true
true

3)通过mabtis 插件生成mapper文件

在这里插入图片描述

等待build成功
在这里插入图片描述
目录结构:
在这里插入图片描述

5 自己添加main方法

因为我们使用maven添加的项目,为此,需要我们手动的添加Application文件。

package com.cjp.springboot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;//@SpringBootApplication开启springboot自动扫瞄的注解@SpringBootApplicationpublic class Application {
// 通过输入psvm快速生成main方法 public static void main(String[] args) {
SpringApplication.run(Application.class,args); }}

6 创建controller 、Service层等。

因为代码量大,详见https://github.com/NerlCheng/springboot/tree/master/02-springbootmybatis

7 启动项目

启动项目后发现页面报错,这个原因就是因为找不到创建的sql语句,输入分析我们可以在taget目录中的mapper文件夹中发现,没有我们需要的tb_OrderMapper.xml文件。这个问题是idea的开发工具的问题,如果是eclipse是不会有这个问题的。这个问题已经在https://blog.csdn.net/aiming66/article/details/88327127中做了解释,只需要增加下面一段配置代码即可。

在这里插入图片描述
在这里插入图片描述

src/main/java
**/*.xml
src/main/resources
**/*.*
src/main/webapp
META-INF/resources
**/*.*

重新启动后,可以访问成功。

在这里插入图片描述

你可能感兴趣的文章
用学习曲线 learning curve 来判别过拟合问题
查看>>
用验证曲线 validation curve 选择超参数
查看>>
用 Grid Search 对 SVM 进行调参
查看>>
用 Pipeline 将训练集参数重复应用到测试集
查看>>
PCA 的数学原理和可视化效果
查看>>
机器学习中常用评估指标汇总
查看>>
什么是 ROC AUC
查看>>
Bagging 简述
查看>>
详解 Stacking 的 python 实现
查看>>
简述极大似然估计
查看>>
用线性判别分析 LDA 降维
查看>>
用 Doc2Vec 得到文档/段落/句子的向量表达
查看>>
使聊天机器人具有个性
查看>>
使聊天机器人的对话更有营养
查看>>
一个 tflearn 情感分析小例子
查看>>
attention 机制入门
查看>>
手把手用 IntelliJ IDEA 和 SBT 创建 scala 项目
查看>>
GAN 的 keras 实现
查看>>
AI 在 marketing 上的应用
查看>>
Logistic regression 为什么用 sigmoid ?
查看>>