1、用Cat2Repository继承extends Repository<Cat, Integer>,此Repository为空接口,需要自己定义方法。
注:1、“查询”方法名,必须以get、find、read开头。
2、涉及查询条件时,条件的属性用条件关键字连接,要注意的是条件属性以首字母大写,如By。
3、图为根据Name查找,故方法名为findByName(B必须大写),传入参数为String类型。若想根据ID查找,则方法名应为findById,传入参数为int类型。
4、public List<Cat> findByname(String mm);加上List则可以返回多项数值。否则会报错,返回元素过多。
5、之所以方法名有规定,是因为此接口用反射的机制编写,内部实现其方法。
2、接口写完后,在Service中使用。在private Cat2Repository cat2Re上使用注解@Resource引用Cat2Repository接口。并编写方法,publicList<Cat> findName(String nam)能返回多项值。
3、最后需要在Controller中使用,在private CatService catser;使用注解@Resource引用CatService。并编写方法,public List<Cat>finde(String namee) 能返回多项值。