SpringDataJPA方法命名规范

SpringDataJPA配置及其方法命名规范

示例:

SpringDataJPA方法命名规范_第1张图片
SpringDataJPA方法命名规范_第2张图片
SpringDataJPA方法命名规范_第3张图片

1.Employee实体类:
@Entity
public class Employee {
	private Integer empId;
	private String empAddress;
	private Integer empAge;
	private String empGender;
	private String empName;
	private Double empSalary;
	private Date empStartDate;
	private Integer empStatus;
//	private String deptId; 
	private Department dept;
	
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Integer getEmpId() {
		return empId;
	}
	public void setEmpId(Integer empId) {
		this.empId = empId;
	}
	public String getEmpAddress() {
		return empAddress;
	}
	public void setEmpAddress(String empAddress) {
		this.empAddress = empAddress;
	}
	public Integer getEmpAge() {
		return empAge;
	}
	public void setEmpAge(Integer empAge) {
		this.empAge = empAge;
	}
	public String getEmpGender() {
		return empGender;
	}
	public void setEmpGender(String empGender) {
		this.empGender = empGender;
	}
	public String getEmpName() {
		return empName;
	}
	public void setEmpName(String empName) {
		this.empName = empName;
	}
	public Double getEmpSalary() {
		return empSalary;
	}
	public void setEmpSalary(Double empSalary) {
		this.empSalary = empSalary;
	}
	public Date getEmpStartDate() {
		return empStartDate;
	}
	public void setEmpStartDate(Date empStartDate) {
		this.empStartDate = empStartDate;
	}
	public Integer getEmpStatus() {
		return empStatus;
	}
	public void setEmpStatus(Integer empStatus) {
		this.empStatus = empStatus;
	}
	@ManyToOne
	@JoinColumn(name="deptId")
	public Department getDept() {
		return dept;
	}
	public void setDept(Department dept) {
		this.dept = dept;
	}
	@Override
	public String toString() {
		return "Employee [empId=" + empId + ", empAddress=" + empAddress + ", empAge=" + empAge + ", empGender="
				+ empGender + ", empName=" + empName + ", empSalary=" + empSalary + ", empStartDate=" + empStartDate
				+ ", empStatus=" + empStatus + "]";
	}
	public Employee(Integer empId, String empAddress, Integer empAge, String empGender, String empName,
			Double empSalary, Date empStartDate, Integer empStatus) {
		super();
		this.empId = empId;
		this.empAddress = empAddress;
		this.empAge = empAge;
		this.empGender = empGender;
		this.empName = empName;
		this.empSalary = empSalary;
		this.empStartDate = empStartDate;
		this.empStatus = empStatus;
	}
	public Employee() {
	}
	public Employee(Integer empId, String empName, Double empSalary) {
		
		super();
//		System.out.println("调用了三个参数的有参构造方法");
		this.empId = empId;
		this.empName = empName;
		this.empSalary = empSalary;
	}
}
2.EmployeeDao接口层:
/**
 * Repository:
 * 			T:表示,当前接口,操作的实体类类型
 * Serializable:指定主键的类型
 * @author boge
 *
 */
public interface EmployeeDao  extends CrudRepository{
	//只需要按照springdata提供的命名规范,根据查询需求,来为方法命名即可。
	//如:当前我们希望按照性别查询员工信息,拼接方法名,符合驼峰命名法
	//指定符合逻辑的方法返回值与参数列表
		public List findByEmpGender(String gender);
		
		
		//按照姓名进行查询
		public List findByEmpName(String empName);
		//按照姓氏进行模糊查询
		public List findByEmpNameLike(String empName);
		//按照年龄查询
		public List findByEmpAge(Integer age);
		//按照empAddress查询
		public List findByEmpAddress(String address);
		//查找年龄大于50的员工信息
		public List findByEmpAgeGreaterThan(Integer age);
		
		//查询50岁以上的男性员工
		public List findByEmpAgeGreaterThanAndEmpGender(Integer age,String gender);
		
		/**
		 * 查询所有男性员工信息
		 */
		public List findByEmpGender(String gender);
		
		/**
		 * 按照性别查询,并且按照年龄进行降序排序
		 */
		public List findByEmpGenderOrderByEmpAgeDesc(String gender);
		
