springboot搭建以及与vue交互
springboot搭建
编写配置文件application.yaml
spring:
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
username: scott
password: tiger123
url: jdbc:oracle:thin:@127.0.0.1:1521:ORCL
mybatis:
type-aliases-package: com.elvis.pojo
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath:mybatis/mapper/*.xml
server:
port: 8081
构建实体类
package com.elvis.pojo;
public class Register {
private String account;
private String email;
private String phone;
private String password;
private String name;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
整合mybatis,编写mapper
package com.elvis.mapper;
import com.elvis.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface IStudentMapper {
public Student getStudentByLogin(@Param(value = "email") String email, @Param(value = "password") String password);
}
配置sql的mapper
<?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.elvis.mapper.IStudentMapper">
<select id="getStudentByLogin" resultType="Student">
select * from Student where STUDENT_ID = (select ACCOUNT from REGISTER where EMAIL=#{email} and PASSWORD=#{password})
</select>
</mapper>
##编写controller接口
public class LoginController {
@Autowired
private IStudentMapper iStudentMapper;
@PostMapping("/login")
public String loginStudent(@RequestParam(value = "email") String email, @RequestParam(value = "password") String password){
System.out.println(email);
System.out.println(password);
Student student = iStudentMapper.getStudentByLogin(email, password);
if(student!=null&&!student.getStudentId().isEmpty()){
return "{\"success\":true}";
}else{
return "{\"success\":false}";
}
}
}
跨域请求
如果我们此时使用本地机不同端口测试,如果报错请求失败,请检查在浏览器是否可以访问127.0.0.1,如果被拒绝,则需要在windows设置里面修改:
保证上图设置全部开启,那么请求则会报错跨域请求错误,这是则需要在springboot中添加一个配置类,重写addCorsMappings方法
package com.elvis.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 CrosConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") //添加映射路径,“/**”表示对所有的路径实行全局跨域访问权限的设置
.allowedOriginPatterns("*") //开放哪些ip、端口、域名的访问权限
.allowedMethods( "GET", "POST", "PUT", "OPTIONS", "DELETE") //开放哪些Http方法,允许跨域访问
.allowCredentials(true) //是否允许发送Cookie信息
.maxAge(3600)
.allowedHeaders("*"); //允许HTTP请求中的携带哪些Header信息
}
}
之后在此请求就可以接收到spring boot传回来的接口数据了