## 项目第三天 ##

Spring Data JPA 查询技巧

Spring Data Jpa的查询


Specification条件查询的使用

1. 具体的代码如下
    @Test
    public void run3(){
        // 创建条件对象
        Specification<Dept> spec = new Specification<Dept>() {
            /**
             * root     可以获取到Dept类中的属性
             * query    可以自己写查询,不怎么使用
             * cb       可以设置条件
             */
            public Predicate toPredicate(Root<Dept> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                // return cb.gt(root.get("state").as(Integer.class), 0);
                return cb.and(cb.gt(root.get("state").as(Integer.class), 0),cb.like(root.get("deptName").as(String.class), "%市场部%"));
            }
        };
        List<Dept> list = deptDao.findAll(spec);
        System.out.println(list.size());
    }

JPQL的查询

1. 演示JPQL的查询,可以直接在DeptDao中添加查询的方法,查询的语法和HQL的类似的
2. 具体的代码如下
    * dao的代码如下
        public interface DeptDao extends JpaRepository<Dept, String>,JpaSpecificationExecutor<Dept>{

            @Query(value="from Dept where id = ?1")
            public Dept findDept(String id);

            @Query(value="from Dept where state = ?2 and deptName like ?1")
            public List<Dept> findAll(String deptName,Integer state);

        }

    * 测试代码如下
        @Test
        public void run3(){
            // Dept d = deptDao.findDept("3d00290a-1af0-4c28-853e-29fbf96a2722");
            // System.out.println(d.getDeptName());
            List<Dept> list = deptDao.findAll("%市场%",1);
            System.out.println(list.size());
        }

使用方法命名规则进行匹配(完全约定)

1. 在Dao中方法的名称以findBy开始,中间拼接属性名称的查询
2. 例如:findByDeptName,就不用添加@Query的注解了
    * DeptDao的代码如下
        public interface DeptDao extends JpaRepository<Dept, String>,JpaSpecificationExecutor<Dept>{

            public Dept findByDeptName(String name);

            public Dept findByDeptNameAndState(String name,Integer state);

        }

    * 测试的代码如下
        @Test
        public void run3(){
            Dept dept = deptDao.findByDeptName("市场部");
            System.out.println(dept.getDeptName());

            Dept d = deptDao.findByDeptNameAndState("市场部", 1);
            System.out.println(d.getDeptName());
        }

SQL语句的查询方式

1. 代码如下
    * DeptDao接口代码如下
        public interface DeptDao extends JpaRepository<Dept, String>,JpaSpecificationExecutor<Dept>{

            @Query(value="select * from DEPT_P",nativeQuery=true)
            public List<Object[]> findSql();

        }

    * 测试代码如下
        @Test
        public void run3(){
            List<Object[]> list = deptDao.findSql();
            for (Object[] objects : list) {
                System.out.println(Arrays.toString(objects));
            }
        }

部门模块


查看部门详细的功能

1. 具体的代码如下
    /**
     * 查看详细的功能
     * @return
     * @throws Exception
     */
    @Action(value = "deptAction_toview", results = {
            @Result(name = "toview", location = "/WEB-INF/pages/sysadmin/dept/jDeptView.jsp") })
    public String toview() throws Exception {
        model = deptService.get(model.getId());
        super.push(model);
        return "toview";
    }

部门的新增功能

1. 具体的代码如下
    /**
     * 跳转到新增页面
     * @return
     * @throws Exception
     */
    @Action(value = "deptAction_tocreate", results = {
            @Result(name = "tocreate", location = "/WEB-INF/pages/sysadmin/dept/jDeptCreate.jsp") })
    public String tocreate() throws Exception {
        List<Dept> deptList = deptService.find(null);
        super.put("deptList", deptList);
        return "tocreate";
    }

    /**
     * 保存
     * @return
     * @throws Exception
     */
    @Action(value = "deptAction_insert", results = {
            @Result(name = "success", type="redirect", location = "deptAction_list") })
    public String insert() throws Exception {
        deptService.saveOrUpdate(model);
        return "success";
    }

修改部门的功能

1. 完成部门的修改功能,步骤如下
    * 查询要修改的部门记录数据,保存到值栈,转发到JSP页面,相关代码如下
        @Action(value = "deptAction_toupdate", results = {
        @Result(name = "toupdate", location = "/WEB-INF/pages/sysadmin/dept/jDeptUpdate.jsp") })
        public String toupdate() throws Exception {
            model = deptService.get(model.getId());
            super.push(model);
            Specification<Dept> spec = new Specification<Dept>() {
                public Predicate toPredicate(Root<Dept> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                    return cb.equal(root.get("state").as(Integer.class), 1);
                }
            };
            List<Dept> deptList = deptService.find(spec);
            deptList.remove(model);
            super.put("deptList", deptList);
            return "toupdate";
        }

    * 用户编辑数据,提交表单,修改部门数据,相关代码如下
        @Action(value = "deptAction_update", results = {
        @Result(name = "success", type="redirect", location = "deptAction_list") })
        public String update() throws Exception {
            // 先查询部门
            Dept dept = deptService.get(model.getId());
            // 设置修改了哪些数据
            dept.setDeptName(model.getDeptName());
            dept.setParent(model.getParent());
            // 修改部门
            deptService.saveOrUpdate(dept);
            return "success";
        }

批量删除部门功能

1. 如果用户选择了多个部门进行删除,先需要把选中的部门ID通过复选框传给Action,Action需要接收请求参数,但是接收的ID可能是多个值。
    * 如果ID是字符串类型,Struts2框架默认把多个ID值封装到Dept对象的ID属性中,默认使用, 隔开(注意是 逗号空格)

2. 如果没有表结构中没有选择了Cascade属性,也可以采用递归的方式删除部门
    * Action的代码
        @Action(value = "deptAction_delete", results = {
        @Result(name = "success", type="redirect", location = "deptAction_list") })
        public String delete() throws Exception {
            String[] ids = model.getId().split(", ");
            deptService.delete(ids);
            return "success";
        }

    * Service的代码
        /**
         * 删除一个
         */
        public void deleteById(final String id) {
            // 删除
            deptDao.delete(id);
        }

        /**
         * 批量删除
         */
        public void delete(String[] ids) {
            for (String id : ids) {
                deleteById(id);
            }
        }

权限管理(非常重要)


BRAC认证方式

1. 需求:某个用户在登录后,可以访问哪些模块?
2. 以前开发的方式:可能直接让用户与模块之间关联
    * 这种方式需要每次都需要维护用户,使用用户关联模块

3. BRAC认证方式:Base Role Access Controller(基于角色的访问控制)
    * 让用户和角色关联,让角色和模块关联
    * 角色和模块的关联关系一般都是有业务决定的,该角色就能操作哪些模块是固定的,一般都是初始化的时候配置好就可以了
    * 如果公司新来一名员工,只要给该员工分配一个角色,该用户根据角色就能访问哪些模块功能了!!  

4. 如果想玩BRAC认证方式,需要有如下的表结构
    * 用户 <--> 角色 多对多
    * 角色 <--> 模块 多对多

用户模块


导入User和UserInfo的JavaBean

1. 导入资料中提供的JavaBean

用户模块的CURD操作

1. 用户的分页查询
2. 用户的详细信息查询
3. 用户的新增(注意)
    * 因为主键的设置是assigned,需要自己使用UUID生成主键,设置User和UserInfo的主键

4. 用户的修改功能
5. 用户的删除功能

角色模块


角色模块的增删改查

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值