		/**
		 * 按照部门编号,查找员工信息
		 * 	可以使用级联查询,非常方便,自动将对应的字段转换成关联的另一个实体类
		 * 也有一种情况:
		 * 	级联查询的最终拼接成的DeptDeptId,恰好与当前实体类的某一个属性同名deptDeptId
		 * 优先,使用当前实体类的这个属性。
		 * 如果遇到这种情况,还想继续使用级联查询:
		 * 	可以通过下划线,来区分:
		 * 	
		 */
		public List findByDept_DeptId(Integer deptId);
		
		/**
		 * 还可以在方法参数中,使用分页参数(Pageable)、排序参数(Sort),实现分页与排序功能。
		 * 按照年龄区间范围查询,并且分页显示
		 */
		public List findByEmpAgeBetween(Integer start,Integer end);
		//添加分页功能,通过方法重载,添加分页参数(Pageable)
		public List findByEmpAgeBetween(Integer start,Integer end,Pageable pageable);
}

3.测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring-beans.xml")
public class EmployeeDaoTest  {
@Autowired
EmployeeDao dao;
	@Test
	public void testFindByEmpGender() {
		List findByEmpGender = dao.findByEmpGender("男");
		for (Employee employee : findByEmpGender) {
			System.out.println(employee);
		}
	}

	@Test
	public void testFindByEmpName() {
		List employee = dao.findByEmpName("周聪");
		for (Employee employee2 : employee) {
			System.out.println(employee2);
		}	
	}

	@Test
	public void testFindByEmpNameLike() {
		List findByEmpNameLike = dao.findByEmpNameLike("张%");
		for (Employee employee : findByEmpNameLike) {
			System.out.println(employee);	
		}
	}
	
	@Test
	public void testFindByEmpAgeGreaterThanAndEmpGender() {
		List list = dao.findByEmpAgeGreaterThanAndEmpGender(50, "男");
		for (Employee employee : list) {
			System.out.println(employee);	
		}
	}

	@Test
	public void testFindByEmpAge() {
		List employee = dao.findByEmpAge(50);
		for (Employee employee2 : employee) {
			System.out.println(employee2);
		}	
	}

	@Test
	public void testFindByEmpAddress() {
		List employee = dao.findByEmpAddress("北京");
		for (Employee employee2 : employee) {
			System.out.println(employee2);
		}	
	}

	@Test
	public void testFindByEmpAgeGreaterThan() {
		List employee = dao.findByEmpAgeGreaterThan(50);
		for (Employee employee2 : employee) {
			System.out.println(employee2);
		}	
	}

	@Test
	public void testFindByEmpAgeBetween(){
		//指定分页参数 参数1:页码,页码从0开始,参数2:每页显示大小
		PageRequest pageRequest = new PageRequest(0, 5);
		List result = employeeDao.findByEmpAgeBetween(20,50,pageRequest);
		for (Employee employee : result) {
			System.out.println(employee.getEmpName()+":"+employee.getEmpAge()+":"+employee.getEmpGender());
		}
	}
	@Test
	public void testFindByDeptDeptId(){
		List result = employeeDao.findByDept_DeptId(4);
		for (Employee employee : result) {
			System.out.println(employee.getEmpName()+":"+employee.getEmpAge()+":"+employee.getEmpGender());
		}
	}
	@Test
	public void testFindByEmpGenderOrderByEmpAgeDesc(){
		List findByEmpGenderOrderByEmpAgeDesc = employeeDao.findByEmpGenderOrderByEmpAgeDesc("女");
		for (Employee employee : findByEmpGenderOrderByEmpAgeDesc) {
			System.out.println(employee.getEmpName()+":"+employee.getEmpAge()+":"+employee.getEmpGender());
		}
	}
	

	@Test
	public void testFindByEmpGender() {
		List findByEmpGender = employeeDao.findByEmpGender("男");
		for (Employee employee : findByEmpGender) {
			System.out.println(employee.getEmpName()+":"+employee.getEmpAge()+":"+employee.getEmpGender());
		}
	}
}
4.spring-beans.xml配置文件:



	
	
	
		
		
		
		
		
	

	
	

	
	
		
		
		
		
			
		
		
		
		
			
				

				
				org.hibernate.dialect.MySQL5InnoDBDialect
				true
				true
				update
			
		
	


	
	
		
	
	
	
	
	


5.jdbc.properties配置文件
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/jpa
jdbc.user=root
jdbc.password=123123
6.所用到的jar包:

百度网盘:
链接:https://pan.baidu.com/s/1Wu6CXl2gOR7_ngM-wf4Ltw
提取码:txpp

你可能感兴趣的:(SpringDataJPA方法命名规范)