SpringBoot、Mybatis、Vue整合出现的问题

这段时间由于实训要求写一个项目,所以一直没有弄笔记了,接下来说说项目期间出现的一些问题

MyBatis和SpringBoot整合期间需要注意的是创建对象和连接数据库都是交给Spring来管理进行创建对象,所以不需要Utils工具类,也不需要config.xml。Mapper接口处没忘了注释,不然识别不到Mapper层,无法自动注入.

在配置文件application.properties下配置mybatis即可。

第一行即配置实体类别名(entity整个包),默认类名首字母大写。第二行即mybatis的映射文件所在位置。第三行即设置驼峰命名

mybatis.type-aliases-package= com.wang.entity
mybatis.mapper-locations= classpath:com/wang/dao/*.xml
mybatis.configuration.map-underscore-to-camel-case= true

前后端完全分离出现的跨域问题

前后端完成分离时出现跨域问题,不能相互交互,解决方法在后端写一个配置类

package com.wang.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 MyWebMvcConfig implements WebMvcConfigurer {
    //解决跨域问题
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedHeaders("*")
                .allowedMethods("*")
                .maxAge(1800)
                .allowedOrigins("*");
    }
}

配置后解决了跨域问题:但是后面又出现了一个问题

Session不一致问题

需求是后面根据Session来获取登录用户的信息,但是发现后端获取的登入时存在Session的数据一直为null。反复测试才发现后端存数据的Session和前端返回的Session不是同一个!!

(之前学习Session知道,Session是存在服务器的,是将SessionId发送给客户端,客户端发送请求时,将SessionId放入Cookie中,服务器根据Id来判断Session进行存取数据)但是此时发现前后端的Id不同,所以识别不出,不能取数据。

百度基本上都是说,在前端的main.js配置

即可

但是配置之后,开始报错CORS错误

 大概还是跨域问题,但是不配置这句话的话没问题,且可以跨域访问。于是百度了很多任然没有解决方法,于是换了思路,让前端保存登入用户的信息,后端需要的时候直接传过去,于是使用vuex的状态保存

什么是vuex?

即相当于公共仓库,保存所有组件都能公用的数据

使用自定义模板:

在src下创建comment.js,main.js引用

//获取当前时间(XXXX-XX-XX)
export function getCurDate() {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1;
  var day = now.getDate();
  month = month < 10 ? "0" + month : month;
  day = day < 10 ? "0" + day : day;
  return year + "-" + month + "-" + day;
}
//向sessionStorage中存储一个JSON对象
export function setSessionStorage(keyStr, value) {
  sessionStorage.setItem(keyStr, JSON.stringify(value));
}
//从sessionStorage中获取一个JSON对象(取不到时返回null)
export function getSessionStorage(keyStr) {
  var str = sessionStorage.getItem(keyStr);
  if (str == '' || str == null || str == 'null' || str == undefined) {
    return null;
  } else {
    return JSON.parse(str);
  }
}
//从sessionStorage中移除一个JSON对象
export function removeSessionStorage(keyStr) {
  sessionStorage.removeItem(keyStr);
}
//向localStorage中存储一个JSON对象
export function setLocalStorage(keyStr, value) {
  localStorage.setItem(keyStr, JSON.stringify(value));
}
//从localStorage中获取一个JSON对象(取不到时返回null)
export function getLocalStorage(keyStr) {
  var str = localStorage.getItem(keyStr);
  if (str == '' || str == null || str == 'null' || str == undefined) {
    return null;
  } else {
    return JSON.parse(str);
  }
}
//从localStorage中移除一个JSON对象
export function removeLocalStorage(keyStr) {
  localStorage.removeItem(keyStr);
}
//导入自定义模块
import {
  getCurDate,
  setSessionStorage,
  getSessionStorage,
  removeSessionStorage,
  setLocalStorage,
  getLocalStorage,
  removeLocalStorage
} from './common.js'
Vue.prototype.$axios = axios;
Vue.prototype.$qs = qs;
Vue.prototype.$getCurDate = getCurDate;
Vue.prototype.$setSessionStorage = setSessionStorage;
Vue.prototype.$getSessionStorage = getSessionStorage;
Vue.prototype.$removeSessionStorage = removeSessionStorage;
Vue.prototype.$setLocalStorage = setLocalStorage;
Vue.prototype.$getLocalStorage = getLocalStorage;
Vue.prototype.$removeLocalStorage = removeLocalStorage;

如果要使用的时候:this.$getSessionStorage(‘user)即可

关于assets和static两个文件的区别

前者在项目编译的过程中会被webpack处理解析为模板依赖,只支持相对路径。而后者不会被webpack处理,会直接复制到dist/static路径下,所以必须使用绝对路径进行引用这些资源

vue动态绑定图片问题

 

 

直接浏览器访问后端的图片

SpringBoot配置文件中设置资源的绝对路径

spring.web.resources.static-locations=file:E:/SpringBootTest/src/main/resources/userImg

 

访问成功

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要在Spring Boot项目中添加MyBatisMyBatis-Spring-Boot-Starter依赖,这可以通过在pom.xml文件中添加以下代码实现: ```xml <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> ``` 然后,你需要配置MyBatis的数据源,这可以通过在application.properties文件中添加以下代码实现: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 接下来,你需要创建数据访问层DAO,这可以通过在Spring Boot项目中创建一个包并添加以下代码实现: ```java @Mapper public interface MyDAO { List<Map<String, Object>> queryData(); } ``` 然后,你需要在MyBatis的Mapper中编写SQL语句,这可以通过在resources/mapper目录下创建一个XML文件并添加以下代码实现: ```xml <mapper namespace="com.example.demo.dao.MyDAO"> <select id="queryData" resultType="java.util.Map"> SELECT * FROM data_table </select> </mapper> ``` 最后,你需要在Spring Boot的Controller中编写代码,将查询结果传递给前端页面,这可以通过在Spring Boot项目中创建一个Controller并添加以下代码实现: ```java @RestController public class MyController { @Autowired private MyDAO myDAO; @GetMapping("/data") public List<Map<String, Object>> getData() { return myDAO.queryData(); } } ``` 然后,在Vue项目中使用axios库来调用Spring Boot的Controller,获取数据并使用Echarts进行可视化展示,这可以通过在Vue项目中添加以下代码实现: ```html <template> <div id="chart" style="width: 100%; height: 400px;"></div> </template> <script> import axios from 'axios' import echarts from 'echarts' export default { mounted() { axios.get('/data').then(res => { const data = res.data const chartData = [] for (let i = 0; i < data.length; i++) { chartData.push([data[i].name, data[i].value]) } const chart = echarts.init(document.getElementById('chart')) chart.setOption({ title: { text: 'Echarts 数据可视化' }, tooltip: {}, xAxis: { type: 'category', data: chartData.map(item => item[0]) }, yAxis: { type: 'value' }, series: [{ data: chartData.map(item => item[1]), type: 'bar' }] }) }) } } </script> ``` 这样,你就可以在Vue中使用Echarts对Spring Boot和MyBatis查询结果进行可视化展示了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值