首页  

springboot jpa 实例     所属分类 JPA 浏览量 105
springboot2  2.2.1.RELEASE

org.springframework.boot:spring-boot-starter-data-jpa

@Entity
@DynamicUpdate
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private Integer age;
    private String address;
    private String email;
    ...
}


public interface UserRepository extends JpaRepository< User, Long > {
    User findByName(@Param("name") String name);
    @Query("from User u where u.email=:email")
    User findByEmail(@Param("email") String email);
    void deleteByName(@Param("name") String name);
}



server.port = 8030
spring.datasource.initialization-mode = ALWAYS

spring.datasource.url = jdbc:h2:mem:test
spring.datasource.driver-class-name = org.h2.Driver
spring.datasource.username = sa
spring.datasource.password =

spring.datasource.schema = classpath:sql/schema-h2.sql
spring.datasource.data = classpath:sql/data-h2.sql


/src/main/resources/logback.xml
日志级别设置为 debug , 查看sql 

io.github.dunwu.springboot.data.UserController

http://127.0.0.1:8030/hello
http://127.0.0.1:8030/insert?name=user01&age=7&address=address001&email=email001

http://127.0.0.1:8030/autoBatchInsert

http://127.0.0.1:8030/findById?id=1

http://127.0.0.1:8030/findByName?name=name_1

http://127.0.0.1:8030/findAllByPage?page=0&size=3


http://127.0.0.1:8030/update?id=1&name=user1_003
http://127.0.0.1:8030/findById?id=1


一些注意点 动态更新 数据对象 加 @DynamicUpdate , 先查出来 再设置修改的字段 http://127.0.0.1:8030/update?id=4&name=user4_004 repository.save 不传 id ,则insert 分页 page 从0开始 @RequestMapping("/findAllByPage") public Object findAllByPage(int page,int size) { PageRequest pageRequest = PageRequest.of(page, size); return repository.findAll(pageRequest); }
https://gitee.com/dyyx/work2024/tree/master/demo/spring-boot-data-jpa

上一篇     下一篇
hutool 国密算法工具类 SmUtil

JWT(JSON Web Token)

hutool JWT 例子

Java各种数据对象转换框架

java8的日期时间

java.time.Instant 说明