业务描述
基于Spring,MyBatis,SpringBoot,Thymeleaf技术实现商品模块的增删改查操作。
添加依赖
https://mybatis.org/spring/ (官网 找依赖)
初始化配置
配置 yml
spring:
thymeleaf:
prefix: classpath:/templates/pages/
suffix: .html
cache: false
datasource:
url: jdbc:mysql:///dbgoods?serverTimezone=GMT%2B8
username: root
password: root
mybatis:
mapper-locations: classpath:/mapper/*/*.xml
logging:
level:
com.cy: debug
初始页面 goods.html
server:
port: 80
spring:
datasource:
url: jdbc:mysql:///dbgoods?serverTimezone=GMT%2B8&characterEncoding=utf8
username: root
password: root
thymeleaf:
prefix: classpath:templates/pages/
suffix: .html
cache: false
mybatis:
mapper-locations: classpath:mapper/*.xml
logging:
level:
com.cy: debug
整体项目结构
项目API架构设计
编辑pojo类
可以lambok 或者 手动 设置set get 方法
package com.cy.pj.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 void setId(Long id) {
this.id = id;
}
public void setName(String name)
{
this.name = name;
}
public void setRemark(String remark) {
this.remark = remark;
}
public void setCreatedTime(Date createdTime) {
this.createdTime = createdTime;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getRemark() {
return remark;
}
public Date getCreatedTime() {
return createdTime;
}
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", name='" + name + '\'' +
", remark='" + remark + '\'' +
", createdTime=" + createdTime +
'}';
}
}
页面编写 去thymeleaf官网
Using Thymeleaf
table
Date
Thymeleaf EL表达式
查询 select
controller
public class GoodsController {
@Autowired
private GoodsService goodsService;
//页面初始化
@RequestMapping("/goods")
public String Goods(Model model){
List<Goods> list= goodsService.findGoods();
model.addAttribute("goods",list);
return "goods";
}
Service
@Service
public class GoodsServiceImpl implements GoodsService{
@Autowired
private GoodsDao goodsDao;
@Override
public List<Goods> findGoods() {
List<Goods> list = goodsDao.select();
return list;
}
dao
@Mapper
public interface GoodsDao {
@Select("select * from tb_goods")
List<Goods> select();
}
页面效果
删除 delete
页面链接
th:href=“@{/user/doDeleByid/{id}(id=${g.id})}”
Thymeleaf 官方th:href应用说明,如图所示
删除操作中,客户端与服务端代码关联说明,如图所示:
controller
@pathVariable不要忘
Service
dao
增加 inselet
页面
首先跳转页面 goods-add.html
添加页面中表单数据提交过程分析,如图所示:
页面submit
th:action=“@{/user/save}”
th:cation 记得加 th
controller
service
pojo封装更快捷
dao
更新 update
先跳转goods-update 根据id回显,再跟新
页面分析 href=“#” th:href="@{/user/doFindByid/{id}(id=${g.id}
controller
service
dao
跳转后页面分析,再update
th:action=“@{/user/doUpdateGoods}”
controller
service
dao
怎样获取 @select 注解 上的 参数
底层运用反射
package com.cy.java.api.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
/**自定义注解
* @Retention 用于描述注解何时有效,例如RUNTIME就表示运行时有效
* @Target 用于描述注解可以描述哪些对象?例如Type表示注解可以描述类或接口
* */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Mapper{}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface Select{
String value();
}
@Mapper
interface GoodsDao{
@Select("select * from tb_goods")
Object findGoods();
}
public class TestAnnotation01 {
public static void main(String[] args) throws Exception {
//1.如何判定GoodsDao接口上是否有@Mapper注解
Class<?> cls=GoodsDao.class;//获得接口的字节码对象
boolean flag=cls.isAnnotationPresent(Mapper.class);
System.out.println(flag);
Mapper mapper=cls.getDeclaredAnnotation(Mapper.class);
if(mapper!=null) {
System.out.println("GoodDao接口上有mapper注解");
}
//2.获取GoodsDao中findGoods方法上的@Select注解中的SQL?
Method method=cls.getDeclaredMethod("findGoods");
Select select=method.getDeclaredAnnotation(Select.class);
String sql=select.value();
System.out.println(sql);
}
}