前后端分离项目示例/后端

目录

后端

后端主要是跨域问题,其余为日常代码。

1. 控制器

import com.example.demo.model.book;
import com.example.demo.service.BookService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/book")
public class bookController {
    @Autowired
    private BookService bookService;

    @PostMapping("/getBookList")
    public List<book> getBookList(@RequestBody JSONObject params) {
        String name = params.getString("name");
        String author = params.getString("author");
        String publish = params.getString("publish");
        Integer pages = params.getInteger("pages");

        Integer bookcaseid = params.getInteger("bookcaseid");
        Integer abled = params.getInteger("abled");

        Map paramMap = new HashMap<String,Object>();
        paramMap.put("name",name);
        paramMap.put("author",author);
        paramMap.put("publish",publish);
        paramMap.put("pages",pages);

        paramMap.put("bookcaseid",bookcaseid);
        paramMap.put("abled",abled);

        List<book> list = bookService.getBookList(paramMap);
        return list;
    }
}

2. Dao

package com.example.demo.dao;

import com.example.demo.model.book;

import java.util.List;
import java.util.Map;
public interface BookMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(book record);

    int insertSelective(book record);

    book selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(book record);

    int updateByPrimaryKey(book record);

    List<book> getBookList(Map param);
}
<?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.example.demo.dao.BookMapper" >
  <resultMap id="BaseResultMap" type="com.example.demo.model.book" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="author" property="author" jdbcType="VARCHAR" />
    <result column="publish" property="publish" jdbcType="VARCHAR" />
    <result column="pages" property="pages" jdbcType="INTEGER" />
    <result column="price" property="price" jdbcType="REAL" />
    <result column="bookcaseid" property="bookcaseid" jdbcType="INTEGER" />
    <result column="abled" property="abled" jdbcType="INTEGER" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, name, author, publish, pages, price, bookcaseid, abled
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from book
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from book
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.example.demo.model.book" >
    insert into book (id, name, author, 
      publish, pages, price, 
      bookcaseid, abled)
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{author,jdbcType=VARCHAR}, 
      #{publish,jdbcType=VARCHAR}, #{pages,jdbcType=INTEGER}, #{price,jdbcType=REAL}, 
      #{bookcaseid,jdbcType=INTEGER}, #{abled,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.example.demo.model.book" >
    insert into book
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="name != null" >
        name,
      </if>
      <if test="author != null" >
        author,
      </if>
      <if test="publish != null" >
        publish,
      </if>
      <if test="pages != null" >
        pages,
      </if>
      <if test="price != null" >
        price,
      </if>
      <if test="bookcaseid != null" >
        bookcaseid,
      </if>
      <if test="abled != null" >
        abled,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="name != null" >
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="author != null" >
        #{author,jdbcType=VARCHAR},
      </if>
      <if test="publish != null" >
        #{publish,jdbcType=VARCHAR},
      </if>
      <if test="pages != null" >
        #{pages,jdbcType=INTEGER},
      </if>
      <if test="price != null" >
        #{price,jdbcType=REAL},
      </if>
      <if test="bookcaseid != null" >
        #{bookcaseid,jdbcType=INTEGER},
      </if>
      <if test="abled != null" >
        #{abled,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.example.demo.model.book" >
    update book
    <set >
      <if test="name != null" >
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="author != null" >
        author = #{author,jdbcType=VARCHAR},
      </if>
      <if test="publish != null" >
        publish = #{publish,jdbcType=VARCHAR},
      </if>
      <if test="pages != null" >
        pages = #{pages,jdbcType=INTEGER},
      </if>
      <if test="price != null" >
        price = #{price,jdbcType=REAL},
      </if>
      <if test="bookcaseid != null" >
        bookcaseid = #{bookcaseid,jdbcType=INTEGER},
      </if>
      <if test="abled != null" >
        abled = #{abled,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.example.demo.model.book" >
    update book
    set name = #{name,jdbcType=VARCHAR},
      author = #{author,jdbcType=VARCHAR},
      publish = #{publish,jdbcType=VARCHAR},
      pages = #{pages,jdbcType=INTEGER},
      price = #{price,jdbcType=REAL},
      bookcaseid = #{bookcaseid,jdbcType=INTEGER},
      abled = #{abled,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="getBookList" resultMap="BaseResultMap">
      select
    <include refid="Base_Column_List"/>
      from book
  </select>
</mapper>

3. model

package com.example.demo.model;

public class book {
    private Integer id;

    private String name;

    private String author;

    private String publish;

    private Integer pages;

    private Float price;

    private Integer bookcaseid;

    private Integer abled;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author == null ? null : author.trim();
    }

    public String getPublish() {
        return publish;
    }

    public void setPublish(String publish) {
        this.publish = publish == null ? null : publish.trim();
    }

    public Integer getPages() {
        return pages;
    }

    public void setPages(Integer pages) {
        this.pages = pages;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public Integer getBookcaseid() {
        return bookcaseid;
    }

    public void setBookcaseid(Integer bookcaseid) {
        this.bookcaseid = bookcaseid;
    }

    public Integer getAbled() {
        return abled;
    }

    public void setAbled(Integer abled) {
        this.abled = abled;
    }
}

4. service

package com.example.demo.service;

import com.example.demo.model.book;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
public interface BookService {
    List<book> getBookList(Map param);
}

package com.example.demo.service.impl;

import com.example.demo.dao.BookMapper;
import com.example.demo.model.book;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;
    @Override
    public List<book> getBookList(Map param) {
        return bookMapper.getBookList(param);
    }
}

5. application.yml

server:
  port: 8088
    # 数据源配置
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mysql?serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

6. 启动类

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@MapperScan(value = "com.example.demo.dao")
@SpringBootApplication
@EnableTransactionManagement
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

7.config配置类,解决跨域问题。(controller同级建一个config文件夹,在文件夹下建一个webconfig类即可)

package com.example.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET","POST","PUT","OPTIONS","DELETE","PATCH")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }

}

Guess you like

Origin blog.csdn.net/after_17/article/details/113872673