SpringBoot整合MyBatis-Plus+Thymeleaf+拦截器

mysql

mysql-connector-java

runtime

配置文件

spring:

datasource:

driver-class-name: com.mysql.cj.jdbc.Driver

url: jdbc:mysql://localhost:3306/book_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC

username: root

password: 123456

resources:

static-locations: classpath:static/

mybatis-plus:

mapper-locations: classpath:mappers/*.xml

type-aliases-package: com.blb.day12_spring_boot.entity

用户实体类

@Data

@TableName(“tb_user”)

public class User implements Serializable {

@TableId(type = IdType.AUTO)

private Integer id;

private String username;

private String password;

private String realName;

}

书籍实体类

@Data

@TableName(“tb_book”)

public class Book {

@TableId(type = IdType.AUTO)

private Integer id;

private String bookName;

private float price;

private Integer typeId;

private String author;

private String publishOrg;

private String publishTime;

private Integer state;

private String bookImage;

}

用户Mapper接口

public interface UserMapper extends BaseMapper {

}

书籍Mapper接口

public interface BookMapper extends BaseMapper {

}

用户Service接口

public interface UserService extends IService {

}

书籍Service接口

public interface BookService extends IService{

}

用户Service接口实现类

@Service

public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

}

书籍Service接口实现类

@Service

public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

}

整合SpringMVC

=========================================================================

导入依赖

org.springframework.boot

spring-boot-starter-web

页面跳转的控制器

/**

  • 页面跳转控制器

  • 例: http://localhost:8080/pages/login 跳转 login页面

*/

@Controller

@RequestMapping(“pages”)

public class PageController {

@RequestMapping(“{page}”)

public String toPage(@PathVariable(“page”)String page){

return page;

}

}

用户登录控制器

/**

  • 用户登录控制器

*/

@Controller

@RequestMapping(“user”)

public class UserController {

@Autowired

private UserService userService;

@PostMapping(“login”)

public String login(Model model, HttpSession session, String username, String password){

//使用MyBatis-Plus的通用service查询账号和密码

User user = userService.getOne(new QueryWrapper().eq(“username”, username)

.eq(“password”, password));

//查询失败,提示错误

if(user == null){

model.addAttribute(“msg”,“账号或密码错误”);

return “login”;

}

//查询成功,保存user对象,跳转书籍列表页面

session.setAttribute(“user”,user);

return “redirect:/book/list”;

}

}

书籍控制器

/**

  • 书籍控制器

*/

@Controller

@RequestMapping(“book”)

public class BookController {

@Autowired

private BookService bookService;

@RequestMapping(“list”)

public String list(Model model){

//查询所有书籍,跳转到index.html

List list = bookService.list();

model.addAttribute(“books”,list);

return “index”;

}

}

整合Thymeleaf

=========================================================================

SpringBoot默认不支持JSP,支持的是比JSP更加优秀的模板引擎技术:Thymeleaf

Thymeleaf的优势:

  • 动静结合:Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。

  • 开箱即用:它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、该jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。

  • 多方言支持:Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。

  • 与SpringBoot完美整合,SpringBoot提供了Thymeleaf的默认配置,并且为Thymeleaf设置了视图解析器,我们可以像以前操作jsp一样来操作Thymeleaf。代码几乎没有任何区别,就是在模板语法上有区别。

导入依赖

org.springframework.boot

spring-boot-starter-thymeleaf

在resources/templates目录中新建HTML文件

页面的html标签中要添加命名空间:xmlns:th=“http://www.thymeleaf.org”

login.html

登录页面

Hello 登录页面



  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值