当前位置: 代码迷 >> 综合 >> # Springboot+Mybatis+thyemleaf 综合商城实战
  详细解决方案

# Springboot+Mybatis+thyemleaf 综合商城实战

热度:79   发布时间:2023-10-31 22:25:18.0

7_19Springboot+Mybatis+thyemleaf综合商城实战(二)


  1. thyemleaf中action的写法
<form id="login-form" th:action="@{/login}">...</form>
  1. 表单提交的时候出现405错误
    在这里插入图片描述
//Requestmapping的默认方法为RequestMethod.POST
@RequestMapping(value = "/add",method = RequestMethod.POST)
//form请求的地址为,表单提交用post
<form class="theme-signin" name="loginform" th:action="@{'/shopcard/add?id='+${product.pId}}" method="post" >
  1. 使用 @ModelAttribute 和 @SessionAttributes 传递和保存数据
 @RequestMapping(value = "/add",method = RequestMethod.POST)public String save(Model model , @RequestParam("id") String id, @RequestParam("num") String num , HttpSession session){
    System.out.println("当前商品编号为"+id+"得到商品的总共的数量为"+num);Product product=productService.selectByPrimaryKey(id);model.addAttribute("product",product);Shopcard shopcard=new Shopcard();shopcard.setpId(id);shopcard.setPnum(Integer.parseInt(num));shopCardService.insert(shopcard);session.setAttribute("num",num);return "/before/introduction";}
//htMl页面取值
  1. Mybatis的关联查询
    MapperExt.xml
<?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.li.shop.mapper.DictionaryMapper"><resultMap id="DictionaryWithProduct" type="com.li.shop.domain.Dictionary" extends="BaseResultMap"><collection property="products" ofType="com.li.shop.domain.Product" javaType="java.util.List"><id column="pid" jdbcType="VARCHAR" property="dId"></id><result column="p_id" jdbcType="VARCHAR" property="pId"></result></collection></resultMap><select id="findAll" resultMap="DictionaryWithProduct" >select d.d_id,d.name,p.product_name,p.d_id as pid  from dictionary dleft join product p on d.d_id = p.d_id</select>
</mapper>
 /*** 前端主页面的商品的展示:当项目启动的时候,前端展示商品页面。*/@RequestMapping(value ="/list",method = RequestMethod.GET)public String show(Model model){
    List<Product> product= productService.listAll();List<Dictionary> dict=dictionaryService.list();List<Dictionary> list=dictionaryService.findAll();for (Dictionary d:list){
    System.out.println("种类为:"+d.getName()+"种类包含的信息为"+d.getProducts());}model.addAttribute("product",product);model.addAttribute("dictionary",dict);System.out.println("已经拿到了dictionarylist");System.out.println("productlist已经拿到"+"product的sieze为"+product.size());return "before/home" ;}
  1. sql查询语句
select p.*,s.s_id,s.u_id,d.name from shopcard s
left join user u on u.s_id=s.s_id
join product p on s.p_id=p.p_id
join dictionary d on p.d_id = d.d_idselect p.product_name,d.name  as dname  from dictionary d
left join product p on d.d_id = p.d_idselect p.*,s.s_id,s.u_id,d.name from shopcard s
left join user u on u.s_id=s.s_id
join product p on s.p_id=p.p_id
join dictionary d on p.d_id = d.d_id
where s.u_id is not null
and p.num is not null
p.p_id
join dictionary d on p.d_id = d.d_id
where s.u_id is not null
and p.num is not null
  相关解决方案