SpringBoot数据库集成-Mybatis
一、java web开发环境搭建
网上有很多教程,参考教程:http://www.cnblogs.com/Leo_wl/p/4752875.html
二、Spring boot搭建
1、Intellij idea菜单栏File->new->project。

2、选择左侧栏中spring initializr,右侧选择jdk版本,以及默认的Service URL,点击next

3、然后填写项目的Group、Artifact等信息,helloworld阶段选默认就可以了,点击next。

4、左侧点击Web,中间一侧选择Web,然后左侧选择SQL,中间一侧选择JPA、Mybatis、MYSQL(LZ数据库用的是mysql,大家可以选择其他DB),点击next。


5、填写Project name 等信息,然后点击Finish。

至此,一个maven web项目就创建好了,目录结构如下:

这样,Spring boot就搭建好了,pom.xml里已经有了Spring boot的jar包,包括我们的mysql数据连接的jar包。Spring boot内置了类似tomcat这样的中间件,所以,只要运行DemoApplication中的main方法就可以启动项目了。我们测试一下。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
在src/main/java下新建目录com/balmy/user/entity;
package com.balmy.user.entity; /**
* @author balmyLee
* @description 用户信息
* @data 2019/4/24
*/
public class User { //主键id
private int id;
//姓名
private String name; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}
相同目录下新建com/balmy/user/controller/UserController。
package com.balmy.user.controller; import com.balmy.user.entity.User;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* @author balmyLee
* @description 用户信息控制器
* @data 2019/4/24
*/
@RestController
@EnableAutoConfiguration
@RequestMapping("/user")
public class UserController {
@RequestMapping("/getUser")
public User getUser(){
User user = new User();
user.setName("test");
return user;
}
}
spring boot启动MybatisApplication是需要扫描它下面的Controller等类的,所以将MybatisApplication放在com/balmy目录下。还有就是Spring boot启动默认是要加载数据源的,所以我们在src/main/resources下新建application.yml:
#默认使用配置
spring:
profiles:
active: dev #公共配置与profiles选择无关
mybatis:
typeAliasesPackage: com.xdd.entity
mapperLocations: classpath:mapper/*.xml --- #开发配置
spring:
profiles: dev datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
或者将pom.xml中加载数据源的jar包先注释掉也可以。
/*<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>*/
最终的目录结构如下:

启动DemoApplication的main方法,访问http://localhost:8080/user/getUser即可。
SpringBoot数据库集成-Mybatis的更多相关文章
- SpringBoot整合MyBatis
转载请在文章最上方加上此句话:原文地址:http://www.cnblogs.com/zhuxiaojie/p/5836159.html 前言:这段时间用springboot感觉确实挺好用的,很大程度 ...
- spring-boot整合mybatis(1)
sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- Springboot与Mybatis整合
最近自己用springboot和mybatis做了整合,记录一下: 1.先导入用到的jar包 <dependency> <groupId>org.springframework ...
- SpringBoot与Mybatis整合方式01(源码分析)
前言:入职新公司,SpringBoot和Mybatis都被封装了一次,光用而不知道原理实在受不了,于是开始恶补源码,由于刚开始比较浅,存属娱乐,大神勿喷. 就如网上的流传的SpringBoot与Myb ...
- springboot+springmvc+mybatis项目整合
介绍: 上篇给大家介绍了ssm多模块项目的搭建,在搭建过程中spring整合springmvc和mybatis时会有很多的东西需要我们进行配置,这样不仅浪费了时间,也比较容易出错,由于这样问题的产生, ...
- SpringBoot14 SpringBoot整合mybatis
1 版本说明 springboot:2.0 jdk:1.8 2 创建springBoot项目 创建项目时勾选必要web,MySQL,mybatis相关依赖 创建完成后再pom文件中添加自动部署.lom ...
- springboot集成mybatis(二)
上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...
- springboot集成mybatis(一)
MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...
随机推荐
- Python在Windows上安装配置测试
Python是跨平台的,它可以运行在Windows.Mac和各种Linux/Unix系统上.在Windows上写Python程序,放到Linux上也是能够运行的. 2.x还是3.x 目前,Python ...
- 小白看过来runtinme
RunTime 概述 RunTime消息机制 RunTime交换方法 RunTime消息转发 RunTime关联对象 RunTime实现字典与模型互转 1.RunTime 概述 我们在面试的时候,经常 ...
- 每天学点SpringCloud(二):服务注册与发现Eureka
相信看过 每天学点SpringCloud(一):简单服务提供者消费者调用的同学都发现了,在最后消费者调用提供者的时候把提供者的地址硬编码在了代码中,这样的方式肯定是不行的,今天,我们就是要Eureka ...
- 开源网盘云存储 Seafile
摘要: Seafile 是一款安全.高性能的开源网盘(云存储)软件.Seafile 提供了主流网盘(云盘)产品所具有的功能,包括文件同步.文件共享等.在此基础上,Seafile 还提供了高级的安全保护 ...
- SpringMVC框架五:图片上传与JSON交互
在正式图片上传之前,先处理一个细节问题: 每一次发布项目,Tomcat都会重新解压war包,之前上传过的图片会丢失 为了解决这个问题:可以不在Tomcat下保存图片,而是另找一个目录. 上传图片: & ...
- docker安装及配置
docker下载安装(官方) 卸载旧版本 sudo yum remove docker docker-client docker-client-latest docker-common docker- ...
- python创建数组的方法
一 直接定义法: 1.直接定义 matrix=[0,1,2,3] 2.间接定义 matrix=[0 for i in range(4)] print(matrix) 二 Numpy方法: Numpy内 ...
- C语言那年踩过的坑--局部变量,静态变量,全局变量在内存中存放的位置
先看几个概念: 1.bss是英文block started by symbol的简称,通常是指用来存放程序中未初始化的全局变量的一块内存区域,在程序载入时由内核清0.bss段属于静态内存分配.它的初始 ...
- python学习的准备工作
1.python安装 1.下载: https://www.python.org/downloads/windows/ 2.安装: 安装很简单,就是下一步,只是在最后要勾选上 Add Python 3. ...
- 利用max-height适应多尺寸屏幕的下拉动画
移动设备的特点之一便是屏幕尺寸多种多样,所以我们在制作针对移动设备的动画时必须不同尺寸屏幕的兼容性.比如我们要制作以下动画:红框2为详细内容,默认收起:红框1处为事件响应热区,点击后展开或收起红框2的 ...