整理代码jiscuss

This commit is contained in:
yaoyuan1.chu
2020-06-09 16:58:59 +08:00
parent 0dcc262f59
commit 852c3b2b61
12 changed files with 76 additions and 265 deletions
@@ -2,8 +2,10 @@ package com.yaoyuan.jiscuss;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class JiccussApplication {
public static void main(String[] args) {
@@ -1,75 +0,0 @@
package com.yaoyuan.jiscuss.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.yaoyuan.jiscuss.entity.Staff;
import com.yaoyuan.jiscuss.exception.Error;
import com.yaoyuan.jiscuss.exception.StaffNotFoundException;
import com.yaoyuan.jiscuss.service.IStaffService;
@RestController
@RequestMapping(path="/staffRest",produces="application/json;charset=utf-8")
public class RestServiceController {
@Autowired
private IStaffService staffService;
@ExceptionHandler(StaffNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public Error staffNotFound(StaffNotFoundException e){
Integer id=e.getStaffId();
return new Error(4,"Staff ["+id+"] not found");
}
@GetMapping("/{id}")
public Staff staffById(@PathVariable Integer id){
Staff staff=staffService.findOne(id);
if(staff==null){
throw new StaffNotFoundException(id);
}
return staff;
}
@GetMapping
public List<Staff> getAllStaffs(){
return staffService.getAllList();
}
@PostMapping
public Staff createStaff(Staff staff){
return staffService.insert(staff);
}
@PutMapping
public Staff updateStaff(Staff staff){
return staffService.insert(staff);
}
@DeleteMapping("/{id}")
public Staff deleteStaffById(@PathVariable Integer id){
Staff staff=staffService.findOne(id);
if(staff==null){
throw new StaffNotFoundException(id);
}
staffService.remove(id);
return staff;
}
@DeleteMapping
public List<Staff> deleteAllStaffs(){
List<Staff> staffList=staffService.getAllList();
staffService.deleteAll();
return staffList;
}
}
@@ -1,4 +1,4 @@
package com.yaoyuan.jiscuss.controller;
package com.yaoyuan.jiscuss.controller.api;
import java.util.List;
@@ -1,4 +1,4 @@
package com.yaoyuan.jiscuss.controller;
package com.yaoyuan.jiscuss.controller.api;
import java.util.List;
@@ -1,31 +0,0 @@
package com.yaoyuan.jiscuss.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
@Data
@Entity
@Table(name="staff")
public class Staff implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name="name")
private String name;
@Column(name="age")
private Integer age;
}
@@ -1,11 +0,0 @@
package com.yaoyuan.jiscuss.exception;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public class Error {
private int code;
private String message;
}
@@ -1,12 +0,0 @@
package com.yaoyuan.jiscuss.exception;
@SuppressWarnings("serial")
public class StaffNotFoundException extends RuntimeException {
private Integer staffId;
public StaffNotFoundException(Integer staffId){
this.staffId=staffId;
}
public Integer getStaffId(){
return staffId;
}
}
@@ -1,20 +0,0 @@
package com.yaoyuan.jiscuss.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.yaoyuan.jiscuss.entity.Staff;
@Repository
public interface StaffRepository extends JpaRepository<Staff,Integer> {
@Query("from Staff where name like %:name%")
List<Staff> getByNameIsLike(@Param("name")String name);
Staff getById(Integer id);
void deleteById(Integer id);
}
@@ -1,25 +0,0 @@
package com.yaoyuan.jiscuss.service;
import java.util.List;
import org.springframework.data.domain.Page;
import com.yaoyuan.jiscuss.entity.Staff;
public interface IStaffService {
List<Staff> getAllList();
Page<Staff> queryAllStaffList(int pageNum,int pageSize);
List<Staff> getByNameIsLike(String name);
// @Cacheable("myCache")
Staff findOne(Integer id);
Staff insert(Staff staff);
void remove(Integer id);
void deleteAll();
}
@@ -1,64 +0,0 @@
package com.yaoyuan.jiscuss.service.impl;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import com.yaoyuan.jiscuss.entity.Staff;
import com.yaoyuan.jiscuss.repository.StaffRepository;
import com.yaoyuan.jiscuss.service.IStaffService;
@Service
@Transactional
public class StaffServiceImpl implements IStaffService {
@Autowired
private StaffRepository staffRepository;
@Override
public List<Staff> getAllList() {
return staffRepository.findAll();
}
@Override
public Page<Staff> queryAllStaffList(int pageNum,int pageSize) {
Sort sort=new Sort(Sort.Direction.DESC,"id");
@SuppressWarnings("deprecation")
Pageable pageable=new PageRequest(pageNum,pageSize,sort);
return staffRepository.findAll(pageable);
}
@Override
public List<Staff> getByNameIsLike(String name) {
return staffRepository.getByNameIsLike(name);
}
@Override
public Staff findOne(Integer id) {
return staffRepository.getById(id);
}
@Override
public Staff insert(Staff staff) {
return staffRepository.save(staff);
}
@Override
public void remove(Integer id) {
staffRepository.deleteById(id);
}
@Override
public void deleteAll() {
staffRepository.deleteAll();
}
}
@@ -8,6 +8,8 @@ import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
@@ -24,14 +26,23 @@ import com.yaoyuan.jiscuss.service.IUsersService;
public class UsersServiceImpl implements IUsersService {
@Autowired
private UsersRepository usersRepository;
/**
* 获取全部用户
* @return
*/
@Cacheable(value = "user")
@Override
public List<Users> getAllList() {
return usersRepository.findAll();
}
/**
* 分页查询
* @param pageNum
* @param pageSize
* @return
*/
@Override
public Page<Users> queryAllUsersList(int pageNum,int pageSize) {
Sort sort=new Sort(Sort.Direction.DESC,"id");
@@ -39,48 +50,90 @@ public class UsersServiceImpl implements IUsersService {
Pageable pageable=new PageRequest(pageNum,pageSize,sort);
return usersRepository.findAll(pageable);
}
/**
* 根据名称模糊查询
* @param name
* @return
*/
@Override
public List<Users> getByUsernameIsLike(String name) {
return usersRepository.getByUsernameIsLike(name);
}
/**
* 根据id查询
* @param id
* @return
*/
@Cacheable(value = "user", key = "#id")
@Override
public Users findOne(Integer id) {
return usersRepository.getById(id);
}
/**
* 新增
* @param user
* @return
*/
@CachePut(value = "user", key = "#user.id")
@Override
public Users insert(Users user) {
return usersRepository.save(user);
}
/**
* 更新修改
* @param user
* @param id
* @return
*/
@CachePut(value = "user", key = "#user.id")
@Override
public Users update(Users user, Integer id) {
return usersRepository.saveAndFlush(user);
}
/**
* 移除
* @param id
*/
@CacheEvict(value = "user", key = "#id")
@Override
public void remove(Integer id) {
usersRepository.deleteById(id);
}
/**
* 删除所有
*/
@Override
public void deleteAll() {
usersRepository.deleteAll();
}
/**
* 根据名称查询
* @param username
* @return
*/
@Override
public Users getByUsername(String username) {
return usersRepository.getByUsername(username);
}
/**
* 验证用户名密码
* @param username
* @param password
* @return
*/
@Override
public Users checkByUsernameAndPassword(String username,String password) {
return usersRepository.checkByUsernameAndPassword(username,password);
}
@Override
public Users update(Users user, Integer id) {
return usersRepository.saveAndFlush(user);
}
}
+6 -12
View File
@@ -1,10 +1,4 @@
drop table if exists staff;
CREATE TABLE staff(
id INTEGER not null primary key,
name char(20),
age INTEGER
);
-- 用户表1
drop table if exists users;
CREATE TABLE users(
id INTEGER not null primary key auto_increment,
@@ -22,7 +16,7 @@ last_seen_time datetime,
flag INTEGER,
level INTEGER
);
-- 主题表2
drop table if exists discussions;
CREATE TABLE discussions(
id INTEGER not null primary key auto_increment,
@@ -44,13 +38,13 @@ ip_address varchar(200),
create_id INTEGER,
create_time datetime
);
-- 主题标签关联表3
drop table if exists discussionstags;
CREATE TABLE discussionstags(
discussion_id INTEGER not null ,
tag_id INTEGER
);
-- 评论表4
drop table if exists posts;
CREATE TABLE posts(
id INTEGER not null primary key auto_increment ,
@@ -68,13 +62,13 @@ is_approved INTEGER,
create_id INTEGER,
create_time datetime
);
-- 设置表5
drop table if exists settings;
CREATE TABLE settings(
key varchar(20) primary key,
value text
);
-- 标签表6
drop table if exists tags;
CREATE TABLE tags(
id INTEGER not null primary key auto_increment,