application.properties的配置
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/to_database
spring.datasource.username=postgres
spring.datasource.password=123456789
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
複製程式碼
新建Repository介面,繼承JpaRepository
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
public interface EmployeeInfoRepository extends JpaRepository<EmployeeInfo, Long> {
List<EmployeeInfo> findByLuckyStatus(String luckyStatus);
List<EmployeeInfo> findByUmNo(String unNo);
List<EmployeeInfo> findByOrderByUmNoAsc();
@Transactional
@Modifying
@Query("update EmployeeInfo set luckyStatus = :luckyStatus")
int updateLuckyStatus(@Param("luckyStatus") String luckyStatus);
}
複製程式碼
新建Entity
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class EmployeeInfo {
@Id
@GeneratedValue
private Long id;
private String umNo;
private String name;
private String luckyStatus;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUmNo() {
return umNo;
}
public void setUmNo(String umNo) {
this.umNo = umNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLuckyStatus() {
return luckyStatus;
}
public void setLuckyStatus(String luckyStatus) {
this.luckyStatus = luckyStatus;
}
}
複製程式碼
service呼叫
import com.stock.core.entity.EmployeeInfo;
import com.stock.core.respository.EmployeeInfoRepository;
import com.stock.core.service.EmployeeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class EmployeeInfoServiceImpl implements EmployeeInfoService {
@Autowired
private EmployeeInfoRepository employeeInfoRepository;
@Override
public List<EmployeeInfo> getAllEmployeeInfoList() {
return employeeInfoRepository.findByOrderByUmNoAsc();
}
@Override
public List<EmployeeInfo> getByLuckyStatus(String luckyStatus) {
return employeeInfoRepository.findByLuckyStatus(luckyStatus);
}
@Override
public int saveEmployeeInfo(String umNo, String name) {
umNo = umNo.toLowerCase();
EmployeeInfo employee = null;
List<EmployeeInfo> list = employeeInfoRepository.findByUmNo(umNo);
if(null != list && !list.isEmpty()){
employee = list.get(0);
}else{
employee = new EmployeeInfo();
}
employee.setUmNo(umNo);
employee.setName(name);
employee.setLuckyStatus("0");
employeeInfoRepository.save(employee);
return 1;
}
@Override
public int employeeSoLuckiLy(Long id) {
Optional<EmployeeInfo> optional = employeeInfoRepository.findById(id);
if(null == optional){
return 0;
}
EmployeeInfo employee = optional.get();
if(null == employee){
return 0;
}
employee.setLuckyStatus("1");
employeeInfoRepository.save(employee);
return 1;
}
@Override
public int setLuckyStatus(String luckyStatus) {
return employeeInfoRepository.updateLuckyStatus(luckyStatus);
}
}
複製程式碼