pojo层 层 层 层 层 层 层 层 层 层 层 层 层 层 层
package com.cy.pj.goods.pojo;
import java.util.Date;
public class Goods {
    private Long id;//id bigint primary key auto_increment
    private String name;//name varchar(100) not null
    private String remark;//remark text
    private Date createdTime;//createdTime datetime
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getRemark() {
        return remark;
    }
    public void setRemark(String remark) {
        this.remark = remark;
    }
    public Date getCreatedTime() {
        return createdTime;
    }
    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }
    @Override
    public String toString() {
        return "Goods [id=" + id + ", name=" + name + ", remark=" + remark + ", createdTime=" + createdTime + "]";
    }
}
dao层 层 层 层 层 层 层 层 层 层 层 层 层 层
/**
  @Mapper 用于描述(做标记)数据层访问接口,用于告诉mybatis框架,
   使用此注解描述的接口要由底层为创建实现类.在实现类中基于mybatis
  API实现与数据库的交互.这个类的对象最后会交给spring管理.
  FAQ
  我们添加了MyBatis-Spring-Boot-Starter这个依赖以后,此依赖内部做了什么?
 /
@Mapper
public interface GoodsDao {//还有一些企业这个GoodsDao的名字会定义为GoodsMapper
      @Insert("insert into tbgoods(name,remark,createdTime) values (#{name},#{remark},#{createdTime})")
      int insertGoods(Goods goods);
      @Update("update tbgoods set name=#{name},remark=#{remark} where id=#{id}")
      int updateGoods(Goods goods);
      /**基于商品id查询商品信息/
      @Select("select  from tbgoods where id={id}")
      Goods findById(Integer id);
      @Select("select  from tb_goods")
      List<Goods> findGoods();
      /**
      基于id执行商品信息的删除,在mybatis中假如SQL映射语句比较简单
      可以直接在dao方法上以注解方式进行定义.
  @param id 商品id
  @return 删除的行数/
      @Delete("delete from tbgoods where id=#{id}")
      int deleteById(Integer id);
      /**
      基于多个id执行商品删除业务
  @param ids 可变参数,用于接收传入的商品id值
  @return 删除行数
 /int deleteObjects(@Param("ids")Integer...ids);
}
service层 层 层
/**
  商品模块的业务层接口,负责具体业务标准的定义
  @author pc
 */
