1 JPA
JPA诞生的缘由是为了整合第三方ORM框架,建立一种标准的方式,百度百科说是JPA为了实现ORM的天下归一,目前也是在按照这个方向发展,但是还没能完全实现。在ORM框架中,Hibernate是一支很大的部队,使用很广泛,也很方便,能力也很强,同时Hibernate也是和JPA整合的比较良好,我们可以认为JPA是标准,事实上也是,JPA几乎都是接口,实现都是Hibernate在做,宏观上面看,在JPA的统一之下Hibernate很良好的运行。
上面阐述了JPA和Hibernate的关系,那么Spring-data-jpa又是个什么东西呢?这地方需要稍微解释一下,我们做Java开发的都知道Spring的强大,到目前为止,企业级应用Spring几乎是无所不能,无所不在,已经是事实上的标准了,企业级应用不使用Spring的几乎没有,这样说没错吧。而Spring整合第三方框架的能力又很强,他要做的不仅仅是个最早的IOC容器这么简单一回事,现在Spring涉及的方面太广,主要是体现在和第三方工具的整合上。而在与第三方整合这方面,Spring做了持久化这一块的工作,我个人的感觉是Spring希望把持久化这块内容也拿下。于是就有了Spring-data-**这一系列包。包括,Spring-data-jpa,Spring-data-template,Spring-data-mongodb,Spring-data-redis,还有个民间产品,mybatis-spring,和前面类似,这是和mybatis整合的第三方包,这些都是干的持久化工具干的事儿
2 Maven依赖
jpa里面包括hibernate框架。
<!-- Hibernate-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- SQL Server-->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Oracle-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.nls</groupId>
<artifactId>orai18n</artifactId>
<version>21.1.0.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
3 Spring Boot依赖
#连接mysql数据库
spring.datasource.url = jdbc:mysql://localhost:3306/xudongbase?useUnicode=true&characterEncoding=utf8&useOldAliasMetadataBehavior=true&useTimezone=true&serverTimezone=GMT%2B8&useLegacyDatetimeCode=false
spring.datasource.username = root
spring.datasource.password = xudong123456
spring.jpa.database = mysql
#连接sqlserver数据库
spring.datasource.url = jdbc:sqlserver://127.0.0.1:1433;DatabaseName=testdb
spring.datasource.username = zl
spring.datasource.password = 123456
spring.datasource.platform = sqlserver
spring.datasource.driverClassName = com.microsoft.sqlserver.jdbc.SQLServerDriver
#连接oracle数据库
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=system
spring.datasource.password=123456
4 UserEntity
用户信息实体类。
package com.entity;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* 用户信息实体类
*/
@Data
@Table(name = "users")
@Entity
public class UserEntity {
/**
* 用户名
*/
@Column(name = "username")
@Id
private String username;
/**
* 昵称
*/
@Column(name = "pickname")
private String pickname;
/**
* 密码
*/
@Column(name = "password")
private String password;
/**
* 性别
*/
@Column(name = "sex")
private String sex;
}
5 UserRepository
用户Dao层。
package com.dao;
import com.entity.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
/**
* 用户Dao层
*/
@Repository
public interface UserRepository extends JpaRepository<UserEntity, String>, JpaSpecificationExecutor<UserEntity> {
}
6 Service(业务逻辑层)
6.1 UserService
package com.service;
import com.entity.UserEntity;
import java.util.List;
/**
* 用户Service层
*/
public interface UserService {
/**
* 获取全部用户
*
* @return
*/
List<UserEntity> getList();
/**
* 添加用户
*
* @param entity 用户信息
*/
UserEntity create(UserEntity entity);
/**
* 更新用户
*
* @param entity 用户信息
*/
UserEntity update(UserEntity entity);
/**
* 删除用户
*
* @param username 用户名
*/
void delete(String username);
}
6.2 UserServiceImpl
package com.service.impl;
import com.dao.UserRepository;
import com.entity.UserEntity;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 用户Service层
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
/**
* 获取全部用户
*
* @return
*/
public List<UserEntity> getList() {
return userRepository.findAll();
}
/**
* 添加用户
*
* @param entity 用户信息
*/
@Override
public UserEntity create(UserEntity entity) {
return userRepository.save(entity);
}
/**
* 更新用户
*
* @param entity 用户信息
*/
@Override
public UserEntity update(UserEntity entity) {
return userRepository.save(entity);
}
/**
* 删除用户
*
* @param username 用户名
*/
@Override
public void delete(String username) {
userRepository.deleteById(username);
}
}
7 UserController
调试代码。
package com;
import com.entity.UserEntity;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 用户controller层
*/
@RestController
public class UserController {
@Autowired
private UserService userService;
/**
* 获取全部用户
*
* @return
*/
@GetMapping("/getList")
public List<UserEntity> getList() {
return userService.getList();
}
/**
* 添加用户
*
* @param entity 用户信息
*/
@PostMapping("/create")
public UserEntity create(@RequestBody UserEntity entity){
return userService.create(entity);
}
/**
* 更新用户
*
* @param entity 用户信息
*/
@PutMapping("/update")
public UserEntity update(@RequestBody UserEntity entity){
return userService.update(entity);
}
/**
* 删除用户
*
* @param username 用户名
*/
@DeleteMapping("/delete/{username}")
public String delete(@PathVariable("username") String username){
userService.delete(username);
return "删除成功";
}
}
8 调试结果
8.1 查询数据
8.2 新增数据
8.3 更新数据
8.4 删除数据