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 {
}
=========================================================================
导入依赖
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”;
}
}
=========================================================================
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