1,POM
xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <groupId>com.hipadgroupId> <artifactId>data-jpaartifactId> <version>0.0.1-SNAPSHOTversion> <packaging>jarpackaging> <name>data-jpaname> <description>spring-data-jpadescription> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.0.2.RELEASEversion> <relativePath/> parent> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding> <java.version>1.8java.version> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-data-jpaartifactId> <version>RELEASEversion> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-thymeleafartifactId> dependency> <dependency> <groupId>com.h2databasegroupId> <artifactId>h2artifactId> <scope>runtimescope> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> project>
spring.jpa.show-sql=true
3,springboot入口
package com.hipad; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DataJpaApplication { public static void main(String[] args) { SpringApplication.run(DataJpaApplication.class, args); } }
4,Contorller
package com.hipad.controller; import com.hipad.domain.Book; import com.hipad.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * @author: create by libin * @version: v1.0 * @description: com.hipad.controller * @date:2018/5/12 */ @Controller @RequestMapping(value = "book") public class UserController { @Autowired UserService userService; @RequestMapping(method = RequestMethod.GET) public String findPage(ModelMap map){ map.addAttribute("bookList",userService.findAll()); return "bookList"; } @RequestMapping(value = "create",method = RequestMethod.GET) public String createPage(ModelMap map){ Book book = new Book(); book.setName("java从入门到放弃"); book.setWriter("名字"); book.setIntroduction("这是讲述学习java的心酸历程"); map.addAttribute("book",book); map.addAttribute("action","create"); return "bookForm"; } @RequestMapping(value = "create",method = RequestMethod.POST) public String addPage(@ModelAttribute Book book){ Book save= userService.addAll(book); if(save!=null){ return "redirect:/book";// 这里注意写redirect:/book,不然拿不到值 } return "bookForm"; } @RequestMapping(value = "delete/{id}",method = RequestMethod.GET) public String delete(@PathVariable Long id){ userService.delete(id); return "redirect:/book"; } @RequestMapping(value = "update/{id}",method = RequestMethod.GET) public String update(ModelMap map,@PathVariable Long id){ Book book = userService.findById(id); map.addAttribute("book",book); map.addAttribute("action","update"); return "bookForm"; } @RequestMapping(value = "update",method = RequestMethod.POST) public String updateById(@ModelAttribute Book book){ userService.updateById(book); return "redirect:/book"; } }
5,service
package com.hipad.service; import com.hipad.domain.Book; import java.util.List; /** * @author: create by libin * @version: v1.0 * @description: com.hipad.service * @date:2018/5/12 */ public interface UserService { ListfindAll(); Book addAll(Book book); void delete(Long id); Book findById(Long id); void updateById(Book book); }
package com.hipad.service.impl; import com.hipad.domain.Book; import com.hipad.domain.UserRepository; import com.hipad.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @author: create by libin * @version: v1.0 * @description: com.hipad.service.impl * @date:2018/5/12 */ @Service public class UserServiceImpl implements UserService { @Autowired UserRepository userRepository; @Override public ListfindAll() { return userRepository.findAll(); } @Override public Book addAll(Book book) { Book save = userRepository.save(book); return save; } @Override public void delete(Long id) { userRepository.deleteById(id); } @Override public Book findById(Long id) { return userRepository.findById(id).get(); } @Override public void updateById(Book book) { userRepository.save(book); } }
6,domain
package com.hipad.domain; import org.springframework.data.jpa.repository.JpaRepository; /** * @author: create by libin * @version: v1.0 * @description: com.hipad.domain * @date:2018/5/12 */ public interface UserRepository extends JpaRepository{ }
package com.hipad.domain; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import java.io.Serializable; /** * @author: create by libin * @version: v1.0 * @description: com.hipad.domain * @date:2018/5/12 */ @Entity public class Book implements Serializable { @Id @GeneratedValue private Long id; private String name; private String writer; private String introduction; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getWriter() { return writer; } public void setWriter(String writer) { this.writer = writer; } public String getIntroduction() { return introduction; } public void setIntroduction(String introduction) { this.introduction = introduction; } @Override public String toString() { return "Book{" + "id=" + id + ", name='" + name + '\'' + ", writer='" + writer + '\'' + ", introduction='" + introduction + '\'' + '}'; } }
th:action="@{/book/{action}(action=${action})}"