【极光系列】SpringBoot集成Mybatis
一.gitee地址
浅夏的猫 @shawsongyue
直接下载可用 https://gitee.com/shawsongyue/aurora.git
二.mysql安装教程
详细参考我的另外一遍博客: https://blog.csdn.net/weixin_40736233/article/details/135582926?spm=1001.2014.3001.5501
三.集成mybatis步骤
1. 处理依赖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.xsy</groupId><artifactId>aurora_mybatis</artifactId><version>1.0-SNAPSHOT</version><!--基础SpringBoot依赖--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.5.RELEASE</version></parent><!--属性设置--><properties><!--java_JDK版本--><java.version>1.8</java.version><!--maven打包插件--><maven.plugin.version>3.8.1</maven.plugin.version><!--编译编码UTF-8--><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!--输出报告编码UTF-8--><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><!--mysql版本--><mysql-connector-java.version>8.0.21</mysql-connector-java.version><!--json数据格式处理工具--><fastjson.version>1.2.75</fastjson.version><!--mybatis--><mybatis.version>3.5.3</mybatis.version></properties><!--通用依赖--><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- Lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!-- json --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>${fastjson.version}</version></dependency><!--mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql-connector-java.version}</version><scope>runtime</scope></dependency><!-- 集成MyBatis --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.2</version></dependency></dependencies><!--编译打包--><build><finalName>${project.name}</finalName><!--资源文件打包--><resources><resource><directory>src/main/resources</directory></resource><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource></resources><!--插件统一管理--><pluginManagement><plugins><!--maven打包插件--><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring.boot.version}</version><configuration><fork>true</fork><finalName>${project.build.finalName}</finalName></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin><!--编译打包插件--><plugin><artifactId>maven-compiler-plugin</artifactId><version>${maven.plugin.version}</version><configuration><source>${java.version}</source><target>${java.version}</target><encoding>UTF-8</encoding><compilerArgs><arg>-parameters</arg></compilerArgs></configuration></plugin></plugins></pluginManagement></build><!--配置Maven项目中需要使用的远程仓库--><repositories><repository><id>aliyun-repos</id><url>https://maven.aliyun.com/nexus/content/groups/public/</url><snapshots><enabled>false</enabled></snapshots></repository></repositories><!--用来配置maven插件的远程仓库--><pluginRepositories><pluginRepository><id>aliyun-plugin</id><url>https://maven.aliyun.com/nexus/content/groups/public/</url><snapshots><enabled>false</enabled></snapshots></pluginRepository></pluginRepositories></project>
2. 处理数据库表
登录mysql数据库,创建表 tianchi_resource,并插入数据
CREATE TABLE `tianchi_resource` (`id` bigint NOT NULL COMMENT '自增主键',`resource_name` varchar(128) DEFAULT NULL COMMENT '资源名称',`resource_path` varchar(255) DEFAULT NULL COMMENT '资源路径',`resource_icon` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '资源图标',`resource_icon_color` varchar(32) DEFAULT NULL COMMENT '资源图标颜色',`resource_describe` varchar(255) DEFAULT NULL COMMENT '资源描述',`resource_author` varchar(16) DEFAULT NULL COMMENT '资源作者',`resource_type` varchar(16) DEFAULT NULL COMMENT '资源类型',`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;-- 插入数据验证
INSERT INTO `aurora`.`tianchi_resource` (`id`, `resource_name`, `resource_path`, `resource_icon`, `resource_icon_color`, `resource_describe`, `resource_author`, `resource_type`, `create_time`, `update_time`) VALUES (1, 'colorUI资源包', 'http://baidu.com', 'url(https://ossweb-img.qq.com/images/lol/web201310/skin/big10001.jpg)', NULL, 'uniapp的ui页面资源包,免费提供,uniapp的ui页面资源包,免费提供,uniapp的ui页面资源包,免费提供', '浅夏的猫', NULL, '2024-01-06 00:50:33', '2024-01-07 23:56:58');
3. 处理配置application.yml
#服务配置
server:#端口port: 7001#spring配置
spring:#应用配置application:#应用名name: aurora_mybatis#数据源配置datasource:#驱动driver-class-name: com.mysql.cj.jdbc.Driver#连接地址url: jdbc:mysql://127.0.0.1:3306/aurora?serverTimezone=UTC&characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true#数据库用户username: root#数据库密码password: yourpassword#mybatis配置
mybatis:#mapper.xml文件扫描路径mapper-locations: classpath*:mapper/*Mapper.xml#实体类扫描路径,配置这个以后mapper,xml里面写的实体类不需要全路径type-aliases-package: com.aurora.entity#mybatis拓展配置configuration:#设置下划线自动转换为大写字母map-underscore-to-camel-case: true
4.创建对应的类
(1)主启动类
tips:Application.class 类 springboot程序启动必备
package com.aurora;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author 浅夏的猫* @description 主启动类* @date 22:46 2024/1/13*/
@SpringBootApplication
@MapperScan("com.aurora.mapper")
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
(2)实体类
tips:ResourceEntity.class 类 创建与数据库表对应的实体类,后续映射需要使用
package com.aurora.entity;import com.fasterxml.jackson.annotation.JsonFormat;import java.io.Serializable;
import java.util.Date;/*** @author 浅夏的猫* @description 资源实体类* @date 22:42 2024/1/13*/
public class ResourceEntity implements Serializable {private static final long serialVersionUID = 1L;//资源idprivate Long id;//资源名称private String resourceName;//资源路径private String resourcePath;//资源图标private String resourceIcon;//资源图标颜色private String resourceIconColor;//资源描述private String resourceDescribe;//资源类型private String resourceType;//资源作者private String resourceAuthor;//资源创建时间@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")private Date createTime;//资源更新时间@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")private Date updateTime;//查询结束时间private Date endTime;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getResourceName() {return resourceName;}public void setResourceName(String resourceName) {this.resourceName = resourceName;}public String getResourcePath() {return resourcePath;}public void setResourcePath(String resourcePath) {this.resourcePath = resourcePath;}public String getResourceIcon() {return resourceIcon;}public void setResourceIcon(String resourceIcon) {this.resourceIcon = resourceIcon;}public String getResourceIconColor() {return resourceIconColor;}public void setResourceIconColor(String resourceIconColor) {this.resourceIconColor = resourceIconColor;}public String getResourceDescribe() {return resourceDescribe;}public void setResourceDescribe(String resourceDescribe) {this.resourceDescribe = resourceDescribe;}public String getResourceType() {return resourceType;}public void setResourceType(String resourceType) {this.resourceType = resourceType;}public String getResourceAuthor() {return resourceAuthor;}public void setResourceAuthor(String resourceAuthor) {this.resourceAuthor = resourceAuthor;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}public Date getUpdateTime() {return updateTime;}public void setUpdateTime(Date updateTime) {this.updateTime = updateTime;}public Date getEndTime() {return endTime;}public void setEndTime(Date endTime) {this.endTime = endTime;}@Overridepublic String toString() {return "ResourceEntity{" +"id=" + id +", resourceName='" + resourceName + '\'' +", resourcePath='" + resourcePath + '\'' +", resourceIcon='" + resourceIcon + '\'' +", resourceIconColor='" + resourceIconColor + '\'' +", resourceDescribe='" + resourceDescribe + '\'' +", resourceType='" + resourceType + '\'' +", resourceAuthor='" + resourceAuthor + '\'' +", createTime=" + createTime +", updateTime=" + updateTime +", endTime=" + endTime +'}';}
}
(3)创建映射类
tips:映射类主要用于编写操作sql的接口方法
package com.aurora.mapper;import com.aurora.entity.ResourceEntity;
import org.apache.ibatis.annotations.Mapper;import java.util.List;/*** @author 浅夏的猫* @description 资源mapper操作类* @date 22:45 2024/1/13*/
@Mapper
public interface ResourceMapper {/*** @return java.util.List<com.aurora.entity.ResourceEntity>* @description* @author 浅夏的猫* @datetime 23:19 2024/1/13*/List<ResourceEntity> list();
}
(4)创建sql映射文件
tips: 每一个映射类对应一个mapper映射文件,在resources的mapper包下写映射文件
<?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.aurora.mapper.ResourceMapper"><select id="list" resultType="ResourceEntity">select * from tianchi_resource</select></mapper>
(5)创建控制类
tips: 控制类用于浏览器或前端访问使用
package com.aurora.controller;import com.aurora.entity.ResourceEntity;
import com.aurora.mapper.ResourceMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;
import java.util.List;/*** @author 浅夏的猫* @description 资源controller* @date 22:38 2024/1/13*/
@RestController
@RequestMapping("/resource")
public class ResourceController {@Resourceprivate ResourceMapper resourceMapper;/*** @return java.util.List<com.aurora.entity.ResourceEntity>* @description 查询全部资源数据列表* @author 浅夏的猫* @date 22:40 2024/1/13*/@RequestMapping("/list")public List<ResourceEntity> list() {return resourceMapper.list();}
}
(5)启动应用,控制台访问
tips: 访问地址http://localhost:7001/resource/list