九.Spring Boot JPAHibernateSpring Data
Posted 永恒之心
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了九.Spring Boot JPAHibernateSpring Data相关的知识,希望对你有一定的参考价值。
1、项目结构
2、导入jar包
<!-- 添加Spring-data-jpa依赖. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
3、添加并编辑 文件:application.properties
spring.datasource.url = jdbc:mysql://localhost:3306/AppleforWindows_test1?characterEncoding=utf8&useSSL=true 说明:5.7的mysql必须加入:characterEncoding=utf8&useSSL=true spring.datasource.username = root spring.datasource.password = AppleforWindows spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.max-active=20 spring.datasource.max-idle=8 spring.datasource.min-idle=8 spring.datasource.initial-size=10 自动在数据库创建table表 spring.jpa.database = MYSQL # Show or not log for each sql query spring.jpa.show-sql = true # Hibernate ddl auto (create, create-drop, update) spring.jpa.hibernate.ddl-auto = update # Naming strategy #[org.hibernate.cfg.ImprovedNamingStrategy #org.hibernate.cfg.DefaultNamingStrategy] spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy # stripped before adding them to the entity manager) spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
4、添加实体类set\\get
package com.ld.bean; import java.util.Date; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import com.alibaba.fastjson.annotation.JSONField; /*创建了一个实体类。 * * 如何持久化呢? * * 1、使用@Entity进行实体类的持久化操作,当JPA检测到我们的实体类当中有 * * @Entity 注解的时候,会在数据库中生成对应的表结构信息。 * * * 如何指定主键以及主键的生成策略? * * 2、使用@Id指定主键.*/ @Entity public class SysUser { /*设置为主键*/ @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; private String name; private String phone; private String mail; @JSONField(format="yyyy-MM-dd HH:mm") private Date createTime; private String remark; 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 getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getMail() { return mail; } public void setMail(String mail) { this.mail = mail; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } }
6、创建server
提示:可根据自定义查看(DAORepository 接口并继承 CrudRepository<T, Integer>该类的源码)
package com.ld.service; import javax.annotation.Resource; import javax.transaction.Transactional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import com.ld.bean.SysUser; import com.ld.repository.SysUserRepository; @Service("sysUserService") public class SysUserService { static final Logger logger = LoggerFactory.getLogger(SysUserService.class); @Resource private SysUserRepository sysUserRepository; public Iterable<SysUser> findAll() { logger.info("=>:findAll"); return sysUserRepository.findAll(); } public SysUser findOne(Integer id) { logger.info("=>:findOne"); return sysUserRepository.findOne(id); } @Transactional public SysUser save(SysUser sysUser) { logger.info("=>:save"); return sysUserRepository.save(sysUser); } public long count() { logger.info("=>:count"); return sysUserRepository.count(); } @Transactional public void delete(Integer id) { logger.info("=>:delete"); sysUserRepository.delete(id); } }
7、测试controller
package com.ld.controller; import java.util.Date; import javax.annotation.Resource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import com.ld.bean.SysUser; import com.ld.service.SysUserService; import com.ld.util.CommonTool; @RestController @RequestMapping("sysUserWeb") public class SysUserWeb { @Resource private SysUserService sysUserService; @RequestMapping("/index") public ModelAndView index(){ ModelAndView mv = new ModelAndView("redirect:AmazeUI/error.html"); return mv; } @RequestMapping("/getAll") @ResponseBody public Iterable<SysUser> getAll(){ return sysUserService.findAll(); } @RequestMapping("/save") @ResponseBody public SysUser save(){ SysUser sysUser = new SysUser(); sysUser.setName(CommonTool.getRandomString(6)); sysUser.setPhone(CommonTool.getRandomPhone()); sysUser.setMail(CommonTool.getRandomMail()); sysUser.setCreateTime(new Date()); sysUserService.save(sysUser); return sysUser; } @RequestMapping("/queryOne") @ResponseBody public SysUser queryOne(){ return sysUserService.findOne(1); } }
以上是关于九.Spring Boot JPAHibernateSpring Data的主要内容,如果未能解决你的问题,请参考以下文章