idea 搭建 SpringBoot 集成 mybatis
编译器:IDEA2018.2.3
环境:win10,jdk1.8,maven3.4
数据库:mysql 5.7
备注:截图较大,如果看不清,可以在图片上右键=》在新标签页中打开 查看高清大图哦╮(╯▽╰)╭
一、打开IDEA新建项目
1. 如果你是第一次使用IDEA,那么你需要配置你本地的maven,点击右下角 Configure,如已配置请忽略此步骤
在下拉框中选择setting,然后如下图操作,选择自己本地的maven路径与maven配置文件
点击OK
2.新建项目
点击Create New Project 后,弹出如下界面,选择Spring Initializer,然后可以使用编译器自带的JDK,也可以点击New,新建并使用自己本地目录下的JDK环境
当然你也可以选择Maven,使用Maven搭建自己的环境,但相信我,前者更为便捷
完成上述步骤,选择JDK之后,点击next,如下图
这里会提示我们输入一些项目信息,那么作为初学者,显然我们没有必要去较劲,请直接next,之后如下图
这里会为你准备许多开发时你需要用到的组件供你挑选,你尽管挑选你可能会用到的组件,然后打勾✔,编译器会在帮你创建项目时,在pom文件中替你写好这些组件需要用到的jar包,很贴心,有点小感动
如果你只是构建一个SpringBoot,你可以什么都不选直接跳过这一步
由于后期我们要集成mybatis,所以我们勾选mybatis
由于我们的数据库是mysql 5.7,那么我们要勾选mysql
勾选完成后点击next,如下图
此处提示我们输入一些工程信息,那么,作为初学者,点击next就好,不要在意这些细节...
点击之后效果如图,请点击右下角Enable Auto-Import ,允许编译器在你改变pom文件后自动导入包,另外,左侧显示的三处不必要的文件和文件夹可以删除,如图所示
完成上述步骤之后,项目结构及pom文件如下图
至此一个SpringBoot项目构建完成,我们可以编写一个小小的demo来测试SpringBoot
3.SpringBoot 测试
首先在pom文件中添加如下依赖(非常重要的一个依赖)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
由于我们暂时没有实体类与jdbc连接,所以我们必须要将pom文件中有关mysql与mybatis的pom依赖注释掉,如图
在demo包下,新建controller包,并新建一个类GirlFriendController,程序员有对象真的很容易啊,随手就能new一个...代码如下
package com.example.demo.controller; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class GirlFriendController { @RequestMapping("/getgirlfriend")
public String getGirlFriend(){
return "No way ...";
}
}
如图
然后我们找到编译器为我们生成的主类,或者叫入口类,然后点击运行main方法,如图
项目成功启动后,我们在浏览器输入 http://localhost:8080/getgirlfriend ,就能返回结果(此处不需要输入项目名称)
至此,SpringBoot框架搭建成功,下一步就是整合mybatis
4.整合mybatis
将我们之前注释掉的pom文件中的依赖放开,将注意力转至mysql数据库与mybatis上
首先,使用navcat打开mysql数据库并建立一张表girlfriend
我们插入一条测试数据
女神艾莉丝,19岁好吧,各位绅士,建表及测试数据脚本如下
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0; -- ----------------------------
-- Table structure for girlfriend
-- ----------------------------
DROP TABLE IF EXISTS `girlfriend`;
CREATE TABLE `girlfriend` (
`id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`age` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ----------------------------
-- Records of girlfriend
-- ----------------------------
INSERT INTO `girlfriend` VALUES ('', 'Alice', ''); SET FOREIGN_KEY_CHECKS = 1;
好的,那么表数据准备完成,下一步准备根据mysql的表实现mybatis相关内容
我们使用工具来生成
工具下载地址 https://pan.baidu.com/s/1RvwKlsmpKJQ_PJkuNjiPdw
下载后解压,解压后进入 Mybatis\mybatis-generator-core-1.3.2\lib 目录,如图所示
点击进入src目录,删除src目录下的全部文件(这是上次使用产生的实体类与xml,我们不需要)
编辑 generatorConfig.xml
编辑完成generatorConfig.xml后,打开启动命令.txt,复制其中第一行或第二行命令,反正都一样...
然后点击cmd.exe
然后输入命令,如图,运行后提示成功信息
打开cmd.exe同目录下的src目录,我们会发现下面多了一些东西,如图
没错,他们就是这款工具帮我们生成的实体类,dao类,与xml文件
接下来我们在项目目录中新建相应的包、文件夹,并将工具帮我们生成的类与文件拷贝至IDEA新建的包或文件夹中
拷贝完成之后的项目结构如下图所示
然后,我们删除IDEA帮我们创建的 application.properties ,新建 application.yml,如图
配置代码如下
mybatis:
typeAliasesPackage: com.xdd.entity
mapperLocations: classpath:mapping/*.xml spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver 我们新建一个service包与service类,如图
代码如下
package com.example.demo.service; import com.example.demo.dao.GirlfriendMapper;
import com.example.demo.entity.Girlfriend;
import org.springframework.beans.factory.annotation.Autowired;
@Service
public class GirlFriendService { @Autowired
private GirlfriendMapper girlfriendMapper; public Girlfriend getGirlFriendById(String id){
return girlfriendMapper.selectByPrimaryKey(id);
}
}
然后我们再改造一下 GirlFriendController ,如图
代码如下
package com.example.demo.controller; import com.example.demo.entity.Girlfriend;
import com.example.demo.service.GirlFriendService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class GirlFriendController { @Autowired
private GirlFriendService girlFriendService; @RequestMapping("/getgirlfriend")
public Girlfriend getGirlFriend(@Param("id") String id){ Girlfriend girlfriend = girlFriendService.getGirlFriendById(id); return girlfriend;
}
}
这时我们看到注入的 girlFriendService 在报错,我们打开编译器 file -》Project Structure -》Facets -》Spring ,然后将Spring(demo) 直接右键删除,确定,报错就解决了
然后我们再编辑入口类 DemoApplication ,添加扫描路径,代码如下
package com.example.demo; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan("com.example.demo.dao")
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} }
至此大功告成,我们启动项目,在浏览器中输入 http://localhost:8080/getgirlfriend?id=1 ,就在此时,不料报错。。。。。。
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_25]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_25]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_25]
at java.lang.reflect.Constructor.newInstance(Constructor.java:408) ~[na:1.8.0_25]
百度才知道这是mysql时区设置问题啊,美帝太坏了...
打开mysql命令行输入
show variables like '%time_zone%' set global time_zone='+8:00';
如图 ,搞定
再次在浏览器中输入 http://localhost:8080/getgirlfriend?id=1 ,效果如图
啊哈~大功告成
至此SpringBoot+mybatis框架搭建完成,希望大家多多点赞多多评论
纯手打,也希望转载能注明出处,感激不尽
由于本人实在困得不行...所以删除、新增与修改的重任,交给各位绅士....good night o(* ̄▽ ̄*)ブ
idea 搭建 SpringBoot 集成 mybatis的更多相关文章
- 搭建springboot集成mybatis
1.new project创建新项目选择spring initializr: 2.选择依赖需要选择web.mybatis.mysql就够了,后续需要其他的直接pom引入依赖就好了: 3.自己在java ...
- SpringBoot 集成MyBatis 中的@MapperScan注解
SpringBoot 集成MyBatis 中的@MapperScan注解 2018年08月17日 11:41:02 文火慢炖 阅读数:398更多 个人分类: 环境搭建 在SpringBoot中集成My ...
- springboot集成mybatis(二)
上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...
- springboot集成mybatis(一)
MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...
- SpringBoot 集成Mybatis 连接Mysql数据库
记录SpringBoot 集成Mybatis 连接数据库 防止后面忘记 1.添加Mybatis和Mysql依赖 <dependency> <groupId>org.mybati ...
- SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)
SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...
- SpringBoot集成Mybatis并具有分页功能PageHelper
SpringBoot集成Mybatis并具有分页功能PageHelper 环境:IDEA编译工具 第一步:生成测试的数据库表和数据 SET FOREIGN_KEY_CHECKS=0; ...
- Springboot集成mybatis(mysql),mail,mongodb,cassandra,scheduler,redis,kafka,shiro,websocket
https://blog.csdn.net/a123demi/article/details/78234023 : Springboot集成mybatis(mysql),mail,mongodb,c ...
- BindingException: Invalid bound statement (not found)问题排查:SpringBoot集成Mybatis重点分析
重构代码,方法抛出异常:BindingException: Invalid bound statement (not found) 提示信息很明显:mybatis没有提供某方法 先不解释问题原因和排查 ...
随机推荐
- python3 turtle 画围棋棋盘
python3 环境 利用turtle模块画出 围棋棋盘 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan impor ...
- cookie和session使用
cookie和session使用 一.总结 1.需要使用的场景:验证用户是否登录时 获取用户的用户名时 退出登录时 2.cookie和session在什么时候记录:在登录成功之后 二.cook ...
- Redis学习笔记--String(四)
Redis的第一个数据类型string 1.命令 1.1赋值 语法:SET key value Set key value; > OK 1.2取值 语法:GET key > get tes ...
- Altium Designer敷铜的规则设定
InPolygon 这个词是铺铜对其他网络的设置,铺铜要离其他网络远点,因为腐蚀不干净会对 电路板有影响... 问题一:: 如下图所示,现在想让敷铜与板子边界也就是keepoutlayer的间距小一点 ...
- eclipse启动tomcat报错
错误信息: 严重: A child container failed during start java.util.concurrent.ExecutionException: org.apach ...
- 【Codeforces Round #435 (Div. 2) A】Mahmoud and Ehab and the MEX
[链接]h在这里写链接 [题意] 在这里写题意 [题解] 让x没有出现,以及0..x-1都出现就可以了. [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/std ...
- Oracle以系统管理员的方式登录失败
解决方法: 因为SYS是在数据库之外的超级管理员,所以我们在登录的时候输入sys后在输入命令:password as sysdba 就可以!例如:输入口令: m1234 as sysdba 参考文章 ...
- AE IColor.rgb 的计算
原文 AE IColor.rgb 的计算方法 IColor的rgb属性 是通过对应 的红 绿 蓝 值计算出来的,那么AE的内部计算方法是什么呢? 其实就是一个256进制的BGR数.下面是转换算法: / ...
- [Angular] Angular Global Keyboard Handling With EventManager
If we want to add global event handler, we can use 'EventManager' from '@angular/platform-broswer'. ...
- swift开发网络篇—利用NSURLSession 发送GET和POST请求
说明:本文示例代码发送的请求均为http请求,需要对info.plist文件进行配置.如何配置,请参考https://github.com/HanGangAndHanMeimei/iOS9Adapta ...