SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

 

需求分析:

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

第一步  创建数据库

01 用户表

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

02 商品表

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

03 订单表

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

04 创建视图

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

写点数据

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

第二步:使用****生成实体类,实体类的映射文件以及dao接口

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

第三步: 创建项目

我是在Idea中创建maven项目

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.zhiyou100.shop</groupId>
	<artifactId>day3_23_shop</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>day3_23_shop</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!--thymeleaf依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<!--web依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--mysql依赖-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<!--mybatis依赖-->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.2.0</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

如果程序启动后,报数据库的版本问题,则降低数据库的版本即可,并且修改外部文件

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

写外部文件

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

#mysql驱动类,mysql6.X之后驱动类是com.mysql.cj.jdbc.Driver
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#数据库地址serverTimezone=GMT%2B8,解决数据库时间问题
spring.datasource.url=jdbc:mysql://localhost:3306/shop?serverTimezone=GMT%2B8
#用户
spring.datasource.username=root
#密码
spring.datasource.password=123
#读取实体类映射文件,存放到resources目录下的mapper文件夹中
mybatis.mapper-locations=classpath:mapper/*.xml

把****生成的文件复制到项目中

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

指定接口:

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

 

第四步  写用户登录模块

概念:什么是序列化?

                    把对象的状态信息转换为可以存储或传输的形式过程,简单说就是把对象转换为字节形式存储的过程称为对象的序列化

         什么是反序列化?

                    把字节序列转化为对象的过程

1  登录页面     user_login.html :

<!DOCTYPE html>
<html lang="en"xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>

<body>
    <form action="#" id="form1">
        <input type="text" name="userName" placeholder="用户名"></br>
        <input type="text" name="userPass" placeholder="密码"><br>
        <input  type="button" id="bto_login" value="登录"><br>
    </form>

</body>
    <script src="/js/jquery-3.3.1.min.js"></script>
<script>
        //设置点击事件 登录
       $("#bto_login").click(function () {
         //ajax提交
           $.ajax({
               url:"/user/login.do",
               dataType:"json",
               type:"post",
               //序列化表单中的所有数据,以json的格式传到指定的url中
               data:$("#form1").serialize(),
               success:function (result) {
                   //返回0,说明登录成功
                   if(result=="0"){
                       //跳转到商品页面
                       window.location.href="/goods/show.do";
                       //alert("登录成功");
                   }else if (result=="1"){
                        //登录失败
                       alert("用户名或密码错误");
                   }
               }
           })

       })

</script>
</html>

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

2 controller层   UserController

package com.zhiyou100.shop.controller;

import com.zhiyou100.shop.pojo.TbUser;
import com.zhiyou100.shop.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    //注入service
    @Resource
    private IUserService userService;

    @RequestMapping("/login.do")
    @ResponseBody
    //@ResponseBody:相当于调用Response的write方法向浏览器输出数据    
    public String  login(TbUser tbUser, HttpSession session){
        //返回用户,存到session中
        TbUser login_user=userService.login(tbUser);
        //判断是否为空
        if (login_user!=null){
            //存到session中
            session.setAttribute("Login_User",login_user);
            //返回数据
            return "0";
        }else {
            return  "1";
        }

    }


}

3 写service层及实现类

接口:IUserService

package com.zhiyou100.shop.service;

import com.zhiyou100.shop.pojo.TbUser;

public interface IUserService {
    //用户登录
    TbUser login(TbUser tbUser);

}

实现类:UserService

package com.zhiyou100.shop.service.imp;

import com.zhiyou100.shop.mapper.TbUserMapper;
import com.zhiyou100.shop.pojo.TbUser;
import com.zhiyou100.shop.pojo.TbUserExample;
import com.zhiyou100.shop.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService implements IUserService {
    //注入mapper
    @Resource
    private TbUserMapper userMapper;

    /**
     * 根据用户名和密码查询
     * @param tbUser
     * @return
     */

    @Override
    public TbUser login(TbUser tbUser) {
        TbUserExample tbUserExample = new TbUserExample();
        TbUserExample.Criteria criteria = tbUserExample.createCriteria();
        //设置条件
        criteria.andUserNameEqualTo(tbUser.getUserName());
        criteria.andUserPassEqualTo(tbUser.getUserPass());
        criteria.andUserStateEqualTo(1);//用户的状态为可用
        //查询
        List<TbUser> users = userMapper.selectByExample(tbUserExample);
        //判断集合中是否有数据,避免角标越界异常
        if(users.size()>0 &&users!=null){
            //获取第一个数据   因为上边的语句返回值是list
            return users.get(0);
        }
        return null;

    }
}

