ityouknow / blog-comments

15 stars 0 forks source link

springboot(十五):springboot+jpa+thymeleaf增删改查示例 - ityouknow's Blog #81

Open ityouknow opened 6 years ago

ityouknow commented 6 years ago

http://www.ityouknow.com/springboot/2017/09/23/spring-boot-jpa-thymeleaf-curd.html

这篇文章介绍如何使用jpa和thymeleaf做一个增删改查的示例。

LainNetWork commented 6 years ago

感谢大佬!

ghost commented 6 years ago

大佬,上面在写UserRepository接口时里面的“Long deleteById(Long id);”一直报错怎么解决?提示说:无效的源查询,实体类中没有这个属性

shanzhaozhen commented 6 years ago

中文乱码怎么处理啊

lizhenzhaofirst commented 6 years ago

现在的技术博客有很多的流派,有的喜欢分析源码,有的倾向于底层原理,我最喜欢写这种小而美的示例,方便自己方便他人。<=>水陆草木之花,可爱者甚蕃。晋陶渊明独爱菊。自李唐来,世人甚爱牡丹。予独爱莲之出淤泥而不染,濯清涟而不妖,中通外直,不蔓不枝,香远益清,亭亭净植,可远观而不可亵玩焉。。。。。。。语法何其相似。嗯,一个伟大的发现。哈哈

ChasingLight commented 6 years ago

@lizhenzhaofirst 现在的技术博客有很多的流派,有的喜欢分析源码,有的倾向于底层原理,我最喜欢写这种小而美的示例,方便自己方便他人。<=>水陆草木之花,可爱者甚蕃。晋陶渊明独爱菊。自李唐来,世人甚爱牡丹。予独爱莲之出淤泥而不染,濯清涟而不妖,中通外直,不蔓不枝,香远益清,亭亭净植,可远观而不可亵玩焉。。。。。。。语法何其相似。嗯,一个伟大的发现。哈哈

果真其实大部分的程序员都是很有趣的,很有文采的,哈哈。顺带把自己也夸了

ChasingLight commented 6 years ago

感谢作者,制作分享!如果丰满上分页部分会更好。

q379811362 commented 6 years ago

@Umizhangs 大佬,上面在写UserRepository接口时里面的“Long deleteById(Long id);”一直报错怎么解决?提示说:无效的源查询,实体类中没有这个属性 因为 作者可能写错了。继承了JpaRepository后。UserRepository里不用写任何东西。默认都有增save删delete改save查findxxxx的方法。删除的方法名是delete(xxxxx)。如果你要写就要遵守jpa命名规范。命名规范基本都是针对查询的。只用过findById。你这个deleteById还是头一次见。应该是不符合jpa命名规则。无法给你自动生成SQL。你要是还想用这个方法名的话deleteById。那你就在上边加@Query("delete from XXXXX where id= :myId") deleteById(@Param("myId") long myId); 这样写。

q379811362 commented 6 years ago

@shanzhaozhen 中文乱码怎么处理啊

@RequestMapping( 给这里加个这试试 produces=“charset=UTF-8”)。

TangJJJJ commented 6 years ago

@shanzhaozhen 中文乱码怎么处理啊

TangJJJJ commented 6 years ago

乱码给properties文件数据库的url后面加charset=UTF-8这个属性

Long id没查询源的那个水友你可以看看你表中的id是不是自增的,还有在实体类中类型改成Long,mysql中没有Long

foolself commented 5 years ago

一个疑问,jpa中 增删改都有实现了,为什么还要再写 server serverImpl呢

shanzhaozhen commented 5 years ago

一个疑问,jpa中 增删改都有实现了,为什么还要再写 server serverImpl呢

jpa 的Respositry相当于dao层, server层是用来处理业务的逻辑,可能同时用上几个类的Respositry,还有其他代码

DJun5 commented 5 years ago

感谢分享

weixuchao commented 5 years ago

Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract void com.example.springboot.dao.UserRepository.delete(long)! No property delete found for type User!

xuwei33 commented 5 years ago

为什么 我User findById (Long id);报错,不是同一个类型。他里面的方法是 Optional findById(ID var1),就是重写不了。求大佬i解释

aloneDesperado commented 5 years ago

return "redirect:/list" 是重定向到list吧

zhou-zhicheng commented 4 years ago

请教一个问题,在user实体类去掉age不能为空的约束后,增加用户的时候如果不填年龄会报如下错误:nested exception is java.lang.NumberFormatException: For input string: "";也就是说不填年龄的时候会报空字符串转int的错误,请问这种情况该如何处理呢?谢谢

zhou-zhicheng commented 4 years ago

@ityouknow 请教一个问题,在user实体类去掉age不能为空的约束后,增加用户的时候如果不填年龄会报如下错误:nested exception is java.lang.NumberFormatException: For input string: "";也就是说不填年龄的时候会报空字符串转int的错误,请问这种情况该如何处理呢?谢谢

q379811362 commented 4 years ago

@ityouknow 请教一个问题,在user实体类去掉age不能为空的约束后,增加用户的时候如果不填年龄会报如下错误:nested exception is java.lang.NumberFormatException: For input string: "";也就是说不填年龄的时候会报空字符串转int的错误,请问这种情况该如何处理呢?谢谢

你既然去掉了age的非空约束。是不是应该更新一下表?让表里age可以为空。你这个问题证明age是int数字类型。你为啥还要传个Sting类型的空字符?不传参数或者传null不好么?

zhou-zhicheng commented 4 years ago

@ityouknow 请教一个问题,在user实体类去掉age不能为空的约束后,增加用户的时候如果不填年龄会报如下错误:nested exception is java.lang.NumberFormatException: For input string: "";也就是说不填年龄的时候会报空字符串转int的错误,请问这种情况该如何处理呢?谢谢

你既然去掉了age的非空约束。是不是应该更新一下表?让表里age可以为空。你这个问题证明age是int数字类型。你为啥还要传个Sting类型的空字符?不传参数或者传null不好么?

表里的非空约束自然是去掉了。age的传参如何控制?

DJun5 commented 4 years ago

这个程序是我初学JPA的时候写的,已经过去很久了,也实在想不起来了,很抱歉帮不上你的忙

------------------ 原始邮件 ------------------ 发件人: "zhou-zhicheng"<notifications@github.com>; 发送时间: 2019年11月25日(星期一) 下午2:27 收件人: "ityouknow/blog-comments"<blog-comments@noreply.github.com>; 抄送: "邓俊"<903131009@qq.com>;"Comment"<comment@noreply.github.com>; 主题: Re: [ityouknow/blog-comments] springboot(十五):springboot+jpa+thymeleaf增删改查示例 - ityouknow's Blog (#81)

@ityouknow 请教一个问题,在user实体类去掉age不能为空的约束后,增加用户的时候如果不填年龄会报如下错误:nested exception is java.lang.NumberFormatException: For input string: "";也就是说不填年龄的时候会报空字符串转int的错误,请问这种情况该如何处理呢?谢谢

你既然去掉了age的非空约束。是不是应该更新一下表?让表里age可以为空。你这个问题证明age是int数字类型。你为啥还要传个Sting类型的空字符?不传参数或者传null不好么?

表里的非空约束自然是去掉了。age的传参如何控制?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.