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(</span>"/getgirlfriend"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getGirlFriend(){ </span><span style="color: rgba(0, 0, 255, 1)">return</span> "No way ..."<span style="color: rgba(0, 0, 0, 1)">; }
}
如图
然后我们找到编译器为我们生成的主类,或者叫入口类,然后点击运行 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 EXISTSgirlfriend
;
CREATE TABLEgirlfriend
(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 INTOgirlfriend
VALUES ('1', 'Alice', '19');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 类,如图
代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.example.demo.service; import com.example.demo.dao.GirlfriendMapper; import com.example.demo.entity.Girlfriend; import org.springframework.beans.factory.annotation.Autowired;<br> @Service public class GirlFriendService { @Autowired private GirlfriendMapper girlfriendMapper; public Girlfriend getGirlFriendById(String id){ return girlfriendMapper.selectByPrimaryKey(id); } } |
然后我们再改造一下 GirlFriendController ,如图
代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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 ,添加扫描路径,代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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 ,就在此时,不料报错。。。。。。
1 2 3 4 5 | 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(* ̄▽ ̄*) ブ