8.28 emlboot
創(chuàng)建后臺(tái)項(xiàng)目
創(chuàng)建spring boot項(xiàng)目
只選這兩個(gè)就可以。

如果網(wǎng)速太慢,下載不出來,在setting里調(diào)整一下maven。(創(chuàng)建springboot教程里有)
pom里引進(jìn)依賴
<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
#更改端口號(hào):
#server.port=8089
#配置上下文的路徑
server.servlet.context-path=/elm
#連接數(shù)據(jù)庫(kù):用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下所有因發(fā)射文件
mybatis.mapper-locations=classpath:mapper/*.xml
#起路徑的別名
mybatis.type-aliases-package=com.foreknow.elmboot.po
#在控制臺(tái)顯示執(zhí)行的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:配置可以被跨域的路徑,可以任意配置,可以具體到直接請(qǐng)求路徑。
* allowedMethods:允許的請(qǐng)求方式,如:POST、GET、PUT、DELETE等。
* allowedOrigins:允許訪問的url,可以固定單條或者多條內(nèi)容,如:"http://www.baidu.com"。前臺(tái)接口
* allowedHeaders:允許的請(qǐng)求header,可以自定義設(shè)置任意請(qǐng)求頭信息。
* maxAge:配置預(yù)檢請(qǐng)求的有效時(shí)間
*/
registry.addMapping("/**")
//前端端口號(hào)
.allowedOrigins("http://localhost:8081")
.allowCredentials(true)
.allowedMethods("GET", "POST", "DELETE", "PUT","PATCH")
.allowedHeaders("*")
.maxAge(36000);
}
};
}
}
簡(jiǎn)單的單表操作時(shí),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
應(yīng)為現(xiàn)在還沒有什么業(yè)務(wù),所以service和dao差不多
controller
controller里要從前臺(tái)獲取到參數(shù),有兩種方法
- 第一種:老師的方法
直接把參數(shù)用對(duì)象的方式傳過來
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
把前臺(tái)傳過來的參數(shù),以注解@RequestParam賦值給不同名字的參數(shù)orderTypeId,之所以要改成這個(gè)名字,是因?yàn)橐蚥ean里的屬性名字保持一致。
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=”參數(shù)名”,required=”true/false”,defaultValue=””)
value:參數(shù)名
required:是否包含該參數(shù),默認(rèn)為true,表示該請(qǐng)求路徑中必須包含該參數(shù),如果不包含就報(bào)錯(cuò)。
defaultValue:默認(rèn)參數(shù)值,如果設(shè)置了該值,required=true將失效,自動(dòng)為false,如果沒有傳該參數(shù),就使用默認(rèn)值
傳遞參數(shù)
前臺(tái)向后臺(tái)傳遞參數(shù),前臺(tái)使用的方法是qs提供的插件序列化傳遞,序列化的傳遞方式是類似于
userId=1&passworld=123456
這種名值對(duì)的模式。
而后臺(tái)獲取的方式是以key和value的方式,servlet里request.getParameter獲取,現(xiàn)在spring給封裝起來了,不需要再調(diào)用這個(gè)方法。前臺(tái)定義對(duì)象的話,屬性要和后臺(tái)Bean里的屬性名字相同,之后前臺(tái)直接向后臺(tái)傳對(duì)象,后臺(tái)就能獲取到參數(shù)了。

bean里定義的屬性:

前臺(tái)調(diào)用方法并傳遞參數(shù):


多表
購(gòu)物車表與商家id和食品id是多對(duì)一的關(guān)系,在bean里的表現(xiàn)方法不變,但在持久層會(huì)有一些變化,以前會(huì)寫很多東西,現(xiàn)在單表操作的方法可以直接用注解,而多表操作的方法可以在映射mapper文件夾里進(jìn)行映射。
持久層接口中:
@Mapper
public interface CartMapper {
/**
*查詢購(gòu)物車信息
* @param cart
* @return
*/
public List<Cart> listCart(Cart cart);
/**
* 添加購(gòu)物車
* @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);
/**
* 刪除購(gòu)物車
* @param cart
* @return
*/
public int removeCart(Cart cart);
}
映射文件:
和以前寫的方法不同,這詞用的分步查詢,老師的教程:http://www.itdecent.cn/p/78261df9daea
不需要再用數(shù)據(jù)庫(kù)的屬性名和bean的屬性一一關(guān)聯(lián),可以分步查詢,直接用屬性select等這種屬性名直接關(guān)聯(lián)到其他持久層的方法上。
<?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>
