搭建spring boot环境
创建项目
一直next到如下图:
给出创建成功的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lm</groupId>
<artifactId>springboot_jpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_jpa</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
给spring boot项目中配置数据源,如下所示:
application.yml(新建项目名为application.properties)
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/lm?serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
# 填写自己的username,password,url和driver驱动信息
jpa:
show-sql: true
# 展示SQL代码到控制台上
properties:
hibernate:
format_sql: true
# 格式化SQL代码在控制台上的显示
实际操作
第一步:创建两个包,分别为entities和repository;前者存放实体类,后者存放实体类的操作类
注:如过学过mybatis的就可以这样理解,entities相当于domain,repository相当于dao;
创建成功后,如下目录结构:
在上述红框中,所创建的包必须在入口程序的同级或下级创建,否则无法扫出bean;(main方法所在文件的同级别或下级别)
创建数据表,如下DDL语言:
-- auto-generated definition
create table user
(
user_id int auto_increment comment '自增id'
primary key,
username varchar(128) default '' not null comment '登录名',
qq_open_id char(32) null comment 'qq官方唯一编号信息',
password char(64) default '' not null comment '登录密码',
user_email varchar(64) default '' not null comment '邮箱',
user_email_code char(13) null comment '新用户注册邮件激活唯一校验码',
is_active enum ('是', '否') default '否' null comment '新用户是否已经通过邮箱激活帐号',
user_sex enum ('保密', '女', '男') default '男' not null comment '性别',
user_qq varchar(32) default '' not null comment 'qq',
user_tel varchar(32) default '' not null comment '手机',
user_xueli enum ('博士', '硕士', '本科', '专科', '高中', '初中', '小学') default '本科' not null comment '学历',
user_hobby varchar(32) default '' not null comment '爱好',
user_introduce text null comment '简介',
create_time int not null comment '创建时间',
update_time int not null comment '修改时间'
)
comment '会员表' charset = utf8;
(这是我从我数据库随便考的一个)
利用Lombok创建实体类user,如下代码:
package com.lm.jpaDemo.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
/**
* @author dell
* @date 2020-12-07
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table (name ="user")
public class User implements Serializable {
private static final long serialVersionUID = 524258970393766098L;
/**
* 自增id
*/
@Column(name = "user_id" )
@Id
private Long userId;
/**
* 登录名
*/
@Column(name = "username" )
private String username;
/**
* qq官方唯一编号信息
*/
@Column(name = "qq_open_id" )
private String qqOpenId;
/**
* 登录密码
*/
@Column(name = "password" )
private String password;
/**
* 邮箱
*/
@Column(name = "user_email" )
private String userEmail;
/**
* 新用户注册邮件激活唯一校验码
*/
@Column(name = "user_email_code" )
private String userEmailCode;
/**
* 新用户是否已经通过邮箱激活帐号
*/
@Column(name = "is_active" )
private String isActive;
/**
* 性别
*/
@Column(name = "user_sex" )
private String userSex;
/**
* qq
*/
@Column(name = "user_qq" )
private String userQq;
/**
* 手机
*/
@Column(name = "user_tel" )
private String userTel;
/**
* 学历
*/
@Column(name = "user_xueli" )
private String userXueli;
/**
* 爱好
*/
@Column(name = "user_hobby" )
private String userHobby;
/**
* 简介
*/
@Column(name = "user_introduce" )
private String userIntroduce;
/**
* 创建时间
*/
@Column(name = "create_time" )
private Long createTime;
/**
* 修改时间
*/
@Column(name = "update_time" )
private Long updateTime;
}
创建UserRepository.java到repository包中,如下所示:
package com.lm.jpaDemo.repositroy;
import com.lm.jpaDemo.entities.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends JpaRepository<User, Integer>, CrudRepository<User, Integer> {
/**
* 自定义的查询方法
* @return
*/
@Query("select u from User u where u.username = ?1")
public User findByName();
/**
* 使用hql进行删除操作
* @param id
*/
@Modifying
@Query("delete from User u where u.userId = ?1")
public void deleteById(Integer id);
/**
* 使用sql进行删除操作
* nativeQuery = true属性标识启用sql操作,即value中写操作将被当作sql语句进行操作,而不是hql语句
* @param id
*/
@Modifying
@Query(value = "delete from User u where u.user_id = ?1", nativeQuery = true)
public void deleteById2(Integer id);
}
直接利用idea快速生成测试类,编写如下所示。
package com.lm.jpaDemo.repositroy;
import com.lm.jpaDemo.entities.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends JpaRepository<User, Integer>, CrudRepository<User, Integer> {
/**
* 自定义的查询方法
* @return
*/
@Query("select u from User u where u.username = ?1")
public User findByName(String name);
/**
* 使用hql进行删除操作;
* 对于DML语句,除了select外,都要带上@Modifying注解,表示开启修改功能;(默认是只读)
* @param id
*/
@Modifying
@Query("delete from User u where u.userId = ?1")
public void deleteById(Integer id);
/**
* 使用sql进行删除操作
* nativeQuery = true属性标识启用sql操作,即value中写操作将被当作sql语句进行操作,而不是hql语句
* @param id
*/
@Modifying
@Query(value = "delete from User u where u.user_id = ?1", nativeQuery = true)
public void deleteById2(Integer id);
}
运行测试类,结果如下所示:
OK。建议读者直接继承JpaRepository和CrudRepository来操作Repository层,并且不要给接口添加@repository注解,因为JpaRepository底层都是继承了Repository接口的,此接口会直接给spring boot的自动配置说此接口可以被spring代理管理。
当然如果你不想继承JpaRepository和CrudRepository,而想写自己的repository操作方法,那么你必须继承Repository接口,表示此接口是一个repository的实体操作类。