ManagementDay03(一对一 多对一 多对多 一对多在hbm配置)

在hibernate.cfg.xml<!--  加载映射文件-->
        <mapping resource="classpath:my/domain/Dept.hbm.xml"></mapping>
        如果要使用   //注入DeptService
        还需要在
        <!-- 用户 -->
    <bean id="userAction" class="my.action.sysadmin.UserAction" scope="prototype">
        <property name="userService" ref="userService"></property>
        <property name="deptService" ref="deptService"></property>
    </bean> 
    数据对不齐可以在forEach下的tr中加align="left" 主要看往哪边靠
具有相同名称的一组值,struts2实现封装
model对象:Dept类型
    id:String
1、对于String类型,struts2会采用","方式进行字符串连接abvcasda,dsaccd
2、对于Integer,Float,Date类型struts2默认接受的是最后一个值
    如:客户端给id传了3个值 id=3 id=4 id=5 服务器只能接受最后一个值就是id=5
    方案
        private List<Integer> ids; 生成它的getter和setter

解决自己是自己的父亲
1public void saveOrUpdate(Dept model){
    if(model.getParent()!=null && "".equals(model.getParent().getId())){
        model.setParent(null);
    }

    if(StringUtils.isEmpty(model.getId())){
        //是新增的
        model.setState(1);
    }else{
        //更新
        //父部门就是自己
        if(model.getId().equals(model.getParent().getId())){
            model.setParetn(null);//不允许自己为自己的父亲
        }
    }
    baseDao.saveOrUpdate(model);
}

2、改造JdeptUpdate.jsp页面
    <s:select name="PARENT_ID.DEPT_ID" list="#deptList" 
        headerKey="" headerValue="--请选择--" 
        listKey="DEPT_ID" listValue="DEPT_NAME">
    </s:select>
    更改为<select>标签
        <c:forEach items="" var="dept">
            <c:if test="${dept.id}!=id">
                <option value="${dept.id}">${dept.deptName}</option>
            </c:if>
        </c:forEach>
3public String toupdate() throws Exception{
    //1、加载当前要更新的对象
    Dept dept = deptService.findDeptById(model.getId());
    super.push(dept);
    //2、下拉列表
    List<Dept> list = deptService.findAll();//查询所有的对象
    //将当前放在值栈中要修改的对象,从部门列表中清除,目的是为了下拉列表中没有当前放在值栈中的这个对象
    list.remove(dept);
    //将这个集合放入值栈的context中
    ActionContext.getContext.put("list",list);
    return "toupdate";
}
传统方式认证的过程就是查看状态字段,复合要求就可以访问功能模块
BRAC认证方式
Base Role Access Controller:基于角色的访问控制

这里写图片描述
这里写图片描述
这里写图片描述
BaseEntity

public class BaseEntity implements Serializable {
    //protected 子也可以用
    protected String createBy;//创建者的id
    protected String createDept;//创建者所在部门的id
    protected Date createTime;//创建时间
    protected String updateBy;//更新者的id
    protected String updateTime;//更新时间

xxx.hbm.xml

        <!-- private Set<User> users = new HashSet<User>(0);//部门与用户一对多 -->
        <set name="users">
            <key column="DEPT_ID"></key>
            <one-to-many class="User"/>
        </set>


    <!-- 让数据库的默认值起作用 false不起作用 -->
    <class name="Role" table="ROLE_P" dynamic-insert="true" dynamic-update="true">
        <id name="id" column="ROLE_ID">
            <generator class="uuid"></generator>
        </id>   
        <property name="name" column="NAME"></property>
        <property name="remark" column="REMARK"></property>
        <property name="orderNo" column="ORDER_NO"></property>

        <property name="createBy" column="CREATE_BY"></property>
        <property name="createDept" column="CREATE_DEPT"></property>
        <property name="createTime" column="CREATE_TIME"></property>
        <property name="updateBy" column="UPDATE_BY"></property>
        <property name="updateTime" column="UPDATE_TIME"></property>

        <!-- private Set<Role> users = new HashSet<Role>(0);//用户和角色 多对多 -->
        <!-- name是属性名 table 是中间表名 -->
        <set name="users" table="ROLE_USER_P">
            <key column="ROLE_ID"></key>
            <many-to-many class="User" column="USER_ID"></many-to-many>
        </set>
    </class>

            <!--private Dept dept;//用户与部门 多对一  -->      
        <many-to-one name="dept" class="Dept" column="DEPT_ID"></many-to-one>
        <!--    private Userinfo userinfo;//用户与用户扩展信息 一对一 级联cascade能保存数据到userinfo数据 -->
        <one-to-one name="userinfo" class="Userinfo" cascade="all"></one-to-one>

