上兩期我們講到了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 結果如圖
好啦,今天的分享就到這裏喔!
記得點一個贊哦,你的贊是小編成功的第一步