目录
前后端分离
1.1后端
1.1.1配置generatorConfig.xml中的mysql文件目录,自动生成代码
1.1.2mapper层
1.1.2.1mapper.xml层
1.1.3StudentService-->将BookMapper复制到Service
1.1.4StudentServiceImpl(业务逻辑层)
1.1.5StudentController层
1.2前端
1.2.1在cmd中下载依赖npm i(install)
1.2.2在api中action.js配置请求路径
1.2.3前端views写前端页面功能代码
1.2.4在cmd中跑起来npm run dev
查询+主页面绑值

新增

generatorConfig.xml

mapper层
-
- @Repository
- public interface StudentMapper {
-
- /**
- * 新增
- * @param record
- * @return
- */
- int insert(Student record);
-
- /**
- * 查询
- * @param student
- * @return
- */
- List
all(Student student); - }
mapper.xml层
- "1.0" encoding="UTF-8" ?>
- mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="com.zking.spboot.mapper.StudentMapper" >
- <resultMap id="BaseResultMap" type="com.zking.spboot.model.Student" >
- <constructor >
- <idArg column="sid" jdbcType="INTEGER" javaType="java.lang.Integer" />
- <arg column="sname" jdbcType="VARCHAR" javaType="java.lang.String" />
- <arg column="score" jdbcType="VARCHAR" javaType="java.lang.String" />
- <arg column="sex" jdbcType="VARCHAR" javaType="java.lang.String" />
- constructor>
- resultMap>
- <sql id="Base_Column_List" >
- sid, sname, score, sex
- sql>
-
- <select id="all" resultType="com.zking.spboot.model.Student">
- select <include refid="Base_Column_List" /> from t_student where 1=1
- <if test="sname!=null">
- and sname like concat('%',#{sname},'%')
- if>
- select>
-
- <insert id="insert" parameterType="com.zking.spboot.model.Student" >
- insert into t_student (sname, score,sex)
- values (#{sname,jdbcType=VARCHAR}, #{score,jdbcType=VARCHAR},#{sex,jdbcType=VARCHAR})
- insert>
-
- mapper>
StudentService
-
- public interface StudentService {
-
- /**
- * 新增
- * @param record
- * @return
- */
- int insert(Student record);
-
- /**
- * 查询
- * @param student
- * @return
- */
- List
all(Student student); - }
StudentServiceImpl
- @Service
- public class StudentServiceImpl implements StudentService {
- @Autowired
- private StudentMapper studentMapper;
- @Override
- public int insert(Student record) {
- return studentMapper.insert(record);
- }
-
- @Override
- public List
all(Student student) { - return studentMapper.all(student);
- }
- }
StudentController层
- package com.zking.spboot.controller;
-
- import com.zking.spboot.model.Student;
- import com.zking.spboot.service.StudentService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- @RestController
- @RequestMapping("/student")
- public class StudentController {
-
- @Autowired
- private StudentService studentService;
-
- /**
- * 查询
- * @param student
- * @return
- */
- @RequestMapping("/list")
- public Map
all(Student student){ - List
all = studentService.all(student); - Map
rs=new HashMap<>(); - rs.put("code",200);
- rs.put("list",all);
- return rs;
- }
-
- /**
- * 新增
- * @param student
- * @return
- */
- @RequestMapping("/add")
- public Map
add(Student student){ - int i = studentService.insert(student);
- Map
rs=new HashMap<>(); - if (i>0)
- rs.put("code",200);
- return rs;
- }
-
- }
api中action.js
- /**
- * 对后台请求的地址的封装,URL格式如下:
- * 模块名_实体名_操作
- */
- export default {
- //服务器
- 'SERVER': 'http://localhost:8080/spboot',
- 'ADD':'/student/add',
- 'ALL':'/student/list',
- //获得请求的完整地址,用于mockjs测试时使用
- 'getFullPath': k => {
- return this.SERVER + this[k];
- }
- }
views
- <template>
- <div>
-
-
- <el-form :inline="true">
- <el-form-item label="学生名称">
- <el-input v-model="sname" placeholder="书本名称">el-input>
- el-form-item>
- <el-form-item>
- <el-button type="primary" @click="query">查询el-button>
- <el-button type="primary" @click="open">新增el-button>
- el-form-item>
- el-form>
-
-
- <el-table :data="tableData" style="width: 100%">
- <el-table-column prop="sid" label="学生编号" width="180">
- el-table-column>
- <el-table-column prop="sname" label="学生名称" width="180">
- el-table-column>
- <el-table-column prop="score" label="学生成绩">
- el-table-column>
- <el-table-column prop="sex" label="学生性别">
- el-table-column>
- el-table>
-
-
- <el-dialog title="新增" :visible.sync="dialogFormVisible" @close="close">
- <el-form :model="student" :rules="rules" ref="student">
- <el-form-item prop="sname" label="学生名称" label-width="90px">
- <el-input v-model="student.sname" autocomplete="off">el-input>
- el-form-item>
- <el-form-item prop="score" label="学生成绩" label-width="90px">
- <el-input v-model="student.score" autocomplete="off">el-input>
- el-form-item>
- <el-form-item prop="sex" label="学生性别" label-width="90px">
- <el-select style="width: 100%;" v-model="student.sex" placeholder="请选择书本类型">
- <el-option label="玄幻" value="玄幻">el-option>
- <el-option label="文学" value="文学">el-option>
- el-select>
- el-form-item>
- el-form>
- <div slot="footer" class="dialog-footer">
- <el-button @click="dialogFormVisible = false">取 消el-button>
- <el-button type="primary" @click="save">确 定el-button>
- div>
- el-dialog>
- div>
- template>
-
- <script>
- export default {
- data: function() {
- return {
- ts: new Date().getTime(),
- //搜索框
- sname: '',
- //表格数据
- tableData: [],
- //弹出框是否显示
- dialogFormVisible: false,
- //弹出框数据
- student: {
- sname: '',
- score: '',
- sex: ''
- },
- //表单验证
- rules: {
- sname: [{
- required: true,
- message: '请输入书本名称',
- trigger: 'blur'
- }, ],
- score: [{
- required: true,
- message: '请输入书本价格',
- trigger: 'blur'
- }, ],
- sex: [{
- required: true,
- message: '请选择书本类型',
- trigger: 'change',
- }, ]
- }
- };
- },
- methods: {
- close: function() {
- //清空表单数据
- this.student = {
- sname: '',
- score: '',
- sex: ''
- };
- //清空表单验证
- this.$refs['student'].resetFields();
- },
- //新增方法
- save: function() {
- this.$refs['student'].validate((valid) => {
- if (valid) {
- let url = this.axios.urls.ADD;
- this.axios.post(url, this.student).then(resp => {
- let data = resp.data;
- if (data.code == 200) {
- //关闭弹出框
- this.dialogFormVisible = false;
- //再次查询列表方法
- this.query();
- } else {
- this.$message.error('新增失败!');
- }
- }).catch(err => {
-
- })
- } else {
- console.log('error submit!!');
- return false;
- }
- });
- },
- //查询方法
- query: function() {
- //1.定义查询参数
- let params = {
- sname: this.sname
- };
- //2.获取请求路径
- let url = this.axios.urls.ALL;
- //3.发起ajax请求
- this.axios.post(url, params).then(resp => {
- let data = resp.data;
- this.tableData = data.list;
- console.log(data);
- }).catch(err => {
- console.log(err);
- })
- },
- //打开弹出框的方法
- open: function() {
- this.dialogFormVisible = true;
- }
- }
-
- }
- script>
-
- <style>
- style>
views
查询
新增
views
查询
新增