目录
四.创建mapper文件 添加xml文件夹管理 创建mapper的sql语句
五 添加service层 将业务逻辑全部放到service中管理
七.在controller层中接受前端发来的请求,同时向后端发送请求
一.导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <optional>true</optional>
</dependency>
<!-- mysql-connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- mybatis-plus代码生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
<!-- jedis依赖,访问redis数据库-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
二.添加实体类 entity
2.1 登录实体
public class User implements Serializable {
/**
* 用户账号
*/
private String userCode;
/**
* 用户名
*/
private String userName;
2.2 内容实体
public class Paper implements Serializable {
private Long id;
private String title;
private Long typeId;
private String paperSummary;
private String paperPath;
private String createdBy;
private LocalDateTime creationData;
private String modifyBy;
private LocalDateTime modifyData;
private String fileName;
private String typeName;
}
2.3 前后端交互传输数据
@AllArgsConstructor
@NoArgsConstructor
public class HttpResult {
private Object data;
private int pageTotle;
private int code;//200=成功 500失败
private String msg;//提示
}
三.添加mapper类 写出业务接口
public interface UserMapper extends BaseMapper<User> {
//登录
}
public interface PaperMapper extends BaseMapper<Paper> {
List<Paper> selectPaperListByCondition(String title, String typeName, int start, int size);
int count(String title,String typeName);//查询总数,用于列表分页
//根据id获取论文信息的接口
Paper getPaperById(long id);
//根据id删除论文的接口
int DeletePaperId(long id);
//添加论文
int insertPaper(Paper paper);
//根据id修改论文
int updatePaperById(Paper paper);
}
四.创建mapper文件 添加xml文件夹管理 创建mapper的sql语句
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.woniu.paper.mapper.PaperMapper">
<!--建立数据库t_paper表和实体Paper的映射关系-->
<resultMap id="paperResult" type="Paper">
<result column="id" property="id"/> <!-- column:数据库字段名 property:实体的属(变量)名 -->
<result column="title" property="title"/> <!-- column:数据库字段名 property:实体的属(变量)名 -->
<result column="type_id" property="typeId"/> <!-- column:数据库字段名 property:实体的属(变量)名 -->
<result column="paper_summary" property="paperSummary"/> <!-- column:数据库字段名 property:实体的属(变量)名 -->
<result column="paper_path" property="paperPath"/> <!-- column:数据库字段名 property:实体的属(变量)名 -->
<result column="created_by" property="createdBy"/> <!-- column:数据库字段名 property:实体的属(变量)名 -->
<result column="creation_data" property="creationData"/> <!-- column:数据库字段名 property:实体的属(变量)名 -->
<result column="modify_by" property="modifyBy"/> <!-- column:数据库字段名 property:实体的属(变量)名 -->
<result column="modify_data" property="modifyData"/> <!-- column:数据库字段名 property:实体的属(变量)名 -->
<result column="file_name" property="fileName"/> <!-- column:数据库字段名 property:实体的属(变量)名 -->
<result column="type_name" property="typeName"/> <!-- column:数据库字段名 property:实体的属(变量)名 -->
</resultMap>
<select id="selectPaperListByCondition" resultMap="paperResult">
select p.*, t.type_name from t_paper as p LEFT JOIN t_type as t on
p.type_id=t.id
<where>
<if test="title != null and title != 'null'">
and title like '%${title}%'
</if>
<if test="typeName !=null and typeName != 'null'">
and type_name=#{typeName}
</if>
</where>
limit #{start},#{size}
</select>
<select id="count" resultType="int">
select count(t.type_name) from t_paper as p LEFT JOIN t_type as t on
p.type_id=t.id
<where>
<if test="title != null and title != ''">
and title like '%${title}%'
</if>
<if test="typeName !=null and typeName != ''">
and type_name=#{typeName}
</if>
</where>
</select>
<select id="getPaperById" resultMap="paperResult" >
select p.*, t.type_name from t_paper as p left join t_type as t on p.type_id=t.id where p.id=#{id}
</select>
<delete id="DeletePaperId">
delete from t_paper where id=#{id};
</delete>
<insert id="insertPaper">
insert into t_paper (title, type_id, created_by)
values ( #{title}, #{typeId}, #{createdBy})
</insert>
<update id="updatePaperById">
update t_paper set title=#{title},type_id=#{typeId},created_by=#{createdBy} where id=#{id}
</update>
</mapper>
五 添加service层 将业务逻辑全部放到service中管理
CRUD
public interface IPaperService {
public HttpResult selectPaperListByCondition(String title, String typeName, int pageIndex, int pageSize);
HttpResult getPaperById(long id);
HttpResult DeletePaperId(long id);
HttpResult insertPaper(Paper paper);
HttpResult updatePaperById(Paper paper);
}
@Service
public class PaperServiceImpl implements IPaperService {
@Autowired(required = false)
private PaperMapper paperMapper;
@Override
public HttpResult selectPaperListByCondition(String title, String typeName, int pageIndex, int pageSize) {
List<Paper> papers = paperMapper.selectPaperListByCondition(title, typeName, (pageIndex - 1) * pageSize, pageSize);
int count=paperMapper.count(title,typeName);
HttpResult result=null;
if(papers!=null && papers.size()>0){
result=new HttpResult(papers,count,200,null);
}else{
result=new HttpResult(null,0,500,"没有更多数据");
}
return result;
}
@Override
public HttpResult getPaperById(long id) {
Paper paper = paperMapper.getPaperById(id);
HttpResult result=null;
if (paper!=null){
result=new HttpResult(paper,0,200,null);
return result;
}else {
result=new HttpResult(null,0,500,"没有更多数据");
return result ;
}
}
@Override
public HttpResult DeletePaperId(long id) {
int count=paperMapper.DeletePaperId(id);
HttpResult result=null;
if (count>0){
result=new HttpResult(null,0,200,"删除充功");
return result;
}else{
result=new HttpResult(null,0,500,"删除失败");
return result ;
}
}
@Override
public HttpResult insertPaper(Paper paper) {
int count=paperMapper.insertPaper(paper);
HttpResult result=null;
if (count>0){
result=new HttpResult(null,0,200,"添加论文充功");
return result;
}else{
result=new HttpResult(null,0,500,"添加论文失败");
return result ;
}
}
@Override
public HttpResult updatePaperById(Paper paper) {
int count=paperMapper.updatePaperById(paper);
HttpResult result=null;
if (count>0){
result=new HttpResult(null,0,200,"修改论文充功");
return result;
}else{
result=new HttpResult(null,0,500,"修改论文失败");
return result ;
}
}
}
登录的业务
public class UserServiceImpl implements IUserService {
@Autowired(required = false)
private UserMapper userMapper;
@Override
public HttpResult login(String userCode, String pwd) {
//创建条件构造器,构造一系列的条件
//条件构造器
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
//构造查询条件
queryWrapper.eq("user_code",userCode);
queryWrapper.eq("user_password",pwd);
User user = userMapper.selectOne(queryWrapper);
System.out.println(user);
HttpResult result=null;
if(user !=null){
//登录成功,将用户的id保存在redis中
Jedis jedis=new Jedis("42.192.37.216",6379);
jedis.auth("zxcv2018");
jedis.set(user.getId()+"",user.getId()+"");//将用户保存到redis中
jedis.close();
result=new HttpResult(user,0,200,"登录成功");
}else{
result=new HttpResult(null,0,500,"登录失败");
}
return result;
}
}
六.登录的拦截器
@Configuration
public class Authinterceptor extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
HandlerInterceptor handlerInterceptor = new HandlerInterceptor() {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
boolean isGo=false;
String url=request.getRequestURI();//获取当前请求的数据接口路径
System.out.println("当前请求的路径"+url);
//获取用户请求时传递的id
String uid=request.getParameter("uid");//接受用户登录的参数
//获取根据uid查询redis中是否哦呦对应的数据
Jedis jedis=new Jedis("42.192.37.216",6379);
if(url.contains("/paper/user/login")){
isGo=true;
}else if (jedis.get(uid)!=null){//如果用户登录传递的值与redis中一致,则方形
isGo=true;
}
jedis.close();
return isGo;
}
registry.addInterceptor(handlerInterceptor).addPathPatterns("/**");
}
}
七.在controller层中接受前端发来的请求,同时向后端发送请求
登录
@RestController
@RequestMapping("/paper/user")
public class UserController {
@Autowired(required = false)
private IUserService userService;
@CrossOrigin(origins = "*")
@RequestMapping("/login")
public HttpResult login(String userCode,String pwd){
return userService.login(userCode,pwd);
}
@RequestMapping("/fun")
public String fun(){
return "ahhah";
}
}
CRUD
@RestController
@RequestMapping("/paper/paper")
public class PaperController {
@Autowired
private IPaperService paperService;
/**
* 根据条件分页查询
*/
@RequestMapping("/list")
@CrossOrigin(origins = "*")
public HttpResult selectPaperListByCondition(String title,String typeName,int pageIndex,int pageSize){
return paperService.selectPaperListByCondition(title,typeName,pageIndex,pageSize);
}
/**
* 根据id查询论文细心
* @param id
* @return
*/
@RequestMapping("/info")
@CrossOrigin(origins = "*")
public HttpResult getPaperById(long id){
return paperService.getPaperById(id);
}
/**
* 根据id删除
*/
@RequestMapping("/delete") //通过axios发送请求
@CrossOrigin(origins = "*")
public HttpResult delete(long id){
return paperService.DeletePaperId(id);
}
@RequestMapping("/add")
@CrossOrigin(origins = "*")
public HttpResult insertPaper(String title,String createdBy,long typeId){
Paper paper = new Paper();
paper.setTitle(title);
paper.setCreatedBy(createdBy);
paper.setTypeId(typeId);
return paperService.insertPaper(paper);
}
@RequestMapping("/update")
@CrossOrigin(origins = "*")
public HttpResult updatePaper(long id,String title,String createdBy,long typeId){
Paper paper = new Paper();
paper.setId(id);
paper.setTitle(title);
paper.setCreatedBy(createdBy);
paper.setTypeId(typeId);
return paperService.updatePaperById(paper);
}
}