我不是在寻找Hibernate/JPA/JDBC实现,而是寻找一般的设计模式.
谷歌搜索"分页"给了我大量的信息,许多有趣的文章,解释如何在UI上实现分页和各种实现,或多或少相同.
由于我使用的是Spring 3.0.5,我偶然发现了这篇很好的参考文章如何在Spring MVC 3中实现分页.
简单的豆子:
public class Person{
private String personName;
private int age;
// ...
}
Run Code Online (Sandbox Code Playgroud)
一个简单的DAO接口:
public interface PersonDAO{
Set<Person> getAllPersons(int start, int limit,String orderBy);
Set<Person> findPersonsByName(String name, int start, int limit,String orderBy);
}
Run Code Online (Sandbox Code Playgroud)
和hibernate实现
@Repository
public class PersonDAOImpl implements PersonDAO {
@Autowired(required = true)
private SessionFactory sessionFactory;
public Set<Person> getAllPersons(int start, int limit, String orderBy){
Criteria crit = sessionFactory.getCurrentSession().createCriteria(Person.class);
crit.setFirstResult(start);
crit.setMaxResults(limit);
crit.addOrder(Order.asc("personName"));
return new LinkedHashSet<Person>(crit.list());
}
public Set<Person> findPersonsByName(String name, int start, int …
Run Code Online (Sandbox Code Playgroud) 假设有一个带有结构的User表:
用户
我想只检索当前公司的用户(公司可以由用户通过UI更改,因此公司是运行时参数)
类似地,还有许多其他表与公共列(公司)具有相似的结构,我想将数据限制为仅当前公司,因此我使用hibernate过滤器来过滤数据.
Hibernate注释:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">Dialect....</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.connection.release_mode">after_transaction</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>User</value>
.....
</list>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
过滤器定义:
@org.hibernate.annotations.FilterDef(name="restrictToCurrentCompany", parameters = {@org.hibernate.annotations.ParamDef( name = "currentCompanyNumber", type = "int" ) } ) @Entity @Table(name = "USER") @org.hibernate.annotations.Filter( name = "restrictToCurrentCompany", condition="company = :currentCompanyNumber" ) public class User implements Serializable { private int company; private String userName; …
我有以下类层次结构场景; A类有一个方法,B类扩展了A类,我想从本地嵌套类中调用超类的方法.我希望一个骨架结构更清楚地表明场景Java是否允许这样的调用?
class A{
public Integer getCount(){...}
public Integer otherMethod(){....}
}
class B extends A{
public Integer getCount(){
Callable<Integer> call = new Callable<Integer>(){
@Override
public Integer call() throws Exception {
//Can I call the A.getCount() from here??
// I can access B.this.otherMethod() or B.this.getCount()
// but how do I call A.this.super.getCount()??
return ??;
}
}
.....
}
public void otherMethod(){
}
}
Run Code Online (Sandbox Code Playgroud)