十次方第二天[Java项目]

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yan12422/article/details/84337736

第二天的内容是隔了几天后才学习的,所以在打开之前的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

猜你喜欢

转载自blog.csdn.net/yan12422/article/details/84337736