【极光系列】springboot集成hibernate
gitee地址
直接下载可用 https://gitee.com/shawsongyue/aurora.git
模块:aurora_hibernate
mysql安装教程
参考我另外一篇文章,直接下载安装 https://blog.csdn.net/weixin_40736233/article/details/135582926?spm=1001.2014.3001.5501
集成hibernate步骤
tips:包结构如图
1.处理数据库表
CREATE DATABASE aurora;
CREATE TABLE `tianchi_resource` (
`id` bigint NOT NULL AUTO_INCREMENT 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 AUTO_INCREMENT=12 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 (12, 'colorUI资源包', 'http://baidu.com', 'tag', 'red', 'colorUI资源包一键下载可用', 'aurora', '1', '2024-01-14 15:26:12', '2024-01-14 15:26:12');
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 (13, 'idea开开发工具资源包', 'http://baidu.com', 'tag', 'red', 'idea开开发工具资源包一键下载可用', 'aurora', '1', '2024-01-14 15:26:12', '2024-01-14 15:26:12');
2.处理依赖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_hibernate</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>
</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>
<!-- 集成spring data jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</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>
3.处理配置application.yml
#服务配置
server:
#端口
port: 7002
#spring配置
spring:
#应用配置
application:
#应用名
name: aurora_hibernate
#数据源配置
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: xiaosongyue.1997
#hibernate配置
jpa:
#配置数据库引擎,不配置则默认为myisam引擎
database-platform: org.hibernate.dialect.MySQL8Dialect
show-sql: true
database: mysql
hibernate:
#create: 每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。
#create-drop :每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。
#update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会。
#validate :每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。
#none : 什么都不做。
ddl-auto: none
#日志设置
logging:
#日志级别
level:
springfox: error
com.example.hibernate: debug
4.创建对应的类
tips:包结构如图
(1)主启动类
package com.aurora;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author 浅夏的猫
* @description 主启动类
* @date 22:46 2024/1/13
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
(2)实体类
tips:* @Entity注 解,表示这是一个JPA实体类* @Table 注解用于指定实体类映射到的数据库表名* @Id注 解用于指定实体类的主键* @GeneratedValue 注解指定了主键的生成策略* @Column 注解用于指定实体类属性映射到的数据库列名* @Transient 映射数据表和对象关系时候就不会报在表中不存在该字段* @CreationTimestamp 插入数据时自动更新时间
package com.aurora.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
/**
* @author 浅夏的猫
* @description 资源实体类
*
* @Entity注 解,表示这是一个JPA实体类
* @Table 注解用于指定实体类映射到的数据库表名
* @Id注 解用于指定实体类的主键
* @GeneratedValue 注解指定了主键的生成策略
* @Column 注解用于指定实体类属性映射到的数据库列名
* @Transient 映射数据表和对象关系时候就不会报在表中不存在该字段
* @CreationTimestamp 插入数据时自动更新时间
*
* @date 22:42 2024/1/13
*/
@Entity
@Table(name = "tianchi_resource")
public class ResourceEntity implements Serializable {
private static final long serialVersionUID = 1L;
//资源id
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
//资源名称
@Column(name = "resource_name")
private String resourceName;
//资源路径
@Column(name = "resource_path")
private String resourcePath;
//资源图标
@Column(name = "resource_icon")
private String resourceIcon;
//资源图标颜色
@Column(name = "resource_icon_color")
private String resourceIconColor;
//资源描述
@Column(name = "resource_describe")
private String resourceDescribe;
//资源类型
@Column(name = "resource_type")
private String resourceType;
//资源作者
@Column(name = "resource_author")
private String resourceAuthor;
//资源创建时间
@Column(name = "create_time")
@CreationTimestamp
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime;
//资源更新时间
@UpdateTimestamp
@Column(name = "update_time")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date updateTime;
//查询结束时间
@Transient
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;
}
@Override
public 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)创建映射类
package com.aurora.mapper;
import com.aurora.entity.ResourceEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* @author 浅夏的猫
* @description 资源mapper操作类
* @date 22:45 2024/1/13
*/
@Repository
public interface ResourceMapper extends JpaRepository<ResourceEntity, Long> {
}
(4)创建控制类
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 {
@Resource
private 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.findAll();
}
}
(5)启动应用,控制台访问
tips: 访问地址http://localhost:7002/resource/list