首页 > 代码库 > 九.Spring Boot JPAHibernateSpring Data
九.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=truespring.datasource.username = rootspring.datasource.password = AppleforWindowsspring.datasource.driverClassName = com.mysql.jdbc.Driverspring.datasource.max-active=20spring.datasource.max-idle=8spring.datasource.min-idle=8spring.datasource.initial-size=10自动在数据库创建table表spring.jpa.database = MYSQL# Show or not log for each sql queryspring.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指定主键.*/@Entitypublic 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。