您的位置:首页 > 教育 > 培训 > SpringBoot接入JPA连接数据库H2或MySQL例子

SpringBoot接入JPA连接数据库H2或MySQL例子

2024/10/6 17:22:32 来源:https://blog.csdn.net/shenzhenNBA/article/details/140574838  浏览:    关键词:SpringBoot接入JPA连接数据库H2或MySQL例子

一,JPA相关的常用注解和对象

@Entity,用于实体类声明语句之前,‌指出该Java类为实体类,‌将映射到指定的数据库表;@Table,当实体类与其映射的数据库表名不同名时需要使用。‌该标注与@Entity标注并列使用,
‌置于实体类声明语句之前,‌用于指明数据库的表名。‌此外,‌@Table标注还有catalog和schema选项,‌
用于设置表所属的数据库目录或模式,‌通常为数据库名;@Basic:‌用于属性到表字段的映射。‌如果get方法前没加注解,‌默认是该注解。‌fetch属性
表示该属性的读取策略,‌EAGER表示主支抓取,‌LAZY表示延迟加载。‌
optional属性表示该属性是否允许为空,‌默认值为true。‌@Id,用于将实体类的属性映射为主键;
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "xx_seq"),指定主键生成策略,参见GenerationType 枚举类;
@SequenceGenerator(name = "xx_seq", sequenceName = "xx_seq"),
@Column(name="xxx"),位于属性前或get方法前,‌通常用于属性名与映射的表列名不同时,
‌name属性可以指定表名,‌还有unique、‌nullable、‌length等属性指定表列的属性;@Transient,表示当前类属性无需映射到数据库表中字段;@Temproal,主要针对 Date 类型的属性使用,可以通过该注解指定时间的精度,有
三种精确度:DATE,TIME,TIMESTAMP三种精确度:DATE,TIME,TIMESTAMP,
分别表示精确到日期,精确到时分秒,精确到日期+时分秒;EntityManagerFactory,类似与 hibernate 的 SessionFactory;EntityManager,是JPA中用于增删改查的接口,‌它充当了内存中的Java对象和数据库数据存储之间的桥梁;EntityTransaction,JPA事务处理相关,根据EntityManager对象建立;

二,SpringBoot接入JPA连接H2或MySQL例子

1,首先,在pom.xml中添加Spring Data JPA和数据库驱动的依赖:

<dependencies><!-- Spring Boot Starter Data JPA --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- 数据库驱动,以H2数据库为例 --><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency>
</dependencies>
<!-- 其它依赖省略 -->

2,然后,在 application.properties 或 application.yml 中配置数据库连接和JPA属性:

# application.properties
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
#下面两行是连接mysql数据库
#spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=sa
spring.datasource.password=spring.jpa.database-platform=org.hibernate.dialect.H2Dialect# 下面一行是使用mysql数据库
#spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect#下面一行表示每次运行应用程序时更新数据库模式,
#即会根据java实体类相应字段的变化情况更新DB中相关表的字段,
#值除了update外还有create,但推荐使用 update,因create会删除对应表然后再新创建表
spring.jpa.hibernate.ddl-auto=update#下面一行表示在控制台中显示执行的SQL语句
spring.jpa.show-sql=true

3,创建一个实体类User:

import javax.persistence.*;@Entity
public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;@Column(nullable = false)private String name;@Column(nullable = false)private Integer age;// 更多字段增加,标准的getter和setter方法省略
}

4,创建一个继承自JpaRepository的接口UserRepository:

import org.springframework.data.jpa.repository.JpaRepository;@Repository
public interface UserRepository extends JpaRepository<User, Long> {/** 根据名字进行模糊查询返回对象结果列表,其中 ?1 代表传递给查询方法的第一个参数*/@Query("SELECT u FROM User u WHERE u.name LIKE %?1%")List<User> findByUserName(String text);/** 根据名字和年龄查找, 其中 :xxx 和 注解@Param("xxx")中指定的名称要对应上,nativeQuery=true, 表示采用原生SQL语句方式编写查询*/@Query(nativeQuery=true, value="select * from user where name = :uname and age = :uage")List<User> queryByCondition(@Param("uname") String uname, @Param("uage") Integer uage);}

5,创建一个服务类UserService:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserService {@Autowiredprivate UserRepository userRepository;@PersistenceUnitprivate EntityManagerFactory entityManagerFactory;// 查询public List<User> listAllUsers() {return userRepository.findAll();}// 模糊查询public List<User> findByUserName(String userName) {return userRepository.findByUserName(userName);}// 精确查询public User getUserById(Long id) {return userRepository.findById(id).orElse(null);}// 保存(新增或更新)public User saveUser(User user) {return userRepository.save(user);}// 精确删除public void deleteEntity(Long userId) {EntityManager entityManager = entityManagerFactory.createEntityManager();EntityTransaction transaction = null;try {// 开启事务transaction = entityManager.getTransaction();transaction.begin();// 通过ID查询到实体User entityToDelete = entityManager.find(User.class, id);// 删除实体entityManager.remove(entityToDelete);// 提交事务transaction.commit();			} catch(Exception e) {if (transaction != null && transaction.isActive()) {transaction.rollback();}// 处理异常e.printStackTrace();} finally {entityManager.close();}}//精确批量删除public void deleteEntity(Class<?> entityClass, String fieldName, List<?> fieldValues) {EntityManager entityManager = entityManagerFactory.createEntityManager();EntityTransaction transaction = null;try {// 开启事务transaction = entityManager.getTransaction();transaction.begin();Query query = entityManager.createQuery("delete from " + entityClass.getSimpleName() + " e where e." + fieldName + " in :fieldValues");query.setParameter("fieldValues", fieldValues);int delNum = query.executeUpdate();// 提交事务transaction.commit();} catch(Exception e) {if (transaction != null && transaction.isActive()) {transaction.rollback();}// 处理异常e.printStackTrace();} finally {entityManager.close();}	}	}

6,controller 层省略

@RestController
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;/** 通过用户名称查询用户列表 */@GetMapping("/query")public List<User> queryUser(@RequestParam("name") String name){List<User> users = userService.findByUserName(name);return users;}}

7,最后,创建一个启动类SpringBootJpaApplication:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class SpringBootJpaApplication {public static void main(String[] args) {SpringApplication.run(SpringBootJpaApplication.class, args);}
}

8,部分单元测试(对应的包名相应修改):

import com.xx.yy.kk.dao.UserRepository;
import com.xx.yy.kk.model.User;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.util.Optional;@RunWith(SpringRunner.class)
@SpringBootTest
public class UserTest {@Autowiredprivate UserRepository userRepository;@Beforepublic void before() {User user = new User();user.setId(1L);user.setName("张三");        userRepository.save(user);user = new User();user.setId(3L);user.setName("李四");        userRepository.save(user);user.setId(5L);user.setName("王五");        userRepository.save(user);}@Testpublic void testAdd() {User user = new User();user.setId(2L);user.setName("老六");        userRepository.save(user);User = new User();user.setId(4L);user.setName("王七");        userRepository.save(user);}@Testpublic void testQuery() {String userName="张三";List<User> users = userRepository.findByUserName(zhangsan);//...more...}@Afterpublic void after() {userRepository.deleteById(1L);userRepository.deleteById(3L);userRepository.deleteById(5L);}}

部分代码需要补全或结合自己的情况修改,这里仅展示SpringBoot接入JPA连接数据库H2或MySQL例子的过程,谢谢拍砖讨论...

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com