spring-boot-jpa

只罗列持久层, 业务层, 控制层关系

Entity建立bean 和数据库的对应关系

@Entity
@Table(name = "t_sss")    // db table_name
public class c_sss implements Serializable{

    /** * */
    private static final long serialVersionUID = 1L;

    // id is used for DB primary key
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

    @Column(name = "XXX")
    private String XXX;

    @Column(name = "YYY", unique = true)
    private String YYY;

    final private String SQEMPTY = " ";

    public c_sss(){
        super();
    }

    public void setId(Integer id){
        this.id = id;
    }

    public Integer getId(){
        return id;
    }

    public void setXXX(String XXX){
        this.XXX = XXX;
    }

    public String getXXX(){
        return (XXX == null ? SQEMPTY : XXX);
    }

    public void setYYY(String YYY){
        this.YYY = envYYY;
    }

    public void getYYY(String YYY){
        return (YYY == null ? SQEMPTY : YYY);
    }
    @Override
    public String toString(){
        return "[" + "XXX:" + "]" ;   //omit
}

DAO建立数据库操作方法与 mysql指令对应关系

public interface CSDao
    extends JpaRepository<c_sss, Integer>, JpaSpecificationExecutor<c_sss>{
    @Modifying
    @Transactional(readOnly = false)  
    @Query(value = "delete from t_sss where id = :id", nativeQuery = true)
    public int deleteByPrimaryKey(@Param("id") Integer id);

    @Modifying
    @Transactional(readOnly = false)  
    @Query(value = "update t_sss set XXX = :XXX where YYY = :YYY", nativeQuery = true)
    public int updateXXXByYYY(@Param("YYY") String YYY, @Param("XXX") String XXX) throws Exception; 

    @Query(value="select * from t_sss where id=:id", nativeQuery = true)
    public c_sss selectByPrimaryKey(@Param("id") Integer id);

    @Query(value = "select * from t_sss", nativeQuery = true)
    public List<c_sss> getcssList();
}

service声明方法

public interface CSService {
    int insert(c_sss cs) throws Exception;

    int deleteById(Integer id);

    int updateXXXByYYY(String XXX, String YYY) throws Exception;

    c_sss findById(Integer id);

    List<c_sss> getCSList();
}

impl 继承并实现service中的接口, with Dao 类的方法.

@Service
public class CSServiceImpl implements CSService {

    @Resource
    private CSDaoDao csDao;

    @Override
    public int insert(c_sss cs) throws Exception {
         csDao.save(cs);
         return cs.getId();
    }

    @Override
    public int deleteById(Integer id) {
        return csDao.deleteByPrimaryKey(id);
    }

@Override
    public int updateXXXByYYY(String XXX, String YYY) throws Exception{
        return csDao.updateXXXByYYY(YYY, XXX);
    }

    @Override
    public c_sss findById(Integer id) {
        return csDao.selectByPrimaryKey(id);
    }

    @Override
    public List<c_sss> getCSList() {
        return CDao.getcssList();
    }

}

Controller 中建立映射 @RestController

@RestController
@RequestMapping("/controller")
public class CSController {

  private static final Logger logger = LoggerFactory.getLogger(CSController.class);
  @Autowired
  private CSService csService;

  @RequestMapping("/getAll")
  public List<c_sss> getCSList() {
      logger.info("handle get request /controller/getAll");
    return csService.getCSList();
  }

}

你可能感兴趣的:(spring-boot-jpa)