mybatics 与jpa

mybatics,dao层接口,mapper:

public interface UserMapper {
@Select("SELECT * FROM T_ROLE WHERE ROLE_NAME = #{role_name}")
User findByName(@Param("role_name") String role_name);
@Insert("INSERT INTO T_ROLE(ROLE_NAME, NOTE) VALUES(#{role_name}, #{note})")
int insert(@Param("role_name") String role_name, @Param("note") String note);
}

jpa,dao层接口,Repository:

@Repository
public interface TSysUserRepository extends JpaRepository<TSysUser, String>, JpaSpecificationExecutor<TSysUser>{ @Query(value = "select tsu from TSysUser tsu where tsu.stAccount=:stAccount ")
TSysUser findLoginUser(@Param("stAccount") String stAccount);
}

jpa,索引参数与命名参数:

(1)索引参数如下所示,索引值从1开始,查询中 ”?X” 个数需要与方法定义的参数个数相一致,并且顺序也要一致 
  //@Modifying 和@Query 这两个annotation一起声明,可定义个性化更新操作,例如只涉及某些字段更新时最为常用
@Modifying
@Query("update User u set u.firstname = ?1 where u.lastname = ?2")
int setFixedFirstnameFor(String firstname, String lastname); (2)命名参数(推荐使用这种方式)
可以定义好参数名,赋值时采用@Param("参数名"),而不用管顺序。如下所示:
public interface UserRepository extends JpaRepository<User, Long> { @Query("select u from User u where u.firstname = :firstname or u.lastname = :lastname")
User findByLastnameOrFirstname(@Param("lastname") String lastname, @Param("firstname") String firstname);
}
上一篇:高级组件——菜单栏JMenuBar


下一篇:BlackVue行车记录仪可让您跟踪其他用户,这是个功能?