Springboot 系列(十)使用 Spring data jpa 访问数据库 (3)

根据 Id 查询也没有问题。

Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.birthday as birthday3_0_0_, user0_.password as password4_0_0_, user0_.skills as skills5_0_0_, user0_.username as username6_0_0_ from user user0_ where user0_.id=? User(id=1, username=Darcy, password=123, age=18, birthday=2019-01-12 21:02:30.0, skills=Go)

更新操作也是正常输出 SQL ,没有任何异常。

Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.birthday as birthday3_0_0_, user0_.password as password4_0_0_, user0_.skills as skills5_0_0_, user0_.username as username6_0_0_ from user user0_ where user0_.id=? 18 Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.birthday as birthday3_0_0_, user0_.password as password4_0_0_, user0_.skills as skills5_0_0_, user0_.username as username6_0_0_ from user user0_ where user0_.id=? Hibernate: update user set age=?, birthday=?, password=?, skills=?, username=? where id=?

最后一个是自定义查询操作,上面三个方法的输出中,Darcy 用户对应的年龄是 18,在经过更新加1 之后应该变为19,下面是自定义查询的结果。

Hibernate: select user0_.id as id1_0_, user0_.age as age2_0_, user0_.birthday as birthday3_0_, user0_.password as password4_0_, user0_.skills as skills5_0_, user0_.username as username6_0_ from user user0_ where user0_.username=? and user0_.password=? User(id=1, username=Darcy, password=123, age=19, birthday=2019-01-12 21:02:30.0, skills=Go)

可见是没有任何问题的。
文章代码已经上传到 GitHub。
测试代码中使用了一些 JDK8 的特性,如 Optional 类的使用,以后会单独写一部分关于 JDK 新特性的文章,欢迎扫码关注公众号。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wppsfz.html