Vue+Mysql+SpringBoot简单增删改查

关于Spring篇

关于mybatis+maven基本知识掌握总结

maven中spring+springmvc+mybatis整合详细配置

SpringBoot+Mybatis+Redis基础配置

这两天写的一个补考作业,敲完还是记录下。不然一个月后没敲代码又忘记了,废话多说,需要项目源码自己拿。

链接:https://pan.baidu.com/s/1RQ1rD9fARI37bhCHwLLVOg 
提取码:gio8 
先贴张效果图,GIF图没下软件懒得弄了。自己试验。

Vue+Mysql+SpringBoot简单增删改查_第1张图片

首先需要你有一定的vue和springboot基础,springboot可以先看看我这篇博客,可能没讲的细,但是问题点我是提到了。

https://blog.csdn.net/qq_41520636/article/details/87192263

老规矩,先说说这个东西遇到的麻烦。

问题一:

在使用vue和axios时,对接后台的post请求,后台一直接收null值。几近百度没法后,选择放弃治疗。换了GET请求方式,有大佬可以解决,麻烦再私信或评论告知。谢谢!

--------------20200417更新----------------------------

此问题已解决!

解决方案链接:https://blog.csdn.net/qq_41520636/article/details/104726763

问题二:

其实只是我忘记vue怎么使用的问题,我想要复选框选中的值,而我在data中写的v-model对应值是

checkData:''

这是错误的写法,不然会点击一个复选框,其他复选框全部选上。

正确的做法是

checkData:[]

问题三:

我使用的是自动生成代码generator辅助,所以百度了很多东西,要安装神马插件,有这么麻烦吗?pom.xml里的配置也是。

这个不算问题吧,只是我被误导了而已。

直接写java运行多好,简单方便易捷。

问题四:

就是我设计数据库时,是按照兴趣一对多的关系来的,因为以前的代码忘干净了,所以一时脑袋卡壳,做不出来。

从controller中我想要的学生表,通过List直接在

@ResponseBody
@RequestMapping(value ="/addStudent")
public ResponseCode addStudent(@Valid Student student){
    System.out.println(student.toString());
    int insert = studentService.insert(student);
    return new ResponseCode(insert);
}
public class Student implements Serializable {
private List hobby;

这个list直接通过Vue+Mysql+SpringBoot简单增删改查_第2张图片

hobby.id直接添加进数据,可惜失败了。获取到的值一直是null值,退而次之,我只能单独添加一个hobby参数来获取。

问题五:

关于请求跨域问题,我配置了下,才解决问题。

以上差不多就是我遇到的问题了,下面是代码贴。

先贴代码自动生成

package org.lanqiao.test;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TestGenerator
 * @Description TODO
 * @Author lisonglin
 * @Date 2018/12/5 17:00
 * @Version 1.0
 */
public class TestGenerator {

