Spring Data JPA练习

1、Entity

@Entity

@Data

public class Bookimplements Serializable{

@Id

@GeneratedValue

    private long id;

    private StringbookName;

    private StringbookAuther;

    private StringbookIntroduce;

    private StringbookCover;

}

2、DAO

public interface BookRepositoryextends JpaRepository {

}

3、service

public interface BookService {

/**

* 获取所有 Book

*/

    ListfindAll();

    /**

* 获取 Book

*

    * @param id 编号

*/

    BookfindById(Long id);

}


@Service

public class BookServiceImplimplements BookService {

@Autowired

    BookRepositorybookRepository;

    @Override

    public ListfindAll() {

return bookRepository.findAll();

    }

@Override

    public BookfindById(Long id) {

return bookRepository.findById(id).get();

    }

}

4、Controller

@Service

public class BookServiceImplimplements BookService {

@Autowired

    BookRepositorybookRepository;

    @Override

    public ListfindAll() {

return bookRepository.findAll();

    }

@Override

    public BookfindById(Long id) {

return bookRepository.findById(id).get();

    }

}

5、index

<html xmlns:th="http://www.thymeleaf.org">

<html lang="zh-CN">

    <script type="text/javascript" th:src="@{https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js}">

    <link th:href="@{https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css}" rel="stylesheet"/>

    <meta charset="UTF-8"/>

    <title>书籍列表

        p{

display:inline;

        }

down{

margin-top:20px;

        }

title{

font-size:25px;;

        }

content{

padding-top:17px;

        }

container{

width:800px;

        }

bookCover{

height:150px;;

            width:270px;;

        }

<div class="media">

    <div class="media-body">

        <div class="container">

            <div style="margin-left: -70px">

                <img src="/img/000.png">

            <div class="alert alert-success" th:each="Book:${bookList}">

                <div class="top">

                    <div class="media">

                <div class="media">

                    <div class="media-left">

                            <div class="pic">

                                <img th:src="@{${Book.bookCover}}">

                    <div class="media-right">

                        <div class="title">

                            <a th:href="@{/book/{bookId}(bookId=${Book.id})}" th:text="${Book.bookName}">

                        <div class="content">

                            <p th:text="${Book.bookIntroduce}">

</html>


<html xmlns:th="http://www.thymeleaf.org">

<html lang="zh-CN">

    <script type="text/javascript" th:src="@{https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js}">

    <link th:href="@{https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css}" rel="stylesheet"/>

    <meta charset="UTF-8"/>

    <title>书籍详情

<div class="media">

    <div class="media-body">

        <div class="container">

            <div style="margin-left: -70px">

                <img src="/img/001.png">

            <img th:src="@{${book.bookCover}}">


            <p th:text="${book.bookName}">

            <p th:text="${book.bookAuther}">

            <p th:text="${book.bookIntroduce}">

</html>


6、运行截图


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容