hibernate中将视图进行映射

来源:互联网 发布:除了淘宝联盟哪个最好 编辑:程序博客网 时间:2024/05/29 04:01

1、在mysql中创建视图

CREATE  VIEW `t_beauty_parlor_view` AS (select `p`.`id` AS `id`,`p`.`name` AS `name`,`p`.`address` AS `address`,`p`.`tel` AS `tel`,`p`.`create_time` AS `create_time`,NULL AS `pid` from `t_beauty_parlor` `p` where (`p`.`del` = 0)) union (select `s`.`id` AS `id`,`s`.`name` AS `name`,`s`.`address` AS `address`,`s`.`tel` AS `tel`,`s`.`create_time` AS `create_time`,`s`.`parlor_id` AS `pid` from `t_beauty_shop` `s` where (`s`.`del` = 0))

2、BeautyParlorView.java如下

@SuppressWarnings("serial")public class BeautyParlorView implements java.io.Serializable {    private String id;    //名称    private String name;    //电话    private String tel;    //地址    private String address;    private String pid;    private Date createTime;    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getTel() {        return tel;    }    public void setTel(String tel) {        this.tel = tel;    }    public String getAddress() {        return address;    }    public void setAddress(String address) {        this.address = address;    }    public String getPid() {        return pid;    }    public void setPid(String pid) {        this.pid = pid;    }    public Date getCreateTime() {        return createTime;    }    public void setCreateTime(Date createTime) {        this.createTime = createTime;    }}

3、BeautyParlorView.hbm.xml如下:

<hibernate-mapping>    <class name="com.weiran.model.BeautyParlorView" table="t_beauty_parlor_view"   >        <id name="id" type="java.lang.String">            <column name="ID" length="50" />            <generator class="uuid.hex" />         </id>         <property name="name" type="java.lang.String">            <column name="name" length="60" />        </property>        <property name="tel" type="java.lang.String">            <column name="tel" length="20" />        </property>        <property name="address" type="java.lang.String">            <column name="address" length="100" />        </property>        <property name="pid" type="java.lang.String">            <column name="pid" length="50" />        </property>        <property name="createTime" type="java.util.Date">            <column name="create_time" length="23" />        </property>    </class></hibernate-mapping>

4、BeautyParlorViewServiceImpl.java如下

@Servicepublic class BeautyParlorViewServiceImpl extends BaseServiceImpl<String, BeautyParlorView> implements BeautyParlorViewServiceIntf {    @Override    public List<BeautyParlorView> queryBeautyParlorTree(String name) throws Exception {        StringBuffer sb = new StringBuffer(" from BeautyParlorView t where 1 = 1 ");        if(StringUtil.notNull(name)){            sb.append(" and t.name like '%"+name+"%' ");        }        sb.append(" order by t.createTime desc ");        List<BeautyParlorView> result =  baseDao.query(sb.toString());        return result;    }}