1.Thymeleaf简介
Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。相较与其他的模板引擎,它有如下三个极吸引人的特点
- Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板 + 数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
- Thymeleaf 开箱即用的特性。它提供标准和 Spring 标准两种方言,可以直接套用模板实现 JSTL、 OGNL 表达式效果,避免每天套模板、改 JSTL、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
- Thymeleaf 提供 Spring 标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。
2、使用Thymeleft
创建pojo
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TbCustomer {
private Integer id;
private String name;
private String remark;
private String telephone;
private String address;
private Integer typeid;
private Users users;
}
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Users {
private Integer id;
private String typename;
}
编写mapper
public interface TbCustomerMapper {
int deleteByPrimaryKey(Integer id);
int insert(TbCustomer record);
int insertSelective(TbCustomer record);
TbCustomer selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(TbCustomer record);
int updateByPrimaryKey(TbCustomer record);
List<TbCustomer> select(@Param("name") String name);
}
编写service业务层
public interface TbCustomerService{
int deleteByPrimaryKey(Integer id);
int insert(TbCustomer record);
int insertSelective(TbCustomer record);
TbCustomer selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(TbCustomer record);
int updateByPrimaryKey(TbCustomer record);
List<TbCustomer> select( String name);
}
实现业务层
@Service
public class TbCustomerServiceImpl implements TbCustomerService{
@Resource
private TbCustomerMapper tbCustomerMapper;
@Override
public int deleteByPrimaryKey(Integer id) {
return tbCustomerMapper.deleteByPrimaryKey(id);
}
@Override
public int insert(TbCustomer record) {
return tbCustomerMapper.insert(record);
}
@Override
public int insertSelective(TbCustomer record) {
return tbCustomerMapper.insertSelective(record);
}
@Override
public TbCustomer selectByPrimaryKey(Integer id) {
return tbCustomerMapper.selectByPrimaryKey(id);
}
@Override
public int updateByPrimaryKeySelective(TbCustomer record) {
return tbCustomerMapper.updateByPrimaryKeySelective(record);
}
@Override
public int updateByPrimaryKey(TbCustomer record) {
return tbCustomerMapper.updateBy