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

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 d
        left 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_id


select p.product_name,d.name  as dname  from dictionary d
left join product p on d.d_id = p.d_id

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_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
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全栈程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值