SpringDataJPA中的JPQL
注:jpql查询不需要事务的支持,但增删改需要事务的支持
@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;
// private String deptDeptId;
// public String getDeptDeptId() {
// return deptDeptId;
// }
// public void setDeptDeptId(String deptDeptId) {
// this.deptDeptId = deptDeptId;
// }
@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;
}
}
@Entity
public class Department {
private Integer deptId;
private String deptName;
private String deptManager;
private Set emps = new HashSet();
@OneToMany(mappedBy="dept",fetch=FetchType.LAZY)
// @JoinColumn(name="deptId")
public Set getEmps() {
return emps;
}
public void setEmps(Set emps) {
this.emps = emps;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getDeptManager() {
return deptManager;
}
public void setDeptManager(String deptManager) {
this.deptManager = deptManager;
}
@Override
public String toString() {
return "Department [deptId=" + deptId + ", deptName=" + deptName + ", deptManager=" + deptManager + "]";
}
public Department(Integer deptId, String deptName, String deptManager) {
super();
this.deptId = deptId;
this.deptName = deptName;
this.deptManager = deptManager;
}
public Department() {
super();
}
}
public interface EmployeeDao extends Repository{
/**
* 按照id修改用户的名字和年龄
*/
@Modifying
@Query("update Employee e set e.empName = ?1,e.empAge=?2 where e.empId=?3")
public Integer updateNameAndAgeById(String empName,Integer empAge,Integer id);
/**
* 什么时候,使用JPQL?
* 方法命名规范很好用,不一定能胜任任何场景,编写JPQL与方法映射。
*/
/**
* 按照性别查询员工信息,要求使用jpql
*/
// @Query("select e from Employee e where e.empGender = ?1")
@Query(value="select * from employee where empGender = ?1",nativeQuery=true)
public List abcdefg(String gender);
}
public interface EmployeeService {
public Integer updateNameAndAgeById(String empName,Integer empAge,Integer empId);
}
@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeDao employeeDao;
@Override
public Integer updateNameAndAgeById(String empName, Integer empAge, Integer empId) {
Integer updateNameAndAgeById = employeeDao.updateNameAndAgeById(empName,empAge,empId);
return updateNameAndAgeById;
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring-beans.xml")
public class EmployeeServiceTest {
@Autowired
EmployeeService service;
@Test
public void testUpdateNameAndAgeById() {
Integer updateNameAndAgeById = service.updateNameAndAgeById("钱媛", 18, 9);
System.out.println(updateNameAndAgeById);
}
@Test
public void testAbcdef(){
List abcdefg = employeeDao.abcdefg("男");
for (Employee employee : abcdefg) {
System.out.println(employee.getEmpName()+":"+employee.getEmpAge()+":"+employee.getEmpGender());
}
}
}
org.hibernate.dialect.MySQL5InnoDBDialect
true
true
update
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/jpa
jdbc.user=root
jdbc.password=123123