    /***
     * 功能描述:
     *  调用generatorConfig.xml
     * @param: []
     * @return: void
     * @auther: 李松林
     * @date: 2018/12/8 19:50
     */
    public void generator(){
        List warnings=new ArrayList();
        //更新数据库时,请修改项目的xml配置绝对路径
        File file=new File("E:\\MyJAVA\\SpringMakeup\\src\\main\\conf\\generatorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = null;
        try {
            config = cp.parseConfiguration(file);
            DefaultShellCallback shellCallback = new DefaultShellCallback(true);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
            myBatisGenerator.generate(null);
            System.out.println(warnings);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        try {
            TestGenerator generatorSqlmap = new TestGenerator();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}





    

        
        

        

        
        
            
            
            
            
            
        
        
        
        
        

        
        
            
        

        
        
            
            
            
            
        
        
        
            
            
        
        
        
            
            
        


        
        
        
        
        
            
            
            
            
            
            
            
        

项目包图

Vue+Mysql+SpringBoot简单增删改查_第3张图片Vue+Mysql+SpringBoot简单增删改查_第4张图片


#热部署
#开启
spring.devtools.restart.enabled=true 
#监听目录
spring.devtools.restart.additional-paths=src/main/java 

#mybatis
#mybatis-config.xml配置文件的路径
#mybatis.configLocation=classpath:mybatis-config.xml
#SQL语句映射文件
#mybatis.mapper-locaitons= classpath*:com/example/mapper/*.xml
mybatis.mapper-locations=classpath*:org/lanqiao/mapper/*.xml
# 类的别名的包
mybatis.type-aliases-package=org.lanqiao.model
#驼峰命名法
mybatis.configuration.mapUnderscoreToCamelCase=true
#允许返回多个结果集
mybatis.configuration.multipleResultSetsEnabled=true
#使用jdbc的getGeneratedKeys获取数据库自增主键值
mybatis.configuration.useGeneratedKeys=true
#日志
#mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#延迟加载总开关( 查询时,关闭关联对象即时加载以提高性能)
mybatis.configuration.lazyLoadingEnabled=false
#侵入懒加载,设置为false则按需加载,否则会全部加载
mybatis.configuration.aggressiveLazyLoading=false

spring.datasource.url=jdbc:mysql://localhost:3306/sys?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.3.RELEASE
         
    
    org.lanqiao
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.0
        

        
        
            mysql
            mysql-connector-java
            5.1.21
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            org.springframework.boot
            spring-boot-devtools
            true
        
        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
        
        
        
            org.mybatis.generator
            mybatis-generator-core
            1.3.6
        
        
            org.junit.jupiter
            junit-jupiter-api
            RELEASE
            compile
        
        
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.7.4
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


 

package org.lanqiao.controller;

import org.lanqiao.model.Hobbys;
import org.lanqiao.model.ResponseCode;
import org.lanqiao.model.Student;
import org.lanqiao.service.HobbysService;
import org.lanqiao.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName MainController
 * @Description TODO
 * @Author lisonglin
 * @Date 2019/3/28 0:03
 * @Version 1.0
 */
@Controller
public class MainController {

    @Autowired
    StudentService studentService;
    @Autowired
    HobbysService hobbysService;

    @ResponseBody
    @RequestMapping("/getAll")
    public List getStudent(){
        List students = studentService.getAll();
        System.out.println(students);
        return students;
    }

    @ResponseBody
    @RequestMapping(value ="/addStudent")
    public ResponseCode addStudent(@Valid Student student, String hobbys){
        System.out.println(student.toString());
        System.out.println("兴趣是:"+hobbys);
        int insert = studentService.insert(student,hobbys);
        return new ResponseCode(insert);
    }

    @ResponseBody
    @RequestMapping(value ="/updateStudent")
    public ResponseCode updateStudent(@Valid Student student,String hobbys){
        System.out.println(student+"兴趣:"+hobbys);
        int i = studentService.updateByPrimaryKey(student, hobbys);
        return new ResponseCode(i);
    }

    @ResponseBody
    @RequestMapping(value ="/deleteStudent")
    public ResponseCode deleteStudent(Integer studentId){
        int i = studentService.deleteByPrimaryKey(studentId);
        return new ResponseCode(i);
    }

    @ResponseBody
    @RequestMapping("/getHobbys")
    public List getHobbys(){
        return hobbysService.getHobbys();
    }

    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }

}
package org.lanqiao.dao;

import org.lanqiao.mapper.StudentHobbysMapper;
import org.lanqiao.mapper.StudentMapper;
import org.lanqiao.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ClassName StudentDao
 * @Description TODO
 * @Author lisonglin
 * @Date 2019/3/27 23:51
 * @Version 1.0
 */
@Repository
public class StudentDao {

    @Autowired
    StudentMapper studentMapper;
    @Autowired
    StudentHobbysMapper studentHobbysMapper;

    public int deleteByPrimaryKey(Integer id){
        studentMapper.deleteByPrimaryKey(id);
        studentHobbysMapper.deleteHobbyIdToStudent(id);
        return 1;
    };

    public int insertStudent(Student student, String hobbys){
        studentMapper.insert(student);
        String regex = ",";
        String[] arr = hobbys.split(regex);
        for(int i=0;i getAll() {
        return studentMapper.getAll();
    }

}
package org.lanqiao.dao;

import org.lanqiao.mapper.HobbysMapper;
import org.lanqiao.model.Hobbys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ClassName HobbysDao
 * @Description TODO
 * @Author lisonglin
 * @Date 2019/3/30 15:54
 * @Version 1.0
 */
@Repository
public class HobbysDao {

    @Autowired
    HobbysMapper hobbysMapper;

    public List getHobbys() {
        return hobbysMapper.getHobbys();
    }
}



  







    
        insert into stu_hobby (id,hobby) values (#{studentId},#{hobbyId})
    

    
        delete from stu_hobby where id=#{studentId}
    





  
    
    
    
    
    
  
  
    id, name, sex, age, score
  
  
    select s.id sid,s.`name`,s.sex,s.age,s.score,h.id,h.hobby
    from student s
    join stu_hobby sh
    on s.id=sh.id
    join hobbys h
    on sh.hobby=h.id
  

  

  
    
    
    
    
    
    
      
      
    
  

  
  
    delete from student
    where id = #{id,jdbcType=INTEGER}
  
  
    
      SELECT LAST_INSERT_ID()
    
    insert into student (name, sex, age,
      score)
    values (#{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER},
      #{score,jdbcType=DOUBLE})
  


  
    
      SELECT LAST_INSERT_ID()
    
    insert into student
    
      
        name,
      
      
        sex,
      
      
        age,
      
      
        score,
      
    
    
      
        #{name,jdbcType=VARCHAR},
      
      
        #{sex,jdbcType=VARCHAR},
      
      
        #{age,jdbcType=INTEGER},
      
      
        #{score,jdbcType=DOUBLE},
      
    
  

  
    update student
    
      
        name = #{name,jdbcType=VARCHAR},
      
      
        sex = #{sex,jdbcType=VARCHAR},
      
      
        age = #{age,jdbcType=INTEGER},
      
      
        score = #{score,jdbcType=DOUBLE},
      
    
    where id = #{id,jdbcType=INTEGER}
  

  
    update student
    set name = #{name,jdbcType=VARCHAR},
      sex = #{sex,jdbcType=VARCHAR},
      age = #{age,jdbcType=INTEGER},
      score = #{score,jdbcType=DOUBLE}
    where id = #{id,jdbcType=INTEGER}
  

package org.lanqiao.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * 解决跨域请求配置
 * @ClassName AccessConfig
 * @Description TODO
 * @Author lisonglin
 * @Date 2019/3/30 22:27
 * @Version 1.0
 */
@Configuration
public class AccessConfig extends WebMvcConfigurationSupport {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600);
    }

}
package org.lanqiao.config;

import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName MapperScannerConfig
 * @Description TODO
 * @Author lisonglin
 * @Date 2019/3/27 22:43
 * @Version 1.0
 */
@Configuration
public class MapperScannerConfig {

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        //指定xml配置文件的路径
        mapperScannerConfigurer.setBasePackage("org.lanqiao.mapper");
        return mapperScannerConfigurer;
    }

}



    
    vue增删改查


    
学生基本信息
姓名
性别
年龄
成绩
兴趣




选中 姓名 性别 年龄 成绩 兴趣 操作
{{1 + index}} {{items.name}} {{items.sex}} {{items.age}} {{ items.score }} {{item.hobby}}

以上基本重要代码都贴出来,详细看源码。

你可能感兴趣的:(Spring框架篇)