一、集合映射
1、set映射
关联对象的属性除了外键之外,只有1、2个属性,那么就可以使用set映射
使用了set标签的element元素,不用创建关联对象就可以实现单向一对多的关联关系
public class Room implements Serializable{
private int id;
private String roomNumber;
private Set<String> equipments = new HashSet<String>();
private Set<Image> images = new HashSet<Image>();
}
<set name="equipments" table="equipment_set">
<key column="roomid" foreign-key="fk_equip_room_set"/>
<element column="description" type="string" length="128" not-null="true"/>
</set>
<set name="images" table="image_set">
<key column="roomid" foreign-key="fk_img_room_set"/>
<composite-element class="Image">
<property name="path" column="path" type="string" length="50" not-null="true"/>
<property name="width" column="width" type="integer" />
<property name="height" column="height" type="integer" />
</composite-element>
2、map映射
非常有用的一种集合映射
public class Room implements Serializable{
private int id;
private String roomNumber;
private Map<String, String> equipments = new HashMap<String, String>();
private Map<String, Image> images = new HashMap<String, Image>();
}
<map name="equipments" table="equipment_map">
<key column="roomid" foreign-key="fk_equip_room_map"/>
<map-key column="name" type="string" length="15" />
<element column="description" type="string" length="128" not-null="true"/>
</map>
<map name="images" table="image_map">
<key column="roomid" foreign-key="fk_img_room_map"/>
<map-key column="name" type="string" length="15" />
<composite-element class="Image">
<property name="path" column="path" type="string" length="50" not-null="true"/>
<property name="width" column="width" type="integer" />
<property name="height" column="height" type="integer" />
</composite-element>
</map>
二、HQL
1、query
(1) session.createQuery("from User");
session.createCriteria(User.class);
(2)查询的分页显示
query.setFirstResult(0);
query.setMaxResult(10);
(a) MySql
select * from student where limite ?,?
(b) Oracle
select * from (select row_.* ,rownum rownum_
from (select student0_.id as id0_,
from student student0_) row)
where rownum_ <= ?
and rownum_ > ?
(3)在配置文件中的query
<query name="findByName">
<![CDATA[ --- 当作字符串看,不做特殊处理
from User u where u.namelike :name
]]>
</query>
session.getNamedQuery("findByName").setString("name",name);
from User; 返回对象数组,不可以强转
select u from User u; 返回对象集合,对于集合中的每个元素都可以强转成User对象
2、Dynamic query
public Collection findStudents(Student student){
StringBuffer sb = new StringBuffer("select student from Student student where student.id>-1 ");
--- student.id>-1 永远为真,再添加条件的时候都用and即可,避免判断用where还是and
if(student.getName() != null)
sb.append("and student.name=:name");
if(student.getBirthday() != null)
sb.append("and student.birthday=:birthday");
if(student.getEmail() != null)
sb.append("and student.email=:email");
……
}
3、QBE
属性多时,效率高,语句少
忽略主键、version、关系(查询当前对象)
与QBC结合,弥补id查询
Example.excludeProperty("param"); --- 查询时忽略某个属性
1、set映射
关联对象的属性除了外键之外,只有1、2个属性,那么就可以使用set映射
使用了set标签的element元素,不用创建关联对象就可以实现单向一对多的关联关系
public class Room implements Serializable{
private int id;
private String roomNumber;
private Set<String> equipments = new HashSet<String>();
private Set<Image> images = new HashSet<Image>();
}
<set name="equipments" table="equipment_set">
<key column="roomid" foreign-key="fk_equip_room_set"/>
<element column="description" type="string" length="128" not-null="true"/>
</set>
<set name="images" table="image_set">
<key column="roomid" foreign-key="fk_img_room_set"/>
<composite-element class="Image">
<property name="path" column="path" type="string" length="50" not-null="true"/>
<property name="width" column="width" type="integer" />
<property name="height" column="height" type="integer" />
</composite-element>
2、map映射
非常有用的一种集合映射
public class Room implements Serializable{
private int id;
private String roomNumber;
private Map<String, String> equipments = new HashMap<String, String>();
private Map<String, Image> images = new HashMap<String, Image>();
}
<map name="equipments" table="equipment_map">
<key column="roomid" foreign-key="fk_equip_room_map"/>
<map-key column="name" type="string" length="15" />
<element column="description" type="string" length="128" not-null="true"/>
</map>
<map name="images" table="image_map">
<key column="roomid" foreign-key="fk_img_room_map"/>
<map-key column="name" type="string" length="15" />
<composite-element class="Image">
<property name="path" column="path" type="string" length="50" not-null="true"/>
<property name="width" column="width" type="integer" />
<property name="height" column="height" type="integer" />
</composite-element>
</map>
二、HQL
1、query
(1) session.createQuery("from User");
session.createCriteria(User.class);
(2)查询的分页显示
query.setFirstResult(0);
query.setMaxResult(10);
(a) MySql
select * from student where limite ?,?
(b) Oracle
select * from (select row_.* ,rownum rownum_
from (select student0_.id as id0_,
from student student0_) row)
where rownum_ <= ?
and rownum_ > ?
(3)在配置文件中的query
<query name="findByName">
<![CDATA[ --- 当作字符串看,不做特殊处理
from User u where u.namelike :name
]]>
</query>
session.getNamedQuery("findByName").setString("name",name);
from User; 返回对象数组,不可以强转
select u from User u; 返回对象集合,对于集合中的每个元素都可以强转成User对象
2、Dynamic query
public Collection findStudents(Student student){
StringBuffer sb = new StringBuffer("select student from Student student where student.id>-1 ");
--- student.id>-1 永远为真,再添加条件的时候都用and即可,避免判断用where还是and
if(student.getName() != null)
sb.append("and student.name=:name");
if(student.getBirthday() != null)
sb.append("and student.birthday=:birthday");
if(student.getEmail() != null)
sb.append("and student.email=:email");
……
}
3、QBE
属性多时,效率高,语句少
忽略主键、version、关系(查询当前对象)
与QBC结合,弥补id查询
Example.excludeProperty("param"); --- 查询时忽略某个属性