public class EmployeeRepositoryTest {
private ApplicationContext applicationContext = null;
private EmployeeRepository employeeRepository = null;
@Before
public void init(){
applicationContext = new AnnotationConfigApplicationContext(SpringConfigNew.class);
employeeRepository = applicationContext.getBean(EmployeeRepository.class);
System.out.println("init...");
}
@After
public void destroy(){
applicationContext=null;
System.out.println("destroy...");
}
@Test
public void findByNametest(){
System.out.println(employeeRepository);
Employee employee = employeeRepository.findByName("test1");
System.out.println(employee);
}
}
@RepositoryDefinition(domainClass = Employee.class,idClass = Integer.class)
interface EmployeeRepository /*extends Repository*/ {//类名 + 主键类型
//获取雇员对象通过名称
Employee findByName(String name);
}
@RepositoryDefinition(domainClass = Employee.class,idClass = Integer.class)
interface EmployeeRepository /*extends Repository*/ {//类名 + 主键类型
//获取雇员对象通过名称
Employee findByName(String name);
//where name like ?% and age < ?
List findByNameStartingWithAndAgeLessThan(String name,Integer age);
//where name like %? and age < ?
List findByNameEndingWithAndAgeLessThan(String name,Integer age);
//where name in () or age < ?
List findByNameInOrAgeLessThan(List name,Integer age);
//where name in () and age < ?
List findByNameInAndAgeLessThan(List name,Integer age);
}
public class EmployeeRepositoryTest {
private ApplicationContext applicationContext = null;
private EmployeeRepository employeeRepository = null;
@Before
public void init(){
applicationContext = new AnnotationConfigApplicationContext(SpringConfigNew.class);
employeeRepository = applicationContext.getBean(EmployeeRepository.class);
System.out.println("init...");
}
@After
public void destroy(){
applicationContext=null;
System.out.println("destroy...");
}
@Test
public void findByNametest(){
System.out.println(employeeRepository);
Employee employee = employeeRepository.findByName("test1");
System.out.println(employee);
}
@Test
public void findByNameStartingWithAndAgeLessThantest(){
List employees = employeeRepository.findByNameStartingWithAndAgeLessThan("test",22);
for(Employee employee:employees){
System.out.println(employee);
}
}
@Test
public void findByNameEndingWithAndAgeLessThantest(){
List employees = employeeRepository.findByNameEndingWithAndAgeLessThan("6",23);
for(Employee employee:employees){
System.out.println(employee);
}
}
@Test
public void findByNameInOrAgeLessThantest(){
List name = new ArrayList<>();
name.add("test1");
name.add("test2");
name.add("test3");
List employees = employeeRepository.findByNameInOrAgeLessThan(name,23);
for(Employee employee:employees){
System.out.println(employee);
}
}
@Test
public void findByNameInAndOrAgeLessThantest(){
List name = new ArrayList<>();
name.add("test1");
name.add("test2");
name.add("test3");
List employees = employeeRepository.findByNameInAndAgeLessThan(name,23);
for(Employee employee:employees){
System.out.println(employee);
}
}
}
在Respository方法中使用,不需要遵循查询方法命令规则
只需要将@Query定义在Respository中的方法之上即可
命名参数及索引参数的使用
本地查询
EmployeeRepository接口
//Query注解
@Query("select o from Employee o where id = (select max(id) from Employee t1)")//注意这里是根据类来查 不是表
Employee getEmployeeByMaxId();
/*
使用占位符进行参数绑定
*/
@Query("select o from Employee o where o.name = ?1 and o.age = ?2")
List listEmployeeByNameAndAge(String name,Integer age);
/*
使用命名参数进行参数绑定
*/
@Query("select o from Employee o where o.name = :name and o.age = :age")
List listEmployeeByNameAndAge2(@Param("name")String name,@Param("age")Integer age);
/*
自定义查询SQL,like,占位符进行参数绑定
*/
@Query("select o from Employee o where o.name like %?1%")
List listEmployeeByLikeName(String name);
/*
自定义查询SQL,like,占位符进行参数绑定
*/
@Query("select o from Employee o where o.name like %:name%")
List listEmployeeByLikeName2(@Param("name") String name);
/*
使用原生态SQL查询
*/
@Query(nativeQuery = true,value="select count(1) from employee")//这才是表查询 默认支持原生态为false 改成true
long getCount();
@Test
public void getEmployeeByMaxIdtest(){
Employee employee = employeeRepository.getEmployeeByMaxId();
System.out.println(employee);
}
@Test
public void listEmployeeByNameAndAgetest(){
List employees = employeeRepository.listEmployeeByNameAndAge("test6",22);
for(Employee employee:employees){
System.out.println(employee);
}
}
@Test
public void listEmployeeByNameAndAge2test(){
List employees = employeeRepository.listEmployeeByNameAndAge2("test6",22);
for(Employee employee:employees){
System.out.println(employee);
}
}
@Test
public void listEmployeeByLikeNametest(){
List employees = employeeRepository.listEmployeeByLikeName("test");
for(Employee employee:employees){
System.out.println(employee);
}
}
@Test
public void listEmployeeByLikeName2test(){
List employees = employeeRepository.listEmployeeByLikeName2("6");
for(Employee employee:employees){
System.out.println(employee);
}
}
@Test
public void getCounttest(){
System.out.println(employeeRepository.getCount());
}
@Modifying注解使用
@Modifying结合@Query注解执行更新操作
@Transaction在Spring Data中的使用
@Modifying
@Query("update Employee o set o.age=:age where o.id=:id ")
void update(@Param("id")Integer id,@Param("age")Integer age);
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
@Transactional
public void update(Integer id,Integer age){
employeeRepository.update(id,age);
}
}
public class EmployeeServiceTest {
private ApplicationContext ctx = null;
private EmployeeService employeeService = null;
@Before
public void init(){
ctx = new AnnotationConfigApplicationContext(SpringConfigNew.class);
employeeService = ctx.getBean(EmployeeService.class);
System.out.println("init...");
}
@After
public void destroy(){
ctx=null;
System.out.println("destroy...");
}
@Test
public void update(){
employeeService.update(1,50);
}
}