4 由于在templates下的页面是不可以直接访问的   

写一个控制层,跳转到登录页面

PageController:

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

查看结果:

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

登录成功后跳转到展示所有商品页面

第五步  展示所有商品信息

1 写展示商品信息页面  goods_list.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>商品展示页面</title>
</head>
<body>
    <table border="1px solid pink" cellspacing="0" align="center" >
        <caption style="font-size: larger ">商品列表</caption>
        <tr>
            <th>商品编号</th>
            <th>商品名</th>
            <th>商品价格</th>
            <th>库存量</th>
            <th>商品类型</th>
            <th colspan="3">操作</th>
        </tr>
        <!--遍历结果-->
        <tr th:each="goods:${goods_list}">
            <td th:text="${goods.goodsId}"></td>
            <td th:text="${goods.goodsName}"></td>
            <td th:text="${goods.goodsPrice}"></td>
            <td th:text="${goods.goodsNum}"></td>
            <td th:text="${goods.goodsType}"></td>
            <!--咱们的商品表里没有写购买数量 只是写了个单价  所以购买数量默认是1  正常的项目肯定会规定购买数量的-->
            <td><button href="@{/order/buy.do(goodsId=${goods.goodsId})}">购买</button></td>
            <td><button href="@{/goods/update.do(goodsId=${goods.goodsId})}">修改</button></td>
            <td><button href="@{/goods/delete(goodsId=${goods.goodsId})}">删除</button></td>
        </tr>
    </table>

</body>
</html>

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

2 写控制层  GoodsController

package com.zhiyou100.shop.controller;

import com.zhiyou100.shop.pojo.TbGoods;
import com.zhiyou100.shop.service.IGoodsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private IGoodsService goodsService;
    @RequestMapping("/show.do")
    public String show(ModelMap map){
       List<TbGoods>  goods_list=goodsService.queryAll();
       //存到map中
        map.put("goods_list",goods_list);
        return "goods_list.html";
    }

}

3 写service层和实现类

接口:IGoodsService

package com.zhiyou100.shop.service;

import com.zhiyou100.shop.pojo.TbGoods;

import java.util.List;

public interface IGoodsService {
    //展示所有商品
    List<TbGoods> queryAll();

}

实现类:GoodsService

package com.zhiyou100.shop.service.imp;

import com.zhiyou100.shop.mapper.TbGoodsMapper;
import com.zhiyou100.shop.pojo.TbGoods;
import com.zhiyou100.shop.pojo.TbGoodsExample;
import com.zhiyou100.shop.service.IGoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class GoodsService implements IGoodsService {
   //注入mapper
    @Resource
    private TbGoodsMapper goodsMapper;

    //展示所有商品
    @Override
    public List<TbGoods> queryAll() {
        TbGoodsExample tbGoodsExample = new TbGoodsExample();
        TbGoodsExample.Criteria criteria = tbGoodsExample.createCriteria();
        //设置条件
        criteria.andGoodsStateEqualTo(1);//商品的状态为1:即可用时
        //查询
        return goodsMapper.selectByExample(tbGoodsExample);
    }
}

结果:

SpringBoot+Mybatis+Mysql+Thymeleaf的小练习——订单查询

 

 

 

 

相关文章:

  • 2021-06-09
  • 2022-01-25
  • 2022-01-12
  • 2021-07-20
  • 2022-12-23
  • 2022-12-23
  • 2021-11-11
猜你喜欢
  • 2021-06-18
  • 2021-12-28
  • 2021-10-27
  • 2021-09-11
  • 2021-10-02
  • 2021-05-04
  • 2021-12-09
相关资源
相似解决方案