(3)Spring-boot学习 做一个简单的带访问数据库的web项目

来源:互联网 发布:股票交易软件下载排名 编辑:程序博客网 时间:2024/05/22 00:08

在pom.xml里添加以下依赖

<!-- 引入spring data jpa   数据库持久操作 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- Thymeleaf视图 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- h2 数据库 --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency>
创建Book类 并添加如下注解,get set方法自行添加
//jpa的pojo类@Entitypublic class Book { @Id//表示此属性为主键   @GeneratedValue(strategy=GenerationType.AUTO)//主键为自增长方式 private Long id; private String reader; private String isbn; private String title; private String author; private String description;
定义仓库接口

public interface ReadingListRepository extends JpaRepository<Book,Long>{List<Book> findByReader(String reader); }
创建Web界面:

创建Controller

@Controller@RequestMapping("/") public class ReadingListController { private ReadingListRepository readingListRepository; @Autowired public ReadingListController(ReadingListRepository readingListRepository) { this.readingListRepository = readingListRepository; }  @RequestMapping(value="/{reader}", method=RequestMethod.GET) public String readersBooks(@PathVariable("reader") String reader,Model model) { List<Book> readingList =readingListRepository.findByReader(reader); if (readingList != null) { model.addAttribute("books", readingList); } return "readingList"; } @RequestMapping(value="/{reader}", method=RequestMethod.POST) public String addToReadingList(@PathVariable("reader") String reader, Book book) { book.setReader(reader); readingListRepository.save(book); return "redirect:/{reader}"; }}

定义HTML页面命名为readingList.html 放到resources目录下的templates目录下

<html><head><title>Reading List</title><link rel="stylesheet" th:href="@{/style.css}"></link></head><body><h2>Your Reading List</h2><div th:unless="${#lists.isEmpty(books)}"><dl th:each="book : ${books}"><dt class="bookHeadline"><span th:text="${book.title}">Title</span> by <spanth:text="${book.author}">Author</span> (ISBN: <spanth:text="${book.isbn}">ISBN</span>)</dt><dd class="bookDescription"><span th:if="${book.description}" th:text="${book.description}">Description</span><span th:if="${book.description eq null}"> No descriptionavailable</span></dd></dl></div><div th:if="${#lists.isEmpty(books)}"><p>You have no books in your book list</p></div><hr /><h3>Add a book</h3><form method="POST"><label for="title">Title:</label> <input type="text" name="title" size="50"/><br /> <label for="author">Author:</label> <input type="text" name="author" size="50"/><br /> <label for="isbn">ISBN:</label><input type="text" name="isbn" size="15"/><br /> <label for="description">Description:</label><br /><textarea name="description" cols="80" rows="5"></textarea><br /> <input type="submit" value="提交"/></form></body></html>
找到MySpringBootApplication类 运行main方法

访问http://127.0.0.1:8080/readingList  可以测试添加数据 

这里是采用了spring 的自动配置

因 为 Classpath 里 有 H2 ,所以会创建一个嵌入式的 H2 数据库 Bean ,它的类型是javax.sql.DataSource,JPA实现(Hibernate)需要它来访问数据库。

因为Classpath里有Hibernate(Spring Data JPA传递引入的)的实体管理器,所以自动配置会配置与 Hibernate 相关的 Bean ,包括 Spring 的 LocalContainerEntityManagerFactoryBean和JpaVendorAdapter。

因为Classpath里有Spring Data JPA,所以它会自动配置为根据仓库的接口创建仓库实现。

因为Classpath里有Thymeleaf,所以Thymeleaf会配置为Spring MVC的视图,包括一个Thymeleaf的模板解析器、模板引擎及视图解析器。视图解析器会解析相对于Classpath根目录的/templates目录里的模板。

因 为 Classpath 里 有 Spring MVC (归功于 Web 起 步 依 赖 ), 所 以 会 配 置 Spring 的DispatcherServlet并启用Spring MVC。

因为这是一个Spring MVC Web应用程序,所以会注册一个资源处理器,把相对于Classpath根目录的/static目录里的静态内容提供出来。(这个资源处理器还能处理/public、/resources和/META-INF/resources的静态内容。)

因为Classpath里有Tomcat(通过Web起步依赖传递引用),所以会启动一个嵌入式的Tomcat容器,监听8080端口。






0 0
原创粉丝点击