1. 程式人生 > >十次方第二天[Java專案]

十次方第二天[Java專案]

 

第二天的內容是隔了幾天後才學習的,所以在開啟之前的docker出現了點問題,就是我在虛擬機器上啟動了docker之後發現連結不上我的mysql,但是我的docker是啟動了的,後來百度了下,找到了以下解決方案

兩種解決辦法

  1. 在 /usr/lib/sysctl.d/00-system.conf配置檔案末尾新增如下指令碼
net.ipv4.ip_forward=1

2.在 /etc/sysctl.conf配置檔案末尾新增如下指令碼

 net.ipv4.ip_forward=1

然後重啟網路

systemctl restart network

經驗證,在centos下第二種方法可行。

練習Spring Data jpa

條件查詢

    public List<Lable> serach(Lable lable) {
        return lableDao.findAll(new Specification<Lable>() {

            /**
             *
             * @param root  根物件,也就是要把條件封裝到那個物件中,where 類名=lable.getid
             * @param query 封裝的都是查詢關鍵字,比如group by,order by
             * @param cb   用來封裝條件物件的,
             * @return 如果返回null的話表示, 表示不需要任何條件
             */
            @Override
            public Predicate toPredicate(Root<Lable> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

                //new一個list用來存放所有的條件
                List<Predicate> prrList = new ArrayList<Predicate>();

                if (lable.getLabelname() != null || !"".equals(lable.getLabelname())) {
                    Predicate labelname = cb.like(root.get("labelname").as(String.class), "%" + lable.getLabelname() + "%");//相當於 where lablename like %php%
                    prrList.add(labelname);
                }

                if (lable.getState() != null || !"".equals(lable.getState())) {
                    Predicate labelname = cb.equal(root.get("state").as(String.class), lable.getState());
                    prrList.add(labelname);
                }

                //new一個數組返回最終的條件
                Predicate[] arrayPrr = new Predicate[prrList.size()];
                arrayPrr = prrList.toArray(arrayPrr);

                return cb.and(arrayPrr);    //相當於 where lablename like %php% and status = 1
            }
        });
    }

分頁條件查詢

    public Page<Lable> pageQuery(Lable lable, int page, int size){
        return lableDao.findAll(new Specification<Lable>() {

            /**
             *
             * @param root  根物件,也就是要把條件封裝到那個物件中,where 類名=lable.getid
             * @param query 封裝的都是查詢關鍵字,比如group by,order by
             * @param cb   用來封裝條件物件的,
             * @return 如果返回null的話表示, 表示不需要任何條件
             */
            @Override
            public Predicate toPredicate(Root<Lable> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

                //new一個list用來存放所有的條件
                List<Predicate> prrList = new ArrayList<Predicate>();

                if (lable.getLabelname() != null || !"".equals(lable.getLabelname())) {
                    Predicate labelname = cb.like(root.get("labelname").as(String.class), "%" + lable.getLabelname() + "%");//相當於 where lablename like %php%
                    prrList.add(labelname);
                }

                if (lable.getState() != null || !"".equals(lable.getState())) {
                    Predicate labelname = cb.equal(root.get("state").as(String.class), lable.getState());
                    prrList.add(labelname);
                }

                //new一個數組返回最終的條件
                Predicate[] arrayPrr = new Predicate[prrList.size()];
                arrayPrr = prrList.toArray(arrayPrr);

                return cb.and(arrayPrr);    //相當於 where lablename like %php% and status = 1
            }
        }, PageRequest.of(page-1,size));

    }

IDEA中匯入多module的Maven專案無法識別module的解決辦法

首先說下正常的專案module上都會有綠色的資料夾的圖示,出現問題的話就是灰色的什麼都沒有

原因:

出現該問題,是由於開啟工程的時候IDEA只編譯了最外層的pom.xml檔案,而內部的各個module未被Maven自動檢索到(各module的pom.xml檔案未被編譯)。

解決方案:

1.點選IDEA最右側邊欄的Maven Project,會出現的Maven專案面板

2.然後點選圖中綠色的加號按鈕

  1. 再分別選擇對應module的pom.xml檔案,點選OK按鈕。

Spring Data Jpa的一些基本操作

根據狀態查詢

    public List<Recruit> findTop4ByStateOrderByCreatetimeDesc(String state);

    public List<Recruit> findTop12ByStateNotOrderByCreatetimeDesc(String state);

查詢操作

    @Query(value = "SELECT * FROM tb_problem , tb_pl WHERE id = problemid AND labelid = ? ORDER BY replytime DESC" , nativeQuery = true)
    public Page<Problem> newList(String lableId, Pageable pageable);

    @Query(value = "SELECT * FROM tb_problem , tb_pl WHERE id = problemid AND labelid = ? ORDER BY reply DESC" , nativeQuery = true)
    public Page<Problem> hotList(String lableId, Pageable pageable);

    @Query(value = "SELECT * FROM tb_problem , tb_pl WHERE id = problemid AND labelid = ? AND reply = 0  ORDER BY createtime DESC" , nativeQuery = true)
    public Page<Problem> waitList(String lableId, Pageable pageable);

增刪改操作

這裡需要注意的是曾啥改操作都需要加上@Modifying這個註解
    @Modifying
    @Query(value = "UPDATE tb_article SET state = 1 WHERE id = ?", nativeQuery = true)
    public void updateState(String id);

    @Modifying
    @Query(value = "UPDATE tb_article SET thumbup = thumbup + 1 WHERE id = ?", nativeQuery = true)
    public void addThumbup(String id);

專案中整合redis

專案中使用到redis的話,必須要有jar包的,首先先匯入依賴,然後配置一些redis的線管配置,埠,ip等等

當然,springboot整合redis也變得非常簡單,配置成功後,直接在專案中引入

@Autowired
private RedisTemplate redisTemplate;

一些常用的redis語法

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集合

最後介紹的就是Spring cache

這裡需要說明的是,Springcache也是快取技術,但是她沒有redis那麼強大,她主要用的地方就是findById,都是永久儲存在記憶體中,沒有時間的限制


	@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);
	}

大家需要十次方專案的視訊可以關注我的微信公眾號,

大家有需要專案視訊的可以加我微信yan1242269186