整理代码jiscuss
This commit is contained in:
@@ -2,8 +2,10 @@ package com.yaoyuan.jiscuss;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@EnableCaching
|
||||||
public class JiccussApplication {
|
public class JiccussApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
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
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.yaoyuan.jiscuss.controller;
|
package com.yaoyuan.jiscuss.controller.api;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.yaoyuan.jiscuss.controller;
|
package com.yaoyuan.jiscuss.controller.api;
|
||||||
|
|
||||||
import java.util.List;
|
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 javax.transaction.Transactional;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.Page;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
@@ -25,13 +27,22 @@ public class UsersServiceImpl implements IUsersService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private UsersRepository usersRepository;
|
private UsersRepository usersRepository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取全部用户
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@Cacheable(value = "user")
|
@Cacheable(value = "user")
|
||||||
@Override
|
@Override
|
||||||
public List<Users> getAllList() {
|
public List<Users> getAllList() {
|
||||||
return usersRepository.findAll();
|
return usersRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页查询
|
||||||
|
* @param pageNum
|
||||||
|
* @param pageSize
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Page<Users> queryAllUsersList(int pageNum,int pageSize) {
|
public Page<Users> queryAllUsersList(int pageNum,int pageSize) {
|
||||||
Sort sort=new Sort(Sort.Direction.DESC,"id");
|
Sort sort=new Sort(Sort.Direction.DESC,"id");
|
||||||
@@ -40,47 +51,89 @@ public class UsersServiceImpl implements IUsersService {
|
|||||||
return usersRepository.findAll(pageable);
|
return usersRepository.findAll(pageable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据名称模糊查询
|
||||||
|
* @param name
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<Users> getByUsernameIsLike(String name) {
|
public List<Users> getByUsernameIsLike(String name) {
|
||||||
return usersRepository.getByUsernameIsLike(name);
|
return usersRepository.getByUsernameIsLike(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查询
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Cacheable(value = "user", key = "#id")
|
||||||
@Override
|
@Override
|
||||||
public Users findOne(Integer id) {
|
public Users findOne(Integer id) {
|
||||||
return usersRepository.getById(id);
|
return usersRepository.getById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增
|
||||||
|
* @param user
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@CachePut(value = "user", key = "#user.id")
|
||||||
@Override
|
@Override
|
||||||
public Users insert(Users user) {
|
public Users insert(Users user) {
|
||||||
return usersRepository.save(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
|
@Override
|
||||||
public void remove(Integer id) {
|
public void remove(Integer id) {
|
||||||
usersRepository.deleteById(id);
|
usersRepository.deleteById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除所有
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void deleteAll() {
|
public void deleteAll() {
|
||||||
usersRepository.deleteAll();
|
usersRepository.deleteAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据名称查询
|
||||||
|
* @param username
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Users getByUsername(String username) {
|
public Users getByUsername(String username) {
|
||||||
return usersRepository.getByUsername(username);
|
return usersRepository.getByUsername(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证用户名密码
|
||||||
|
* @param username
|
||||||
|
* @param password
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Users checkByUsernameAndPassword(String username,String password) {
|
public Users checkByUsernameAndPassword(String username,String password) {
|
||||||
return usersRepository.checkByUsernameAndPassword(username,password);
|
return usersRepository.checkByUsernameAndPassword(username,password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Users update(Users user, Integer id) {
|
|
||||||
return usersRepository.saveAndFlush(user);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,4 @@
|
|||||||
drop table if exists staff;
|
-- 用户表1
|
||||||
CREATE TABLE staff(
|
|
||||||
id INTEGER not null primary key,
|
|
||||||
name char(20),
|
|
||||||
age INTEGER
|
|
||||||
);
|
|
||||||
|
|
||||||
drop table if exists users;
|
drop table if exists users;
|
||||||
CREATE TABLE users(
|
CREATE TABLE users(
|
||||||
id INTEGER not null primary key auto_increment,
|
id INTEGER not null primary key auto_increment,
|
||||||
@@ -22,7 +16,7 @@ last_seen_time datetime,
|
|||||||
flag INTEGER,
|
flag INTEGER,
|
||||||
level INTEGER
|
level INTEGER
|
||||||
);
|
);
|
||||||
|
-- 主题表2
|
||||||
drop table if exists discussions;
|
drop table if exists discussions;
|
||||||
CREATE TABLE discussions(
|
CREATE TABLE discussions(
|
||||||
id INTEGER not null primary key auto_increment,
|
id INTEGER not null primary key auto_increment,
|
||||||
@@ -44,13 +38,13 @@ ip_address varchar(200),
|
|||||||
create_id INTEGER,
|
create_id INTEGER,
|
||||||
create_time datetime
|
create_time datetime
|
||||||
);
|
);
|
||||||
|
-- 主题标签关联表3
|
||||||
drop table if exists discussionstags;
|
drop table if exists discussionstags;
|
||||||
CREATE TABLE discussionstags(
|
CREATE TABLE discussionstags(
|
||||||
discussion_id INTEGER not null ,
|
discussion_id INTEGER not null ,
|
||||||
tag_id INTEGER
|
tag_id INTEGER
|
||||||
);
|
);
|
||||||
|
-- 评论表4
|
||||||
drop table if exists posts;
|
drop table if exists posts;
|
||||||
CREATE TABLE posts(
|
CREATE TABLE posts(
|
||||||
id INTEGER not null primary key auto_increment ,
|
id INTEGER not null primary key auto_increment ,
|
||||||
@@ -68,13 +62,13 @@ is_approved INTEGER,
|
|||||||
create_id INTEGER,
|
create_id INTEGER,
|
||||||
create_time datetime
|
create_time datetime
|
||||||
);
|
);
|
||||||
|
-- 设置表5
|
||||||
drop table if exists settings;
|
drop table if exists settings;
|
||||||
CREATE TABLE settings(
|
CREATE TABLE settings(
|
||||||
key varchar(20) primary key,
|
key varchar(20) primary key,
|
||||||
value text
|
value text
|
||||||
);
|
);
|
||||||
|
-- 标签表6
|
||||||
drop table if exists tags;
|
drop table if exists tags;
|
||||||
CREATE TABLE tags(
|
CREATE TABLE tags(
|
||||||
id INTEGER not null primary key auto_increment,
|
id INTEGER not null primary key auto_increment,
|
||||||
|
|||||||
Reference in New Issue
Block a user