Springboot集成JPA

1)简介:JPA(Java Persistence API),是Java EE 5的标准ORM接口标准。

2)目的:现Springboot通过集成JPA(具体实现,还是Hibernate),以便对数据进行操作

3)实现:

3.1)配置maven


	org.springframework.boot
	spring-boot-starter-data-jpa

3.2)配置yml

spring:
  datasource: 
     driver-class-name: com.mysql.jdbc.Driver
     url: jdbc:mysql://localhost:3306/blade?useUnicode=true&characterEncoding=UTF-8&useSSL=false
     username: root
     password: 123456
  jpa:
     database: mysql
     show-sql: true
     database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
     hibernate:
       naming:
         physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

3.3)实体类配置(相关JPA注解参考:传送门:JPA 批注参考)

import lombok.Data;

import javax.persistence.*;
import java.util.Date;

@Data//这里我用了lombok,非JPA范畴,只是为了简化实体类
@Entity
@Table(name = "UserInfo")
public class UserInfo implements java.io.Serializable{

    @Id
    @GeneratedValue
    @Column(name = "UserId", unique = true, nullable = false)
    private Integer userId;

    @Column(name = "UserCode", nullable = false, length = 20)
    private String userCode;

    @Column(name = "PassWord", nullable = false, length = 100)
    private String passWord;

    @Column(name = "RegEmail", nullable = false, length = 1600)
    private String regEmail;

    @Column(name = "Regmobile", length = 320)
    private String regmobile;

    @Column(name = "UserImg", length = 100)
    private String userImg;

    @Column(name = "LastLoginDate", length = 16)
    private Date lastLoginDate;

    @Column(name = "LastLoginIP", length = 20)
    private String lastLoginIp;

    @Column(name = "NoLogDate", length = 16)
    private Date noLogDate;

    @Column(name = "NoLogNum")
    private Integer noLogNum;

    @Column(name = "UserStatus", nullable = false)
    private Integer userStatus;

    @Column(name = "RegDate", nullable = false, length = 16)
    private Date regDate;

    @Column(name = "RegIp", nullable = false, length = 20)
    private String regIp;

    @Column(name = "UserUuid", nullable = false, length = 50)
    private String userUuid;

}

3.4)配置DAO,定义接口即可

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

/**
 * JPA查询例子
 * @author PI2-0086
 *
 */
//@Repository("userInfoDao")//这里有其他冲突名字时,才自行配置
public interface UserInfoRepository extends JpaRepository {

	/**
	 * 通过SQL查询
	 * @param regEmail
	 * @return
	 * @throws Exception
	 */
    @Query(nativeQuery = true, value = " select * from userInfo where regEmail=:regEmail")
    UserInfo findByRegEmail(@Param("regEmail") String regEmail) throws Exception;
    
    //通过findBy***即可根据对应的属性,查找
    UserInfo findByUserCode(String userCode) throws Exception;
    
    UserInfo findByUserCodeAndRegEmail(String userCode, String regEmail) throws Exception;
    
    /**
     * 	多表查询,但是获取数据时,只有数据,没有字段,每行数据的顺序跟查询SQL的字段顺序一致
     * @param RegEmail
     * @return
     * @throws Exception
     */
    @Query(nativeQuery = true, value = " select u.RegEmail,b.UserName from "
    		+ " UserInfo u left join BasicInfo b on u.UserUuid = b.UserUuid"
    		+ " where u.RegEmail=:regEmail ")
    List findUserAndBasic(@Param("regEmail") String RegEmail) throws Exception;
    
    /**
     * 	多表查询,字段和数据,都可以查出来
     * @param RegEmail
     * @return
     * @throws Exception
     */
    @Query(nativeQuery = true, value = " select u.regEmail,b.userName from "
    		+ " UserInfo u left join BasicInfo b on u.UserUuid = b.UserUuid"
    		+ " where u.RegEmail=:regEmail ")
    List> findUserAndBasicMap(@Param("regEmail") String RegEmail) throws Exception;

}

3.5)调用

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService{
	
	@Autowired
	//@Qualifier("userInfoDao")//名字冲突时,手动指向
	private UserInfoRepository userInfoRepository;

	@Override
	public UserInfo findByRegEmail(String email) throws Exception {
		return userInfoRepository.findByRegEmail(email);
	}

}

 

你可能感兴趣的:(springboot)