Springboot整合hibernate
1:引入依赖
<!--hibernate依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </dependency>
2:引入其他可选依赖,此处使用数据库为mysql,所以引入mysql,返回数据 引入web启动器
<dependencies> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> </dependency> <!--hibernate依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--web启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
3:新增配置
server.port=8016 spring.datasource.url=jdbc:mysql://192.168.197.170:3306/test-hibernate?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=123456 spring.jpa.database = MYSQL spring.jpa.show-sql = true spring.jpa.hibernate.ddl-auto = update spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
4:实体类配置
package com.text.jpa.demo.entity; import lombok.Data; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import java.io.Serializable; import java.util.Date; /** * * * @date 2022-05-27 13:24:48 */ @Data @Entity @Table(name = "test_user") public class TestUserEntity implements Serializable { private static final long serialVersionUID = 1L; @Id private Integer id; private String mark; private String name; private String pswd; private String phone; private Date createtime; private Date updatetime; }
5:写dao接口 来继承jpa的方法,就是能实现基本的增删改查了
package com.text.jpa.demo.dao; import com.text.jpa.demo.entity.TestUserEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; /** * * * @date 2022-05-27 13:24:48 */ @Repository public interface TestUserDao extends JpaRepository<TestUserEntity, Long> { }
6:controller层,调用一下,返回数据
/** * 列表 */ @GetMapping("/list") public Object list() { List<TestUserEntity> list = testUserDao.findAll(); return list; }
7:启动类上需要启动JPA
@SpringBootApplication @EnableJpaRepositories public class TestJPAApplication { public static void main(String[] args) { SpringApplication.run(TestJPAApplication.class, args); } }
8:测试
控制台打印