8.28 emlboot
创建后台项目
创建spring boot项目
只选这两个就可以。
image.png
如果网速太慢,下载不出来,在setting里调整一下maven。(创建springboot教程里有)
pom里引进依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
springboot配置文件:application.properties
#更改端口号:
#server.port=8089
#配置上下文的路径
server.servlet.context-path=/elm
#连接数据库:用spring提供的工具 不需要配置
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/elm?useUnicode=true&characterEncoding=utf8&useSSL=true
spring.datasource.username = root
spring.datasource.password = root
#扫描mapper下所有因发射文件
mybatis.mapper-locations=classpath:mapper/*.xml
#起路径的别名
mybatis.type-aliases-package=com.foreknow.elmboot.po
#在控制台显示执行的sql
logging.level.org.springframework=debug
logging.level.com.foreknow.elmboot.mapper=debug
跨域的配置文件:WebMvcConfig
解决前后端跨域问题,直接拿来用
import org.springframework.context.annotation.Bean;
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 WebMvcConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
/*
* addMapping:配置可以被跨域的路径,可以任意配置,可以具体到直接请求路径。
* allowedMethods:允许的请求方式,如:POST、GET、PUT、DELETE等。
* allowedOrigins:允许访问的url,可以固定单条或者多条内容,如:"http://www.baidu.com"。前台接口
* allowedHeaders:允许的请求header,可以自定义设置任意请求头信息。
* maxAge:配置预检请求的有效时间
*/
registry.addMapping("/**")
//前端端口号
.allowedOrigins("http://localhost:8081")
.allowCredentials(true)
.allowedMethods("GET", "POST", "DELETE", "PUT","PATCH")
.allowedHeaders("*")
.maxAge(36000);
}
};
}
}
简单的单表操作时,dao层的写法:以Business表为例
可以写映射文件mapper,直接在接口的方法上,用注解的方法写sql
package com.foreknow.elmboot.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.foreknow.elmboot.po.Business;
@Mapper
public interface BusinessMapper {
@Select("select * from business where orderTypeId=#{orderTypeId} order by businessId")
public List<Business> listBusinessByOrderTypeId(Integer orderTypeId);
@Select("select * from business where businessId=#{businessId}")
public Business getBusinessById(Integer businessId);
}
service
应为现在还没有什么业务,所以service和dao差不多
controller
controller里要从前台获取到参数,有两种方法
- 第一种:老师的方法
直接把参数用对象的方式传过来
import com.foreknow.elmboot.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/BusinessController")
public class BusinessController {
@Autowired
private BusinessService businessService;
@RequestMapping("/listBusinessByOrderTypeId")
public List<Business> listBusinessByOrderTypeId(Business business) throws Exception{
return businessService.listBusinessByOrderTypeId(business.getOrderTypeId());
}
}
-
第二种:@RequestParam
把前台传过来的参数,以注解@RequestParam赋值给不同名字的参数orderTypeId,之所以要改成这个名字,是因为要和bean里的属性名字保持一致。
image.png
写法
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/BusinessController")
public class BusinessController {
@Autowired
private BusinessService businessService;
@RequestMapping("/listBusinessByOrderTypeId")
public List<Business> listBusinessByOrderTypeId(@RequestParam(required=false)Integer orderTypeId, Model model) throws Exception{
return businessService.listBusinessByOrderTypeId(orderTypeId);
}
}
注解@RequestParam的语法:
语法:@RequestParam(value=”参数名”,required=”true/false”,defaultValue=””)
value:参数名
required:是否包含该参数,默认为true,表示该请求路径中必须包含该参数,如果不包含就报错。
defaultValue:默认参数值,如果设置了该值,required=true将失效,自动为false,如果没有传该参数,就使用默认值
传递参数
前台向后台传递参数,前台使用的方法是qs提供的插件序列化传递,序列化的传递方式是类似于
userId=1&passworld=123456
这种名值对的模式。
而后台获取的方式是以key和value的方式,servlet里request.getParameter获取,现在spring给封装起来了,不需要再调用这个方法。前台定义对象的话,属性要和后台Bean里的属性名字相同,之后前台直接向后台传对象,后台就能获取到参数了。
image.png
bean里定义的属性:
image.png
前台调用方法并传递参数:
image.png
image.png
多表
购物车表与商家id和食品id是多对一的关系,在bean里的表现方法不变,但在持久层会有一些变化,以前会写很多东西,现在单表操作的方法可以直接用注解,而多表操作的方法可以在映射mapper文件夹里进行映射。
持久层接口中:
@Mapper
public interface CartMapper {
/**
*查询购物车信息
* @param cart
* @return
*/
public List<Cart> listCart(Cart cart);
/**
* 添加购物车
* @param cart
* @return
*/
@Insert("insert into cart(foodId,businessId,userId,quantity) values(#{foodId},#{businessId},#{userId},1)")
public int saveCart(Cart cart);
@Update("update cart set quantity=#{quantity} where foodId=#{foodId} and businessId=#{businessId} and userId=#{userId}")
public int updateCart(Cart cart);
/**
* 删除购物车
* @param cart
* @return
*/
public int removeCart(Cart cart);
}
映射文件:
和以前写的方法不同,这词用的分步查询,老师的教程:https://www.jianshu.com/p/78261df9daea
不需要再用数据库的属性名和bean的属性一一关联,可以分步查询,直接用属性select等这种属性名直接关联到其他持久层的方法上。
<?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.foreknow.elmboot.mapper.CartMapper">
<select id="listCart" resultMap="cartInfoMap" parameterType="com.foreknow.elmboot.po.Cart">
select * from cart
<where>
userId=#{userId}
<if test="businessId!=null and businessId!='' ">
and businessId=#{businessId}
</if>
</where>
</select>
<resultMap id="cartInfoMap" type="com.foreknow.elmboot.po.Cart">
<result column="cartId" property="cartId"/>
<result column="foodId" property="foodId"/>
<result column="businessId" property="businessId"/>
<result column="userId" property="userId"/>
<result column="quantity" property="quantity"/>
<association property="food" column="foodId"
select="com.foreknow.elmboot.mapper.FoodMapper.getFoodById" />
<association property="business" column="businessId"
select="com.foreknow.elmboot.mapper.BusinessMapper.getBusinessById" />
</resultMap>
<delete id="removeCart" parameterType="com.foreknow.elmboot.po.Cart">
delete from cart
<where>
userId=#{userId} and businessId=#{businessId}
<if test="foodId!=null and foodId!='' ">
and foodId=#{foodId}
</if>
</where>
</delete>
</mapper>