public interface GoodsService {
      Goods findById(Integer id);
      int saveGoods(Goods goods);
      int updateGoods(Goods goods);
      int deleteById(Integer id);
      List<Goods> findGoods();
}
service层 imp实现层 imp实现层 imp实现层 imp实现层
/***
  业务层对象,后续会在此对象中执行:
  1)核心业务(例如,点击购买商品信息,要生成点单项信息,扣减库存,....)

  2)扩展业务(例如,事务控制,权限控制,日志记录,。。。。)
 /
@Service //是一个特殊的@Component
public class GoodsServiceImpl implements GoodsService {
    private static final Logger  log=    LoggerFactory.getLogger(GoodsServiceImpl.class);
    @Autowired
    private GoodsDao goodsDao;
    @Override
    public Goods findById(Integer id) {
        return goodsDao.findById(id);
    }
    @Override
    public int saveGoods(Goods goods) {
        goods.setCreatedTime(new java.util.Date());
        return goodsDao.insertGoods(goods);
    }
    @Override
    public int updateGoods(Goods goods) {
        return goodsDao.updateGoods(goods);
    }
    @Override
    public List<Goods> findGoods() {
        return goodsDao.findGoods();
    }
    @Override
    public int deleteById(Integer id) {
       long t1=System.currentTimeMillis();
       int rows=goodsDao.deleteById(id);
       long t2=System.currentTimeMillis();
//System.out.println(log.getClass().getName());
       log.info("deleteById execute time : {}",(t2-t1));
       return rows;
    }
}
Controller 层 层 层 层 层 层 层 层 层 层 层
@Controller //@Service,@Component
@RequestMapping("/goods/")
public class GoodsController {
    //has a+DI
    @Autowired
    private GoodsService goodsService;
    @RequestMapping("doFindById/{id}")
    public String doFindById(@PathVariable Integer id,Model model) {
        Goods goods=goodsService.findById(id);
        model.addAttribute("goods",goods);
        return "goods-update";
    }
    @RequestMapping("doGoodsAddUI")
    public String doGoodsAddUI() {
        return "goods-add"
    }
    @RequestMapping("doSaveGoods")
    public String doSaveGoods(Goods goods) {//"name=aaa&&remark=aaaaaaaaa"
        goodsService.saveGoods(goods);
        return "redirect:/goods/doGoodsUI";
    }
    @RequestMapping("doUpdateGoods")
    public String doUpdateGoods(Goods goods) {
        goodsService.updateGoods(goods);
        return "redirect:/goods/doGoodsUI";
    }
    /**
   基于商品id执行商品删除操作
  @param id 商品id,一定要与客户端传过来的参数id名相同.
  @return 重定向到doGoodsUI
  rest风格:一种软件架构编码风格,其设计的目的主要是在异构系统之间实现兼容(跨平台)
  rest风格的url的定义:{a}/{b}/{c},这里的a,b,c分别为变量
    假如希望方法参数的值来自rest风格的url,
    可以使用@PathVariable注解对方法参数进行描述(方法参数名需要和url中{}内部变量名相同)/
    @RequestMapping("doDeleteById/{id}")
    public String doDeleteById(@PathVariable Integer id) {
        goodsService.deleteById(id);
        return "redirect:/goods/doGoodsUI";
    }//这里redirect表示重定向,后面的第一个"/"默认位置为localhost:port/context-path/
    @RequestMapping("doGoodsUI")
    public String doGoodsUI(Model model) {//ModelAndView中的model对象
         //调用业务层方法获取商品信息
         List<Goods> list=
         goodsService.findGoods();
         //将数据存储到请求作用域
         model.addAttribute("goods", list);
         return "goods";//viewname
        //FAQ:
        //1)返回的viewname会给谁?谁调用doGoodsUI方法就给谁(DispatcherServlet).
        //2)谁负责解析viewname?ViewResolver(ThymleafViewResolver)
        //3)viewname解析的结果会响应到哪里?(prefix+viewname+suffix会响应到客户端)
    }
}
sql语句......
<mapper namespace="com.cy.pj.goods.dao.GoodsDao">
    <delete id="deleteObjects">
         delete from tb_goods
         where id in <!-- (1,2,3,4,5) -->
         <foreach collection="array" open="(" close=")" separator="," item="id">
             #{id}
         </foreach>
     </delete>
  </mapper>
html页面 页面 页面 页面 页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
  <h1>The Goods Page</h1>
<a th:href="@{/goods/doGoodsAddUI}">添加商品</a>
<table>
  <thead>
    <tr>
      <th>id</th>
      <th>name</th>
      <th>remark</th>
      <th>createdTime</th>
      <th colspan="2">operation</th>
    </tr>
  </thead>
  <tbody>
    <tr th:each="g:${goods}">
      <td th:text="${g.id}">1</td>
      <td th:text="${g.name}">AAAAAAA</td>
      <td th:text="${g.remark}">AA....</td>
      <td th:text="${#dates.format(g.createdTime, 'yyyy/MM/dd HH:mm')}">2020/08/31</td>
      <td><a href="#" th:href="@{/goods/doDeleteById/{id}(id=${g.id})}">delete</a></td>
      <td><a href="#" th:href="@{/goods/doFindById/{id}(id=${g.id})}">update</a></td>
    </tr>
  </tbody>
</table>
</body>
</html>
sql 添加语句
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
  ul li {list-style-type: none}
</style>
</head>
<body>
   <h1>The Goods Add Page</h1>
   <form th:action="@{/goods/doSaveGoods}" method="post">
      <ul>
        <li>name:
        <li><input type="text" name="name">
        <li>remark:
        <li><textarea rows="3" cols="30" name="remark"></textarea>
        <li><input type="submit" value="Save Goods">
       </ul>
   </form>
</body>
</html>
修改sql语句:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
  ul li {list-style-type: none}
</style>
</head>
<body>
   <h1>The Goods Update Page</h1>
   <form th:action="@{/goods/doUpdateGoods}" method="post">
      <input type="hidden" name="id" th:value="${goods.id}">
      <ul>
        <li>name:
        <li><input type="text" name="name" th:value="${goods.name}">
        <li>remark:
        <li><textarea rows="3" cols="30" name="remark" th:text="${goods.remark}"></textarea>
        <li><input type="submit" value="Update Goods">
       </ul>
   </form>
</body>
</html>
配置文件 
# server
server.port=80
#server.servlet.context-path=/
# close banner
spring.main.banner-mode=off
# Spring DataSource
spring.datasource.url=jdbc:mysql:///dbgoods?serverTimezone=GMT%2B8&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
# spring mybatis
mybatis.mapper-locations=classpath:/mapper/*/*.xml
# spring thymleaf
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/pages/
spring.thymeleaf.suffix=.html
# spring log
logging.level.com.cy=debug

CV战士
7 声望1 粉丝