目前分類:java_spring (10)
- Jun 17 Sat 2023 00:00
[java spring boot] 來安裝監控功能SpringBoot Admin
- Jun 18 Thu 2020 11:19
[java spring] error case: Named parameter not bound : __$synthetic$__25
spring data jpa error
org.hibernate.QueryException: Named parameter not bound : __$synthetic$__25
處理方式:
spring-data-commons升版
官方ISSUE:
Avoid potentially expensive constructor parameter creation where possible
- Jan 01 Wed 2020 14:55
[Java Spring boot ] spring-boot 使用swagger變更param名稱
使用JsonProperty 定名
@JsonProperty("uid") @ApiModelProperty(required=true, example="13690000") private String memberId; /* swagger-ui { "uid": 13690000, } */
refer: https://github.com/springfox/springfox/issues/1289
- Jan 01 Wed 2020 14:17
[Java Spring] BeanPropertyRowMapper 對應DB column
spring使用JdbcTemplate進行OR mapping的時候,對應column名稱支援底線_
主要是下面這段程式
/** * Initialize the mapping meta-data for the given class. * @param mappedClass the mapped class */ protected void initialize(Class<T> mappedClass) { this.mappedClass = mappedClass; this.mappedFields = new HashMap<>(); this.mappedProperties = new HashSet<>(); PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass); for (PropertyDescriptor pd : pds) { if (pd.getWriteMethod() != null) { this.mappedFields.put(lowerCaseName(pd.getName()), pd); String underscoredName = underscoreName(pd.getName()); if (!lowerCaseName(pd.getName()).equals(underscoredName)) { this.mappedFields.put(underscoredName, pd); } this.mappedProperties.add(pd.getName()); } } }
例如:
public class CardInfo { private String name; private String expirationYear; private String expirationMonth; }
經過處理mapping對象為以下
mappedFields={ expiration_year=org.springframework.beans.GenericTypeAwarePropertyDescriptor[name=expirationYear], expirationyear=org.springframework.beans.GenericTypeAwarePropertyDescriptor[name=expirationYear], expiration_month=org.springframework.beans.GenericTypeAwarePropertyDescriptor[name=expirationMonth], expirationmonth=org.springframework.beans.GenericTypeAwarePropertyDescriptor[name=expirationMonth], name=org.springframework.beans.GenericTypeAwarePropertyDescriptor[name=name] } mappedProperties=[expirationYear, name, expirationMonth]
- Jan 01 Wed 2020 10:45
[Spring data Jpa] @Query string 處理null empty list or object
主要透過SQL COALESCE做資料處理判斷,COALESCE ( expression [ ,...n ] )傳回第一個非null expression
JPA運作:
----------
以
List<Long> testList= new ArrayList();
testList.add(null);
testList.add(Long.valueOf(13699885));
- Jan 30 Wed 2019 20:22
[Java Spring boot ] spring-boot cache 結合 redis 快取機制設定
spring boot cache 結合 redis 快取機制設定
使用spring boot版本:2.1.6
- pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
- application.yml
增加快取管理設定
reference:
Redis Sentinel Support https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#redis:sentinel
spring: cache: type: REDIS redis: jedis: pool: max-idle: 8 min-idle: 0 max-active: 8 max-wait: -1 sentinel: master: mymaster nodes: 127.0.0.1:26379
- Jan 21 Mon 2019 17:45
[Spring data Jpa] 撈取DB資料,需比對多Column資料
比對相同資料寫法
有一筆pojo資料,要查出與他相同的其他資料
POJO
@Entity @Table(name = "RECEIVER") public class ReceiverDTO implements Serializable { private static final long serialVersionUID = -5368343800218280407L; @Id private String id; @Column private String name; @Column private String email; @Column private String mobile; @Column private String phone; @Temporal(TemporalType.TIMESTAMP) @Column private Date updateTime; public MemReceiverDTO() { super(); } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } }
hibernate
public List<ReceiverDTO> findSameData(ReceiverDTO DTO) { //搜尋資料以Map包裝 Map map = new HashMap(); map.put("name",DTO.getName()); map.put("mobile",DTO.getMobile()); map.put("email",DTO.getEmail()); Criteria criteria = this.getSession().createCriteria(ReceiverDTO.class); criteria.add(Restrictions.allEq(map)); //下條件 criteria.addOrder(Order.desc("updateTime")); return criteria.list(); }
Spring Data JPA
- Jan 17 Thu 2019 11:47
[Spring data Jpa] 暫存參考文件
springdata jpa使用Example快速实现动态查询
https://blog.csdn.net/long476964/article/details/79677526
Spring Data JPA ——默认方法使用
https://segmentfault.com/a/1190000011067941
- Jan 01 Tue 2019 12:30
[Spring Jpa] service同時進行delect、select事務
service需先進行delect 再select新資料
service:
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public List<CreditCardInfoResponse> getCreditCardList(Long fridayUid) { //移除資料 prodDao.deleteByProdId(prodId); prodDao.flush(); //撈取資料 List<prod> prodList = prodDao.findByProdId(ProdId); }
Dao
public interface prodDao{ public List<prod> findByProdId(Long prodId); @Modifying @Transactional @Query("delete from Prod where prodId = :prodId") public int deleteByIdFridayUidAndIsDefault(Long prodId); }
==>
如果表定Query,實際對資料庫操作
1. delete from PROD where PRODID=?
2. select PRODID, PRODNAME from PROD where PRODID=?
- Jan 01 Mon 2018 10:43
[Java Spring] @Validated驗證
要驗證的資料加上@Validated注解,表示spring必須對其進行檢核,BindingResult 則存放檢核後的訊息 (此兩值需相鄰)。
public ResultView quoteValidate(@Validated DataVO dataVO , BindingResult resul) { }
多個資料需要檢核的話,則一對一對設定 ->
(@Validated DataOneVO dataOneVO , BindingResult resul ,@Validated DataTwoVO dataTwoVO , BindingResult resul);