上两期我们讲到了JdbcTemplate和devtools热部署的使用,
小编要讲的内容是spingboot-jpa,首先说一下Jpa的相关概念
如果实现嫌啰嗦可以去看看官网说明:http://spring.io/projects/spring-data-jpa
1、JPA概念
JPA全称Java Persistence API,JPA通过JDK5.0注解或XML描述对象、关系表的映射关系,
并将运行期的实体对象持久化到数据库中。
持久化(Persistence),即把数据保存到可永久保存的存储设备中(如磁盘),
持久化的主要应用是将内存中的对象存储在数据库中,或者存储在磁盘文件中或XML数据文件中。
3、Spring Boot JPA特点
a、简洁,只需要声明接口,接口无需实现
b、简单易用,提供了多种不同功能的接口
c、集成了多种查询策略,支持JPQL
d、简化了排序分页等功能
e、可以和Spring Boot整合,进一步简化
下面进行实战操作:
一、在pom.xml加入以下依赖
<!-- springboot-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
二、修改application.properties文件
#mysql连接池
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc\:mysql\://localhost:3306/hr
spring.datasource.username=root
spring.datasource.password=rootadmin
#spring boot JPA相关配置
spring.jpa.datasource=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 strateg
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
#spring.jpa.database=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
三、创建一下package(包)
com.test.application >>>>>> MyJapApplication.java //启动类
com.test.controller >>>>>>UserJpaController.java
com.test.jpa.dao >>>>>>UsersDao.java //interface接口
com.test.jpa.entity >>>>>>Users.java //实体类
com.test.jpa.service >>>>>>UserJpaService.java //interface接口
com.test.jpa.service.Impl >>>>>>UserJpaServiceImpl.java //实现类
四、com.test.jpa.entity >>>>>>Users.java
package com.test.jpa.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity(name="Users")
public class Users {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String usename;
private Integer age;
private String birthday;
//get、set自己弄
}
五、com.test.jpa.dao>>>>>>UsersDao.java (接口类)
package com.test.jpa.dao;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import com.test.jpa.entity.Users;
public interface UsersDao extends Repository<Users, Integer> {
/**
* 我们这里只需要写接口,不需要写实现,
* spring boot会帮忙自动实现
* @Title: getUser
* @Description: TODO
* @param @param id
* @param @return
* @return Users
* @throws
*/
@Query("from Users where id =:id ")
public Users getUser(@Param("id") Integer id);
}
六、com.test.jpa.service >>>>>>UserJpaService.java //interface接口
package com.test.jpa.service;
import com.test.jpa.entity.Users;
/**
*
* @ClassName: UserJpaService
* @Description: TODO
* @author chenshangbing
* @date 2018年10月22日 下午1:41:46
*
*/
public interface UserJpaService {
public Users getUser(Integer id);
}
七、com.test.jpa.service.Impl >>>>>>UserJpaServiceImpl.java //实现类
package com.test.jpa.service.Impl;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.test.jpa.dao.UsersDao;
import com.test.jpa.entity.Users;
import com.test.jpa.service.UserJpaService;
@Service("UserJpaService")
public class UserJpaServiceImpl implements UserJpaService {
@Autowired
UsersDao usesdao;
public Users getUser(Integer id) {
// TODO Auto-generated method stub
Users u=usesdao.getUser(id);
return u;
}
}
八、com.test.controller >>>>>>UserJpaController.java
package com.test.controller;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.test.jpa.entity.Users;
import com.test.jpa.service.UserJpaService;
/**
*
* @ClassName: UserJpaController
* @Description: TODO
* @author chenshangbing
* @date 2018年10月22日 上午11:17:46
*
*/
@RestController
public class UserJpaController {
@Autowired
UserJpaService service;
@RequestMapping("getUser")
public Users getUser(Integer id){
System.out.println(id+"=========");
Users us=service.getUser(id);
System.out.println(us.getBirthday()+"========");;
return us;
}
}
九、com.test.application >>>>>> MyJapApplication.java //启动类
package com.test.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.bind.annotation.RestController;
/**
*
* @ClassName: MyJapApplication
* @Description: TODO
* @author chenshangbing
* @date 2018年10月22日 上午9:41:50
*
*/
@RestController
@ComponentScan(basePackages="com.test.controller,com.test.jpa.service")
@EnableJpaRepositories(basePackages="com.test.jpa.dao")
@EntityScan(basePackages="com.test.jpa.entity")
@EnableAutoConfiguration
public class MyJapApplication {
public static void main(String[] args){
// TODO Auto-generated method stub
SpringApplication.run(MyJapApplication.class, args);
}
}
十、启动访问http://localhost:8090/getUser?id=2 结果如图
好啦,今天的分享就到这里喔!
记得点一个赞哦,你的赞是小编成功的第一步