填写项目名
选择web,spring web
next,然后finish
springboot项目即搭建完成
创建controller包,HelloController类
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello";
}
}
浏览器输入地址:http://localhost:8080/hello
整合thymeleaf
pom.xml添加如下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
application.properties文件添加如下配置
#是否开启缓存
spring.thymeleaf.cache=true
#检查模板是否存在
spring.thymeleaf.check-template=true
#检查模板位置是否存在
spring.thymeleaf.check-template-location=true
#模板文件编码
spring.thymeleaf.encoding=UTF-8
#模板文件位置
spring.thymeleaf.prefix=classpath:/templates/
#content-type配置
spring.thymeleaf.servlet.content-type=text/html
#模板文件后缀
spring.thymeleaf.suffix=.html
添加实体类和controller
实体类:
public class Book {
private int id;
private String name;
private String author;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
'}';
}
}
controller
@Controller
public class BookController {
@GetMapping("/books")
public ModelAndView books(){
List<Book> list = new ArrayList<>();
Book b1 = new Book();
Book b2 = new Book();
Book b3 = new Book();
b1.setId(1);
b1.setName("三国");
b1.setAuthor("罗贯中");
b2.setId(2);
b2.setName("水浒");
b2.setAuthor("施耐庵");
b3.setId(3);
b3.setName("红楼");
b3.setAuthor("曹雪芹");
list.add(b1);
list.add(b2);
list.add(b3);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("books",list);
modelAndView.setViewName("books");
return modelAndView;
}
}
templates下新增books.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<tr>
<td>编号</td>
<td>名称</td>
<td>作者</td>
</tr>
<tr th:each="book:${books}">
<td th:text="${book.id}"></td>
<td th:text="${book.name}"></td>
<td th:text="${book.author}"></td>
</tr>
</table>
</body>
</html>
访问地址http://localhost:8080/books
整合mybatis
pom.xml文件新增
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
application.properties配置文件中新增
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/bootdo1?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=123456
mybatis.mapper-locations=classpath:mapper/*.xml
dao包下创建BookMapper,service下创建IBookService接口和BookService实现类,resources文件夹下创建mapper文件夹,以及BookMapper.xml文件
BookMapper
@Mapper
public interface BookMapper {
List<Book> getAllBooks();
}
IBookService
public interface IBookService {
List<Book> getList();
}
BookService
@Service
public class BookService implements IBookService {
@Autowired
BookMapper bookDao;
public List<Book> getList(){
return bookDao.getAllBooks();
}
}
BookMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.springboot.sboot.dao.BookMapper">
<select id="getAllBooks" resultType="com.springboot.sboot.entity.Book">
select * from book
</select>
</mapper>
BookMapper.xml中namespace的地址即为dao目录下的BookMapper地址,id与BookMapper.java中的的方法名相同
最后,修改BookController
@Controller
public class BookController {
@Autowired
public IBookService service;
@GetMapping("/books")
public ModelAndView books(){
// List<Book> list = new ArrayList<>();
// Book b1 = new Book();
// Book b2 = new Book();
// Book b3 = new Book();
// b1.setId(1);
// b1.setName("三国");
// b1.setAuthor("罗贯中");
// b2.setId(2);
// b2.setName("水浒");
// b2.setAuthor("施耐庵");
// b3.setId(3);
// b3.setName("红楼");
// b3.setAuthor("曹雪芹");
// list.add(b1);
// list.add(b2);
// list.add(b3);
List<Book> list = service.getList();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("books",list);
modelAndView.setViewName("books");
return modelAndView;
}
}
浏览器输入地址:localhost:8080/books,即可访问到数据库中查询到的数据
注:还有一种方法,application.properties配置文件中,注释掉
#mybatis.mapper-locations=classpath:mapper/*.xml
BookMapper.xml文件中的内容也注释掉,或直接删除BookMapper.xml
BookMapper接口方法中,添加@Select注解,如下所示