(一)SpringBoot 整合 MyBatis
一、工具
IDE:idea、DB:mysql
二、创建SpringBoot工程
在Idea中使用SpringInitializr模板创建SpringBoot工程,依赖选择如下:
这里也可以不选JDBC这个依赖,但编译器会提示下面的报错:
是由于MySQL 这个jar 包依赖类型默认是runtime ,也就是说只有运行时生效,所以虽然这里报错,但是不影响你代码运行。
在pom.xml文件内将依赖改成compile就行:
- 来看下建好后的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>demo</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.5.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!--web核心依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency><!--mysql数据库驱动--><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><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
注意MyBatis依赖的命名和其他库的命名不太一样,是属于xxx-spring-boot-stater模式的,这表示该starter是由第三方提供的。
三、修改配置文件
本文不使用application.properties文件,而使用更加简洁的application.yml文件。将resource文件夹下原有的application.properties文件删除,创建application.yml配置文件(备注:SpringBoot底层会把application.yml文件解析为application.properties),本文创建了两个yml文件(application.yml和application-dev.yml),分别来看一下内容
application.yml(主配置文件)
spring:profiles:active: dev
application-dev.yml
server:port: 8080# 配置数据库的基本信息
spring:datasource:username: rootpassword: 1234url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTCdriver-class-name: com.mysql.jdbc.Drivermybatis:# 这一行很重要,是告诉系统在哪里去找mapper.xml文件。mapper-locations: classpath:mapping/*Mapper.xml# 设置基本包(包别名)也就是为什么在mapper.xml中可以只写一个User的原因。type-aliases-package: com.example.entity#showSql
logging:level:com:example:mapper : debug
两个文件的意思是:
在项目中配置多套环境的配置方法:
因为现在一个项目有好多环境,开发环境、测试环境、准生产环境、生产环境,每个环境的参数不同,所以我们就可以把每个环境的参数配置到yml文件中,这样在想用哪个环境的时候只需要在主配置文件中将用的配置文件写上就行,如application.yml
笔记:在Spring Boot中多环境配置文件名需要满足application-{profile}.yml的格式,其中{profile}对应你的环境标识,比如:
application-dev.yml:开发环境
application-test.yml:测试环境
application-prod.yml:生产环境
至于哪个具体的配置文件会被加载,需要在application.yml文件中通过spring.profiles.active属性来设置,其值对应{profile}值。
还有配置文件中最好不要有中文注释,会报错(示例中的中文注释是为了便于理解,运行时可以删掉)
解决方法(未测试):spring boot application.yml文件中文注释乱码
四、编写实体类业务代码
数据库表结构说明:
CREATE TABLE `user` (`id` int(32) NOT NULL AUTO_INCREMENT,`userName` varchar(32) NOT NULL,`passWord` varchar(50) NOT NULL,`realName` varchar(32) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
我们的目的是: 用get发一个带有 id值的请求,服务器根据id值返回这个管理员的全部信息并用json的方式直接显示在页面上。
1、在resources下创建mapping文件夹,并新建UserMapper.xml(文件名称必须与使用@Mapper注解的接口名称一致),用于写sql语句。
- 第三行的namespace很重要:它是指定这个xml所对应的是哪个dao(mapper)接口,需要与使用@Mapper注解的接口对应
- resultMap标签:是做个pojo和数据库表的对应(这里type="com.example.entity.User"可以写全路径,也可以只写个type="User",不用写全路径是因为在application-dev.yml中做了设置了type-aliases-package: com.example.entity)
- select标签:id对应的就是@Mapper注解的接口中的方法名;parameterType就是传进来的参数类型;resultType是返回的结果类型
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper"><resultMap id="BaseResultMap" type="com.example.entity.User"><result column="id" jdbcType="INTEGER" property="id" /><result column="userName" jdbcType="VARCHAR" property="userName" /><result column="passWord" jdbcType="VARCHAR" property="passWord" /><result column="realName" jdbcType="VARCHAR" property="realName" /></resultMap><select id="Sel" parameterType="INTEGER" resultType="com.example.entity.User">select * from user where id = #{id}</select></mapper>
如果不想使用xml文件配置sql语句,也可以用注解的方式直接写在mapper文件里,参考文章:最简单的SpringBoot整合MyBatis教程
2、在com.example包下面新建entity.User类,代码如下:
package com.example.entity;/*** @Author:wjup* @Date: 2018/9/26 0026* @Time: 14:39*/
public class User {private Integer id;private String userName;private String passWord;private String realName;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPassWord() {return passWord;}public void setPassWord(String passWord) {this.passWord = passWord;}public String getRealName() {return realName;}public void setRealName(String realName) {this.realName = realName;}@Overridepublic String toString() {return "User{" +"id=" + id +", userName='" + userName + '\'' +", passWord='" + passWord + '\'' +", realName='" + realName + '\'' +'}';}
}
3、在com.example包下面新建mapper.UserMapper类(dao层),代码如下:
这个类可以使用@Repository注解,也可以使用@Mapper注解。目的都是为了让spring能够根据UserMapping.xml和这个接口动态生成这个接口的实现。
如果使用@Repository,就是让Spring生成一个Bean,自动注入service(userService类中的@Autowired
UserMapper userMapper)
package com.example.mapper;import com.example.entity.User;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;/*** @Author:wjup* @Date: 2018/9/26 0026* @Time: 15:20*/
@Repository
public interface UserMapper {User Sel(int id);
}
如果使用@Mapper,则是使用MyBatis的注解
package com.example.mapper;import com.example.entity.User;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;/*** @Author:wjup* @Date: 2018/9/26 0026* @Time: 15:20*/
@Mapper//指定这是一个操作数据库的mapper
public interface UserMapper {User Sel(int id);
}
4、在com.example包下面新建service.UserService类,代码如下:
这里记得要加@Service注解,才会被Spring生成Bean,才能在Controller中使用@Authwired注入
然后看到这里注入了个userMapper。
package com.example.service;import com.example.entity.User;
import com.example.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** @Author:wjup* @Date: 2018/9/26 0026* @Time: 15:23*/
@Service
public class UserService {@AutowiredUserMapper userMapper;public User Sel(int id){return userMapper.Sel(id);}
}
5、在com.example包下面新建controller.UserController类,代码如下:
@RestController是@responsebody+@Controller两个注解的合体,一般就拿来直接传json数据。 为什么可以直接传个对象过去呢?这是因为springboot内置了jackson模块,可以在maven的依赖下看到这方面的jar包。
package com.example.controller;import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @Author:wjup* @Date: 2018/9/26 0026* @Time: 14:42*/@RestController
@RequestMapping("/testBoot")
public class UserController {@Autowiredprivate UserService userService;@RequestMapping("getUser/{id}")public String GetUser(@PathVariable int id){return userService.Sel(id).toString();}
}
6、project最终框架结构,如下:
7、启动类修改
下面在启动类里加上注解用于给出需要扫描的mapper文件路径@MapperScan("com.example.mapper")
@MapperScan是MyBatis提供的设置注解。
package com.example;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** 指定所扫的包,会自动扫描指定包下的全部标有@Component的类,并注册成bean,* 当然包括@Component下的子注解@Service,@Repository,@Controller。**/
@MapperScan("com.example.mapper") //扫描的mapper
@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}
8、测试验证
- 最后启动,浏览器输入地址看看吧:http://localhost:8080/testBoot/getUser/1
五、彩蛋
- 最后给个番外篇如何更改启动时显示的字符拼成的字母,就是更改下图标红框的地方
其实很好改,只需要在resources下新建一个txt文件就可以,命名为banner.txt,那这种字符该怎么拼出来呢,下面推荐一个网址,有这种工具,链接传送门:字母转字符。如下:
直接输入要生成的字母,系统会自动转换,然后复制下面转换好的字符到新建的banner.txt文件中,重新启动项目就可以了。
参考文章:
SpringBoot整合Mybatis完整详细版
Springboot与MyBatis简单整合
(一)SpringBoot 整合 MyBatis相关推荐
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例(转)...
SpringBoot整合mybatis.shiro.redis实现基于数据库的细粒度动态权限管理系统实例 shiro 目录(?)[+] 前言 表结构 maven配置 配置Druid 配置mybatis ...
- mybatis plugins_[MyBatis] SpringBoot 整合Mybatis
现在基本上搭建一个简单的工程都是三剑客 springboot+mybatis+redis 之前整合Mybatis 都是按照SSM来,所以,这一次带来SpringBoot+MyBatis 的快速整合 p ...
- SpringBoot整合Mybatis超详细流程
SpringBoot整合Mybatis超详细流程 文章目录 SpringBoot整合Mybatis超详细流程 前言 详细流程 0.引入Mybatis 1.创建数据 2.创建程序目录 3.理解后台访问流 ...
- SpringBoot整合Mybatis(高级)
SpringBoot整合Mybatis(高级) 文章目录 SpringBoot整合Mybatis(高级) 前言 基础环境配置 增删改查 ResultMap 复杂查询 多对一 一对多 动态SQL if ...
- 3、SpringBoot整合MyBatis注解版及配置文件版
目录 1.配置pom.xml 2.配置application.yml 3.配置DruidConfig关联yml的配置文件spring.datasource 4.创建数据库及数据库表结构 5.创建对应的 ...
- SpringBoot整合mybatis进行快速开发
SpringBoot整合mybatis进行数据库操作 1.环境的搭建 pom.xml <!--核心模块,包括自动配置支持.日志和YAML --> <dependencies>& ...
- springboot 整合mybatis实现curd
springboot 整合mybatis pom文件 mvc 架构 application.properties 扩展配置,druid配置类 项目地址: https://github.com/seve ...
- springboot整合mysql5.7_详解SpringBoot整合MyBatis详细教程
1. 导入依赖 首先新建一个springboot项目,勾选组件时勾选Spring Web.JDBC API.MySQL Driver 然后导入以下整合依赖 org.mybatis.spring.boo ...
- SpringBoot整合Mybatis,并实现事务控制
SpringBoot整合Mybatis,并实现事务控制 1. 在pom文件里添加相关maven文件 <parent><groupId>org.springframework.b ...
最新文章
- python 使用 requests 做 http 请求
- 进程和线程(通信方式)、调度算法(6种)
- python 列表操作-切片
- linux 命令 mv 自己的理解
- 三甲医院his系统源码_三甲医院科研管理系统是什么,科研成果包括哪些
- 元素隐式具有 “any” 类型,因为类型“Shared”没有索引签名问题解决思路
- Spring : ListableBeanFactory
- nodejs调用函数和模块
- 为什么有些人电脑,电视,家电坏了首先是换新的,而不是维修?
- 自己动手开发编译器特别篇——用词法分析器解决背诵圣经问题
- 国产环境小卫星数据预处理及简单应用
- java发送hotmail邮件,使用javamail将电子邮件发送到hotmail时遇到问题
- 『Windows Builder』Java Swing期末课设神器
- ones刻录软件怎么用
- 5分钟学会双拼 双拼输入法 最简单的双拼入门教学 图文教程
- 2021年,普通人,如何快速合法地赚到你人生的第一桶金?
- POI-HSSFWorkbook合并单元格边框及文字居中问题
- opencv Fast特征提取函数
- 第157章 SQL函数 WEEK
- 关于 iOS开发者账号提示必须更新绑定手机号的解决方法