        <!-- private Set<Role> roles = new HashSet<Role>(0);//用户与角色 多对多 -->
        <set name="roles" table="ROLE_USER_P">
            <key column="USER_ID"></key>
            <many-to-many class="Role" column="ROLE_ID"></many-to-many>
        </set>

            <!--private User manager;//用户与直属领导 多对一 -->      
        <many-to-one name="manager" class="User" column="MANAGER_ID"></many-to-one>

service

    public void deleteById(Class<Dept> entityClass, Serializable id) {
        //有哪些子部门,它的父部门编号为第二个参数:id
        String hql = "from Dept where PARENT_ID.DEPT_ID=?";
        List<Dept> list = baseDao.find(hql, Dept.class, new Object[]{id});//查出当前父部门下的子部门的列表
        if(list!=null && list.size()>0) {
            for(Dept dept:list) {
                deleteById(Dept.class,dept.getDEPT_ID());//递归调用

            }
        }
        baseDao.deleteById(entityClass, id);//删除父部门
    }
        public void delete(Class<Dept> entityClass, Serializable[] ids) {
        for(Serializable s : ids){
            this.deleteById(entityClass, s);
        }
    }

DeptAction

    private Dept model = new Dept();
    public Dept getModel() {
        return model;
    }


    //分页查询
    private Page page = new Page();
    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

    //注入DeptService
    private DeptService deptService;
    public void setDeptService(DeptService deptService) {
        this.deptService = deptService;
    }

    /**
     * 分页查询
     */
    public String list() throws Exception {

        //引用数据类型在方法体内对它的修改是有用的
        deptService.findPage("from Dept", page, Dept.class, null);
        //设置分页的url地址
        page.setUrl("deptAction_list");
        //将page对象压入栈顶 集合用put
        super.push(page);
        return "list";
    }

    /**
     * 查看
     *  DEPT_ID =dsa
     * model对象
     *  DEPT_IDs属性:
     */
    public String toView() throws Exception {
        //1、调用业务方法,根据id得到对象
        Dept dept = deptService.get(Dept.class, model.getDEPT_ID());
        //2、放入栈顶
        super.push(dept);
        //3、跳页面
        return "toview";
    }
    /**
     * 进入新增页面
     */
    public String tocreate() throws Exception {
        //调用业务方法
        List<Dept> deptList = deptService.find("from Dept where state=1", Dept.class, null);
        //将查询的结果放入值栈中
        super.put("deptList", deptList);
        //跳页面
        return "tocreate";
    }

    /**
     * 保存
     *  <s:select name="PARENT_ID.PARENT_ID"
     *  <input type="text" name="DEPT_NAME" value=""/>
     * model对象能接受
     *  PARENT_ID
     *   PARENT_ID
     *  DEPT_NAME   
     */
    public String toinsert() throws Exception {
        //1、调用业务方法,实现保存 有oid执行update 没有save 因为不是已经存在的
        deptService.saveOrUpdate(model);
        //跳页面
        return "alist";
    }
    /**
     * 进入修改页面
     * @return
     * @throws Exception
     */
    public String toupdate() throws Exception {
        //1、根据id,得到一个对象
        Dept obj = deptService.get(Dept.class, model.getDEPT_ID());
        //2、将对象放入值栈中
        super.push(obj);
        //3、查询父部门
        List<Dept> deptList = deptService.find("from Dept where state=1", Dept.class, null);
        //4、将查询的结果放入值栈中,放在context区域中
        super.put("deptList", deptList);
        //5、条页面
        return "toupdate";
    }

    /**
     * 修改
     */
    public String update() throws Exception {
        //调用业务
        Dept obj = deptService.get(Dept.class, model.getDEPT_ID());//根据id,得到一个数据库中保存的对象
        //2、设置要修改的属性
        obj.setPARENT_ID(model.getPARENT_ID());
        obj.setDEPT_NAME(model.getDEPT_NAME());
        deptService.saveOrUpdate(obj);
//      deptService.close();
        return "alist";
    }
    /**
     * 删除
     * <input type="checkbox" name="DEPT_ID" value="100"/>
     * <input type="checkbox" name="DEPT_ID" value="4028827c4fb6202a014fb6209c730000"/>
     * model
     *  id:String类型
     *      具有同名框的一组值如何封装数据
     *      如果服务器端是String类型:
     *          100,4028827c4fb6202a014fb6209c730000,12
     *  id:Integer,Float,Double,Date 类型 id=100 id=112 id=116
     *  id=116 被删
     *  Integer []id;{100,112,116}
     * 
     *      
     * @return
     * @throws Exception
     */
    public String delete() throws Exception {
        String ids[] = model.getDEPT_ID().split(",");
        //调用业务方法,实现批量删除
        deptService.delete(Dept.class, ids);
        return "alist";
    }
}

猜你喜欢

转载自blog.csdn.net/civilizationv/article/details/80244109