<!-- jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- sql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
application.yml:
ip: 192.168.10.200//本机就用localhost或者本机IP
server:
port: 8090
spring:
jpa:
database-platform: org.hibernate.dialect.MySQL5Dialect
properties:
hibernate:
format_sql: true
use_sql_comments: true
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${ip}:3316/lyl?useSSL=true&verifyServerCertificate=false&useUnicode=true&characterEncoding=UTF8
username: 1111111
password: 1111111
首先,定义我所需要功能的接口
package com.usermanager.demo.service.inter;
import com.usermanager.demo.entity.UserEntity;
import com.usermanager.demo.entity.vo.UserParam;
import com.usermanager.demo.enums.ResponseResult;
import org.springframework.data.domain.Page;
import java.util.List;
public interface IUserService {
void Insert(UserParam add);
void DeleteUserById(Integer id);
void UpdateUsers(UserParam userParam);
List<UserEntity> getUserList();
List<UserEntity> findByUnameLike(String uname);
Page<UserEntity> findByPage(String uname, String mobile);
}
首先,我们要定义实体类,实体类对应一个数据表,其中的属性对应数据表中的字段。
package com.usermanager.demo.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.*;
import javax.persistence.criteria.Fetch;
import java.util.Date;
import java.util.List;
@Setter
@Getter
@Entity
@Table (name = "users")//数据表的名称
public class UserEntity {
@Id
@GenericGenerator(name = "autoId", strategy = "native")//ID自增长
@GeneratedValue(generator = "autoId")
private Integer id;
private String uname;
//设置时间格式为2019-02-19这种
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Temporal(TemporalType.TIMESTAMP)
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
private Date birthday;
private String mobile;
private Integer dept;
private Integer ugroup;
private Boolean isdelete;
}
下面是DAO层,实现用户的增删改查方法
/**
* @Description:实现用户的增删改查方法
*/
@Repository
public interface UserRepository extends JpaRepository<UserEntity,Integer>, JpaSpecificationExecutor<UserEntity> {
}
下面我们进行最简单的查找数据信息,在这里我们就能简单体会到Jpa的强大之处,只需要写一句userRepository.findAll();就可以查到全部数据并放到user列表里
@Service
public class UserService implements IUserService {
@Autowired
private UserRepository userRepository;
@Override
public List<UserEntity> getUserList() {
return userRepository.findAll();
}
下面是简单的新增数据,save方法同时也可以用于修改,只要id在数据库中存在,通过save方法就可以把这个id的信息全部修改为输入的信息。
@Override
public void Insert(UserParam param) {
Date date = new Date();
UserEntity entity=new UserEntity();
entity.setId(1);
entity.setDept(1);
entity.setUgroup(2);
entity.setMobile("18300000000");
entity.setUname("测试");
entity.setBirthday(date);
entity.setIsdelete(false);
userRepository.save(entity);
}
下面我们进行简单的带条件查询
@Repository
public interface UserRepository extends JpaRepository<UserEntity,Integer>, JpaSpecificationExecutor<UserEntity> {
List<UserEntity> findByUnameLike(@Param("uname") String uname);
}
只要我门在持久层(DAO)层定义一个列表,起名字时就会选择查询条件,通过起的名字JPA就可以自动生成查询语句,也就免去了我们手动写sql的时间。
当然,我们手写sql语句也是可以的,下面简单的介绍一下。
service层:
@Override
public void DeleteUserById(Integer id) {
userRepository.DeleteUserById(id);
}
这是一个逻辑删除,把user的isdelete属性改为true
DAO层:
@Repository
public interface UserRepository extends JpaRepository<UserEntity,Integer>, JpaSpecificationExecutor<UserEntity> {
// List findByUnameLike(@Param("uname") String uname);
@Modifying
@Transactional
@Query(value = "update users set users.isdelete=true where users.id=?1",nativeQuery = true)
void DeleteUserById(@Param("id")Integer id);
}
通过Jpa直接删除也很简单,通过jpa所给的Api就可直接操作。
Controller层:
@RequestMapping("/users")
@RestController
public class UserController {
@Autowired
private IUserService userService;
@ApiOperation(value = "查询所有信息")
@GetMapping("/findAll")
public List<UserEntity>getUserInfo()
{
return userService.getUserList();
}
}