关于springboot:第五天的代码

44次阅读

共计 5419 个字符,预计需要花费 14 分钟才能阅读完成。

pojo 层 层 层 层 层 层 层 层 层 层 层 层 层 层 层
//@AllArgsConstructor // 增加基于全副属性构建的构造函数
//@NoArgsConstructor // 增加无参构造函数
@ToString
@Getter
@Setter
//@Data // 增加此注解会在 Goods 类中主动增加 set,get,toString 等办法
public class Goods {//Goods.class
    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
}//lombok 底层基于字节码技术为类增加相干办法或属性 
dao 层 层 层 层 层 层 层 层 层 层 层 层 层 层
@Mapper
public interface GoodsDao {// 这个接口在运行时零碎底层会产生其实现类, 但没有源码 ( 只有字节码).
     @Update("update tb_goods set name=#{name},remark=#{remark} where id=#{id}")
     int updateGoods(Goods goods);
     @Insert("insert into tb_goods(name,remark,createdTime) values (#{name},#{remark},#{createdTime})")
     int insertGoods(Goods goods);
     @Select("select id,name,remark,createdTime from tb_goods where id=#{id}")
     Goods findById(Integer id);
     @Select("select id,name,remark,createdTime from tb_goods")
     List<Goods> findGoods();
     @Delete("delete from tb_goods where id=#{id}")
     int deleteById(Integer id);
}
service 层 层 层 层 层 层 层 层 层 层 层 层
/**
 *  商品模块业务接口:
 *  负责:
 * 1) 外围业务
 * 2) 拓展业务 (记录日志, 缓存, 事务管制, 权限管制,...)
 */
public interface GoodsService {Goods findById(Integer id);
      int updateGoods(Goods goods);
      int saveGoods(Goods goods);
      /**
 *     查问所有商品信息
 * @return
 */
      List<Goods> findGoods();
      int deleteById(Integer id);
}
service imp 实现层 实现层 实现层 实现层 实现层
@Slf4j
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;
    //private static final Logger log=LoggerFactory.getLogger(GoodsServiceImpl.class);
    @Override
    public List<Goods> findGoods() {long t1=System.currentTimeMillis();
        List<Goods> list=goodsDao.findGoods();
        long t2=System.currentTimeMillis();
        log.info("findGoods().time:{}",(t2-t1));
        return list;
    }
    @Override
    public Goods findById(Integer id) {return goodsDao.findById(id);
    }
    @Override
    public int updateGoods(Goods goods) {
        // TODO Auto-generated method stub
        return goodsDao.updateGoods(goods);
    }
    @Override
    public int saveGoods(Goods goods) {
        // TODO Auto-generated method stub
        goods.setCreatedTime(new java.util.Date());
        return goodsDao.insertGoods(goods);
    }
    @Override
    public int deleteById(Integer id) {try{Thread.sleep(5000);}catch(Exception e) {}
        return goodsDao.deleteById(id);
    }
}
Controller 层 层 层 层 层 层 层 层 层
@Controller
@RequestMapping("/goods/")
public class GoodsController {
     @Autowired
     private GoodsService goodsService;
     @RequestMapping("doGoodsAddUI")
     public String doGoodsAddUI() {return "goods-add";}
     @RequestMapping("doFindById/{id}")
     public String doFindById(@PathVariable Integer id,Model model) {Goods goods=goodsService.findById(id);
         model.addAttribute("goods",goods);
         return "goods-update";// 此 view 由谁解析?ViewResolver(ThymeleafViewResolver)
         // 这里的 ThymeleafViewResolver 做了什么?
         //1) 在 viewname 的根底上增加前缀和后缀 (/templates/pages/goods-update.html), 并找到对应的 view(真正的页面对象)
         //2) 将 model 中的数据取出, 而后填充到 view 上 (/templates/pages/goods-update.html)
         //3) 将 view 交给 DispatcherServlet
     }
     @RequestMapping("doUpdateGoods")
     public String doUpdateGoods(Goods goods) {goodsService.updateGoods(goods);
         return "redirect:/goods/doGoodsUI";
     }
     @RequestMapping("doSaveGoods")
     public String doSaveGoods(Goods goods) {goodsService.saveGoods(goods);
         return "redirect:/goods/doGoodsUI";
     }
     @RequestMapping("doDeleteById/{id}")
     public String doDeleteById(@PathVariable Integer id) {goodsService.deleteById(id);
         return "redirect:/goods/doGoodsUI";
     }
     @RequestMapping("doGoodsUI")
     public String doFindGoods(Model model)throws Exception {//Thread.sleep(7000);
         List<Goods> list=goodsService.findGoods();
         model.addAttribute("list", list);
         return "goods";
     }
}
goods 层 层 层 层 层 层
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<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:${list}">
            <td th:text="${g.id}">1</td>
            <td th:text="${g.name}">AAA</td>
            <td th:text="${g.remark}">AAAAA</td>
            <td th:text="${#dates.format(g.createdTime,'yyyy/MM/dd HH:mm')}">2020/09/01</td>
            <td><a th:href="@{/goods/doDeleteById/{id}(id=${g.id})}">delete</a></td>
            <td><a th:href="@{/goods/doFindById/{id}(id=${g.id})}">update</a></td>
        </tr>
    </tbody>
 </table>
</body>
</html>
goods-add 增加代码
<!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 cols="30" rows="5" name="remark"></textarea>
        <li><input type="submit" value="Save Goods">
     </ul>
  </form>
</body>
</html>
goods-update 批改代码
<!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 cols="30" rows="5" 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=/
spring.main.banner-mode=console
# 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 thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/pages/
spring.thymeleaf.suffix=.html
#spring actuator
management.endpoints.web.exposure.include=*
#Spring log
logging.level.com.cy=debug

正文完
 0