-------------------------------------------------------------------01------------------------------------------------------------------------
springdataJpa:jpa的操作。
標籤查詢。
寫service:
service怎麼寫呢:分頁 條件 分頁和條件。
service的書寫:
public List<Label> findSearch(Label label) {
Specification<Label> specification=new Specification<Label>() {
/**
*
* @param root 根對象,也就是要把條件封裝到哪個對象中,where leiming=label.getId
* @param query 封裝的是查詢關鍵字, 比如group by order by等
* @param cb 用來封裝條件對象
* @return
*/
@Override
public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
//new一個集合存放所有的條件
List<Predicate> list=new ArrayList<Predicate>();
if(label.getLabelname()!=null &&!"".equals(label.getLabelname())){
Expression<String> labelname = root.get("labelname").as(String.class);
Predicate predicate = cb.like(labelname, "%" + label.getLabelname() + "%");//where labelname like "%XX%"
list.add(predicate);
}
if(label.getState()!=null && !"".equals(label.getState())){
Expression<String> state = root.get("state").as(String.class);
Predicate predicate = cb.equal(state,label.getState());//where labeState = "XX"
list.add(predicate);
}
//new一個數組作爲最終返回值的條件
Predicate[] parr = new Predicate[list.size()];
//把list直接轉成數組
parr = list.toArray(parr);
return cb.and(parr);
}
};
List<Label> labels = labelDao.findAll(specification);
return labels;
}
測試:
JSON:
{
"labelname": "string123",
"state": "1",
"count": 0,
"recommend": "1"
}
------------------------------------------------------------------------02----------------------------------------------------------------------------------------
分頁的:
寫controller:
/**
* 標籤條件查詢
* @return
*/
@RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
public Result PageQuery(@PathVariable("page")Integer page,@PathVariable("size")Integer size,@RequestBody Label label){
Page<Label> pageData=labelService.PageQuery(label,page,size);
return new Result(true,StatusCode.OK,"查詢成功",new PageResult<Label>(pageData.getTotalElements(),pageData.getContent()));
}
寫service,這個是固定的寫法。
public Page<Label> PageQuery(Label label, Integer page, Integer size) {
Specification<Label> specification = new Specification<Label>() {
/**
*
* @param root 根對象,也就是要把條件封裝到哪個對象中,where leiming=label.getId
* @param query 封裝的是查詢關鍵字, 比如group by order by等
* @param cb 用來封裝條件對象
* @return
*/
@Override
public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
//new一個集合存放所有的條件
List<Predicate> list = new ArrayList<Predicate>();
if (label.getLabelname() != null && !"".equals(label.getLabelname())) {
Expression<String> labelname = root.get("labelname").as(String.class);
Predicate predicate = cb.like(labelname, "%" + label.getLabelname() + "%");//where labelname like "%XX%"
list.add(predicate);
}
if (label.getState() != null && !"".equals(label.getState())) {
Expression<String> state = root.get("state").as(String.class);
Predicate predicate = cb.equal(state, label.getState());//where labelname like "%XX%"
list.add(predicate);
}
//new一個數組作爲最終返回值的條件
Predicate[] parr = new Predicate[list.size()];
//把list直接轉成數組
parr = list.toArray(parr);
return cb.and(parr);
}
};
Pageable pageable = PageRequest.of(page - 1, size);
return labelDao.findAll(specification, pageable);
}
注意這個page是從1開始的。要減1變爲0。
查詢:
結果:
------------------------------------------------------------------03------------------------------------------------------------------------
招聘微服務:
用代碼生成器生成代碼:
第一步:
第二步:
第三步:
寫pom:
修改父工程的信息。
第四步:
修改啓動類的名稱
修改yml文件。
-------------------------------------------------------------------04------------------------------------------------------------------------
做這個界面。
寫面向對象的sql語句,不能出現表名。不推薦這樣寫的。
-------------------------------------------------------------------05------------------------------------------------------------------------
查詢熱門企業:
寫相關的service和controller
查詢:
http://localhost:9002/enterprise/search/hotlist
-------------------------------------------------------------------06------------------------------------------------------------------------
帶順序的查詢。
第一步:
第二步:
寫service和controller
測試:
http://localhost:9002/recruit/search/newlist
http://localhost:9002/recruit/search/recommend
-------------------------------------------------------------------07------------------------------------------------------------------------
問答的微服務:
問答微服務的業務代碼生成。
我們要寫sql語句了。
第一步:代碼生成器tensquare_qa,代碼生成器framework。
第二步:改代碼
-------------------------------------------------------------------08------------------------------------------------------------------------
php的最新熱門等待
java的最新熱門等待
要關聯表。
聯合主鍵表示聯合是唯一的。問題可能有很多標籤,標籤可能含有很多問題。
聯合查詢必須寫sql語句了。
---------------------------------------------------------------------------09---------------------------------------------------------------------------------
JPA的聯合查詢。最新 熱門 等待
第一步:
寫sql:寫Dao。
@Query(value = "select * from tb_problem,tb_pl where id = problemid and labelid=? order by replytime DESC " ,nativeQuery = true)
Page<Problem> newList(String labelid, Pageable pageable);
@Query(value = "select * from tb_problem,tb_pl where id = problemid and labelid=? order by reply DESC " ,nativeQuery = true)
Page<Problem> hotList(String labelid,Pageable pageable);
@Query(value = "select * from tb_problem,tb_pl where id = problemid and labelid=? and reply=0 order by createtime DESC " ,nativeQuery = true)
Page<Problem> waitList(String labelid, Pageable pageable);
JPA的分頁就這麼簡單。
想分頁傳參數就可以。
寫service和controller。
測試:
http://localhost:9003/problem/newlist/1/1/1
單表查詢很少。
-------------------------------------------------------------------------------10-------------------------------------------------------------------------------------
文章模塊編寫:
表結構:審覈才能顯示出來,沒審覈是不讓顯示的。
第一步:寫dao
/**
* 增刪改需要加@Modifyting
* 審覈文章
*/
@Modifying
@Query(value = "update tb_article set state=1 where id =? ",nativeQuery = true)
void updateState(String id);
/**
* 點贊數增加
* @param id
*/
@Modifying
@Query(value = "update tb_article set thumbup = thumbup + 1 where id =? ",nativeQuery = true)
void addThumbup(String id);
注意一下:注意增刪改都要加一個@Modifying,線程問題都要加。
第二步:寫service和controller
測試:
http://localhost:9004/article/thumbup/1
-------------------------------------------------------------------11----------12--------------------------------------------------------------
SpringDataRedis
製作redis容器:一直查數據庫使數據庫的壓力過大。
啓動redist:
docker run -di --name=tensquare_redis -p 6379:6379 redis
-------------------------------------------------------------------13------------------------------------------------------------------------
使用redis:
第一步:pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
第二步:yml文件
redis:
host: 192.168.244.136
第三步:寫一個查詢下。先在緩存中查詢,要是緩存沒有的話就在數據庫查詢下,更新緩存。
public Article findById(String id)
{
//先在緩存中查詢
Article article = (Article)redisTemplate.opsForValue().get("article_"+id);
if(article==null){
article = articleDao.findById(id).get();
redisTemplate.opsForValue().set("article_"+id,article);
}
return article;
}
擴展:
stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);//向redis裏存入數據和設置緩存過期時間
stringRedisTemplate.opsForValue().get("test")//根據key獲取緩存中的val
stringRedisTemplate.boundValueOps("test").increment(-1);//val做-1操作
stringRedisTemplate.boundValueOps("test").increment(1);//val +1
stringRedisTemplate.getExpire("test")//根據key獲取過期時間
stringRedisTemplate.getExpire("test",TimeUnit.SECONDS)//根據key獲取過期時間並換算成指定單位
stringRedisTemplate.delete("test");//根據key刪除緩存
stringRedisTemplate.hasKey("546545");//檢查key是否存在,返回boolean值
stringRedisTemplate.expire("red_123",1000 , TimeUnit.MILLISECONDS);//設置過期時間
stringRedisTemplate.opsForSet().add("red_123", "1","2","3");//向指定key中存放set集合
stringRedisTemplate.opsForSet().isMember("red_123", "1")//根據key查看集合中是否存在指定數據
stringRedisTemplate.opsForSet().members("red_123");//根據key獲取set集合
-------------------------------------------------------------------14-------------------------------------------------------------------------
修改和刪除要更新或者刪除緩存的。
/**
* 修改
* @param article
*/
public void update(Article article)
{
redisTemplate.opsForValue().set("article_"+article.getId(),article);
articleDao.save(article);
}
/**
* 刪除
* @param id
*/
public void deleteById(String id) {
redisTemplate.delete("article_"+id);
articleDao.deleteById(id);
}
-------------------------------------------------------------------15------------------------------------------------------------------------
redis過期時間:
註冊,驗證碼的過期時間。
public Article findById(String id)
{
//現在緩存中查詢
Article article = (Article)redisTemplate.opsForValue().get("article_"+id);
if(article==null){
article = articleDao.findById(id).get();
redisTemplate.opsForValue().set("article_"+id,article,10,TimeUnit.SECONDS);
}
return article;
}
-------------------------------------------------------------------------------16---------------------------------------------------------------------------------------
springcache:
沒有過期時間使sringcache。
------------------------------------------------------------------------------17------------------------------------------------------------------------------------
在活動的模塊用springcache。
第一步:
第二步:
gathering是CacheManager的名字,id是cache的鍵值對。
@Cacheable(value="gathering",key="#id")
public Gathering findById(String id) {
return gatheringDao.findById(id).get();
}
第三步:
/**
* 修改
* @param gathering
*/
@CacheEvict(value="gathering",key="#gathering.id")
public void update(Gathering gathering) {
gatheringDao.save(gathering);
}
/**
* 刪除
* @param id
*/
@CacheEvict(value="gathering",key="#id")
public void deleteById(String id) {
gatheringDao.deleteById(id);
}
弊端不能設置過期時間。
-------------------------------------------------------------------18------------------------------------------------------------------------