终于能够赶上研发的工作了,然后公司用的JPA在整理JPA的东西
首先JPA是一种规范,公司使用的JPA的哪一种部署工具我还不清楚,等到我正式发出来之前会看的吧
总体的目录文件结构
config
首先是JPA配置文件
由于公司已经写好的架构,可能你们使用原生的JPA的实现化工具可能需要配置文件,对于我们的来说就是完成一个config的路径配置文件就可以了
@EnableJpaRepositories({"com.inspur.buliding.imageguoxin.Repository"})
@EntityScan({"com.inspur.buliding.imageguoxin.entity"})
@Configuration(proxyBeanMethods = false)
@ComponentScan({"com.inspur.buliding.imageguoxin.webService"})
public class BeanConfigImageGuoxin
{
@Bean
@ConditionalOnBean(ImageGuoxinService.class)
public RESTEndpoint BeanConfigImageGuoxin(ImageGuoxinService service){
//路径还没有修改
return new RESTEndpoint("/bp/pf/v1.1",service);
}
@Bean
public ImageGuoxinService imageGuoxinService(){return new ImageGuoxinServiceImpl();}
}
EnableJpaRepositories
JPA中的主要的完成sql的包名
EntityScan
扫描entity
Configuration
应该是springboot的配置注解
ComponentScan
扫描的是你具体的接口的注解
这里的RESTEndpoint
是一个路径的entity
在下面的entity有详细的介绍
entity
public class ResultFrontModel {
@JsonProperty("code")
public String code = "ok";
@JsonProperty("data")
public Object data;
@JsonProperty("mes")
public String mes;
@JsonProperty("stack")
public String stack;
public ResultFrontModel(Object data) {
this.data = data;
}
public ResultFrontModel(String code, String mes) {
this.code = code;
this.mes = mes;
}
public ResultFrontModel(String code, String mes,Object data) {
this.code = code;
this.mes = mes;
this.data=data;
}
public ResultFrontModel(){}
public static ResultFrontModel getResult(Object data){
return new ResultFrontModel(data);
}
public static ResultFrontModel getResult(String code, String mes){
return new ResultFrontModel(code, mes);
}
public static ResultFrontModel getResult(String code, String mes,Object data){
return new ResultFrontModel(code, mes,data);
}
public ResultFrontModel setCode(String code){
this.code = code;
return this;
}
public ResultFrontModel setMessage(String mes){
this.mes = mes;
return this;
}
public ResultFrontModel setStackTrace(String stack){
this.stack = stack;
return this;
}
public ResultFrontModel setMes(String mes){
this.mes = mes;
return this;
}
public ResultFrontModel setStack(String stack){
this.stack = stack;
return this;
}
}
这个类的作用大体上就是用来为了配置访问保证路径的正常使用。
package com.inspur.buliding.imageguoxin.entity;
import lombok.Data;
import javax.persistence.*;
import java.util.Arrays;
@Data
@Entity
@Table(name = "`GSGUOXIN_PUSHED`")
public class GuoXinPush {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
// @SequenceGenerator(name = "SEQ_USER_ID", sequenceName = "SEQ_USER_ID")
private String id ;
private String DJBH ;
private String DJWCSj ;
private String filename ;
private String fileid ;
private byte[] bytes;
public GuoXinPush(String id, String DJBH, String DJWCSj, String filename, String fileid, byte[] bytes) {
this.id = id;
this.DJBH = DJBH;
this.DJWCSj = DJWCSj;
this.filename = filename;
this.fileid = fileid;
this.bytes = bytes;
}
@Override
public String toString() {
return "GuoXinPush{" +
"id='" + id + '\'' +
", DJBH='" + DJBH + '\'' +
", DJWCSj='" + DJWCSj + '\'' +
", filename='" + filename + '\'' +
", fileid='" + fileid + '\'' +
", bytes=" + Arrays.toString(bytes) +
'}';
}
public GuoXinPush() {
}
}
上面的实体类就是一个普通的我们需要使用的实体类
然后我们挨个解释注解
Data
这个是lombok的注解,主要是为了方便使用lombok的注解就可以省去写getset方法,是整个实体类看起来更加的见解。
Entity
springboot的注解为了向启动的时候表名这个是个entity
Table
这个注解主要是为了关联上数据库的表结构
我们如果要使用Table
这个注解的话我们需要注意两点
第一是需要根据idea的功能连接上数据库
如图所示将idea连同自己的数据库,我这里使用的是oracle
第二点则是需要注意绑定表
看到这个Assign…就选这个,连接上固定的表
当然还有一点需要注意的
这里需要保证的entity和数据库里面的要求完全一致,完全一致,否则后面的Repository会出现问题
Repository
package com.inspur.buliding.imageguoxin.Repository;
import com.inspur.buliding.imageguoxin.entity.FSBZDJ;
import com.inspur.buliding.imageguoxin.entity.GuoXinPush;
import io.iec.edp.caf.data.orm.DataRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
@Repository
public interface GuoXInpushRepository extends DataRepository<GuoXinPush,String> {
@Query(value = "select * from GSGUOXIN_PUSHED o where o.DJWCSj >= to_timestamp(:DJWCSj, 'YYYY-MM-DD')", nativeQuery = true)
List<GuoXinPush> findYearOfAllGuoxin( @Param("DJWCSj") String DJWCSj);
@Query(value = "SELECT o FROM GuoXinPush o WHERE o.id = :id")
GuoXinPush findByGuoXinPushId(@Param("id") String userId );
@Transactional
@Modifying
@Query(value = "insert into GSGUOXIN_PUSHED values(:id,:DJBH,:DJWCSj,:filename,:fileid)", nativeQuery = true)
void addGuoXinPush(@Param("id") String id, @Param("DJBH") String DJBH, @Param("DJWCSj") String DJWCSj, @Param("filename") String filename, @Param("fileid") String fileid );
@Transactional
@Modifying
@Query(value = "update GSGUOXIN_PUSHED o set o.DJBH = :DJBH where o.id = :id", nativeQuery = true)
void updateGuoXinPush(@Param("id") String id,@Param("DJBH") String DJBH);
@Transactional
@Modifying
@Query(value = "delete GSGUOXIN_PUSHED where id = :id", nativeQuery = true)
void deleteGuoXinPush(@Param("id") String id);
}
这其实就是关键,我认为JPA 的关键的关键就是在这里,其实我也是第一次接触,交给我同事,给我写了这个@Query(value = "SELECT o FROM GuoXinPush o WHERE o.id = :id") GuoXinPush findByGuoXinPushId(@Param("id") String userId );
当我第一次接触的是偶感觉没啥问题,但是当我后面接触到增删改的时候发现需要加nativeQuery = true
注解,这个注解的作用主要是为了证明这个注解里面的sql是否是原生sql,我逐渐的发现这个添加了比较好用,我也不知道为啥。
然后就是jpa的增删改查,差的话有两种。
- 第一种就是不加
nativeQuery = true
,也就是不是原生sql,感觉没有带注解的好用,SELECT o FROM GuoXinPush o WHERE o.id = :id
这个需要有一个别名,然后如果select所有的列名则需要把别名拿过来,就是里面的o, - 第二种就是添加了
nativeQuery = true
注解的,增删改查都可以使用,我觉得要更加的方便,因为是在注解放原生的sql,所以更加的方便和便利,如果真的发现代码出现问题,可以及时的将sql黏贴到数据库直接进行查询,可以有效的看到问题 - 我们在这里主要的说一下第二种的增删改差可能出现的问题。
- 首先是查询,可以传入参数,在sql中使用英文:对象名的方式。
- 然后是增删改,需要添加注解
Modifying
俺也不知道为啥需要加,也没有测试过不加会怎么样,但是我知道使用了这个注解的话这个方法的返回值只能够是void或者int,当然int我没有试,因为真的没有时间,真的感觉自己面试的时候掉坑里了,哎太可惜了!!!!留给研发的时间太少了!
大体上JPA对我来说就是这些了。
还是呼吁大家在入职前一定要问好来了干啥,日常想离职。