美文网首页
JPA小案例实现笔记

JPA小案例实现笔记

作者: 数字d | 来源:发表于2022-03-24 00:54 被阅读0次

1.所有用到的依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.xellitix.commons</groupId>
            <artifactId>jackson-utils</artifactId>
            <version>0.2.0</version>
        </dependency>
        <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>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

2.application.properties中的简单配置

spring.datasource.url=jdbc:mysql://127.0.0.1/book?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
spring.datasource.username=root
spring.datasource.password=rootroot
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true
spring.thymeleaf.mode=HTML
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
#spring.thymeleaf.check-template=false
#spring.thymeleaf.check-template-location=false
server.port=8080

controller中的代码

@Controller
@RequestMapping("article")
public class ArticleController {
    @Autowired

    private ArticleRepository articleRepository;

    /**
     * Description: 文章列表
     */
    @RequestMapping("")
    public ModelAndView articlelist(@RequestParam(value = "start", defaultValue = "0") Integer start,
                                    @RequestParam(value = "limit", defaultValue = "5") Integer limit) {
        start = start < 0 ? 0 : start;
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(start, limit, sort);
        Page<Article> page = articleRepository.findAll(pageable);
        ModelAndView mav = new ModelAndView("article/list");
        mav.addObject("page", page);
        return mav;
    }

    /**
     * Description: 根据id获取文章对象
     */
    @GetMapping("/{id}")
    public ModelAndView getArticle(@PathVariable("id") Integer id)  throws Exception {
        Article articles = articleRepository.findById(id);
        ModelAndView mav = new ModelAndView("article/show");
        mav.addObject("article", articles);
        return mav;
    }


    /**
     * Description: 新增操作视图
     */
    @GetMapping("/add")
    public String addArticle()   throws Exception{
        return "article/add";
    }

    /**
     * Description: 新增保存方法
     */
    @PostMapping("")
    public String saveArticle(Article model)  throws Exception{
        articleRepository.save(model);
        return "redirect:/article/";
    }

    /**
     * Description: 删除
     */
    @DeleteMapping("/{id}")
    public String del(@PathVariable("id") long id)  throws Exception{
        articleRepository.deleteById(id);
        return "redirect:";
    }


    /**
     * Description: 编辑视图
     */
    @GetMapping("/edit/{id}")
    public ModelAndView editArticle(@PathVariable("id") long id)  throws Exception {
        Article model = articleRepository.findById(id);
        ModelAndView mav = new ModelAndView("article/edit");
        mav.addObject("article", model);
        return mav;
    }

    /**
     * Description: 修改方法
     */
    @PutMapping("/{id}")
    public String editArticleSave(Article model, long id)  throws Exception{
        model.setId(id);
        articleRepository.save(model);
        return "redirect:";
    }

}

入口类的特殊添加

@EnableJpaAuditing
@SpringBootApplication

Article实体类自动生成了对Id的set和get方法,没有@Data的注解,这里需要将对Id的set和get方法注释,然后添加@Data注解

2.png

这里的处理方式是为了解决可能出现的以下问题:

ERROR 1409 --- [nio-8080-exec-2] 
org.thymeleaf.TemplateEngine: 
[THYMELEAF][http-nio-8080-exec-2] 
Exception processing template "article/list":
 An error happened during template parsing
 (template: "class path resource [templates/article/list.html]")

代码拉取地址:

https://gitee.com/xgkp/SpringBoot8_5_1.git

相关文章

网友评论

      本文标题:JPA小案例实现笔记

      本文链接:https://www.haomeiwen.com/subject/dpfkjrtx.html