Apache Click快速开发整合Hibernate33和Spring30.docx
- 文档编号:6439105
- 上传时间:2023-01-06
- 格式:DOCX
- 页数:18
- 大小:20.31KB
Apache Click快速开发整合Hibernate33和Spring30.docx
《Apache Click快速开发整合Hibernate33和Spring30.docx》由会员分享,可在线阅读,更多相关《Apache Click快速开发整合Hibernate33和Spring30.docx(18页珍藏版)》请在冰豆网上搜索。
ApacheClick快速开发整合Hibernate33和Spring30
[ApacheClick快速开发]整合Hibernate3.3和Spring3.0
[ApacheClick快速开发]整合Hibernate3.3和Spring3.0
分类:
Java2011-10-3118:
07151人阅读评论
(1)收藏举报
俗话说,说的多,不如做的多。
废话不说了,快速进入配置。
首先,hibernate配置。
mysql中新建名为click的数据库,新建一个person表,表结构如下:
+-----------+--------------+------+-----+---------+----------------+
|Field|Type|Null|Key|Default|Extra|
+-----------+--------------+------+-----+---------+----------------+
|id|int(11)|NO|PRI|NULL|auto_increment|
|name|varchar(100)|YES||NULL||
|gender|varchar(10)|YES||NULL||
|birth|datetime|YES||NULL||
|email|varchar(255)|YES||NULL||
|tel|varchar(255)|YES||NULL||
|job|varchar(100)|YES||NULL||
|salary|float|YES||NULL||
|education|varchar(50)|YES||NULL||
|remark|varchar(500)|YES||NULL||
|active|int(11)|YES||NULL||
+-----------+--------------+------+-----+---------+----------------+
完成后,在eclipse中新建工程,并添加click支持。
不知道怎么添加click支持的请点击[ApacheClick快速开发]开发环境配置初试
1.配置web.xml,这些在新建工程完毕后是可以自动生成的,click和spring整合所用的关键类为SpringClickServlet
viewplaincopytoclipboard?
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-beans.xml</param-value>
</context-param>
<filter>
<filter-name>PerformanceFilter</filter-name>
<filter-class>org.apache.click.extras.filter.PerformanceFilter</filter-class>
<init-param>
<param-name>cachable-paths</param-name>
<param-value>/assets/*</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PerformanceFilter</filter-name>
<servlet-name>ClickServlet</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>PerformanceFilter</filter-name>
<url-pattern>*.css</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>PerformanceFilter</filter-name>
<url-pattern>*.js</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>PerformanceFilter</filter-name>
<url-pattern>*.gif</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>PerformanceFilter</filter-name>
<url-pattern>*.png</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>ClickServlet</servlet-name>
<servlet-class>org.apache.click.extras.spring.SpringClickServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ClickServlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
修改click.xml配置文件,添加clickpage所在包
viewplaincopytoclipboard?
<!
DOCTYPEclick-appPUBLIC
"-//ApacheSoftwareFoundation//DTDClickConfiguration2.2//EN"
"http:
//click.apache.org/dtds/click-2.2.dtd">
<click-appcharset="UTF-8">
<pagespackage="com.csh.pages"autobinding="annotation">
</pages>
<modevalue="debug"/>
</click-app>
2.新建hibernate持久化对象Person,这里就不详细讲了,相信做过Hibernate持久化的都知道。
下面是spring配置(hibernate.cfg.xml省略掉,直接用spring托管),
viewplaincopytoclipboard?
<beansxmlns="http:
//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns:
tx="http:
//www.springframework.org/schema/tx"
xmlns:
aop="http:
//www.springframework.org/schema/aop"
xmlns:
context="http:
//www.springframework.org/schema/context"
xsi:
schemaLocation="http:
//www.springframework.org/schema/aop
http:
//www.springframework.org/schema/aop/spring-aop-3.0.xsd
http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context-3.0.xsd
http:
//www.springframework.org/schema/tx
http:
//www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:
annotation-config/>
<context:
component-scanbase-package="com.csh"/>
<beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close">
<propertyname="driverClass"value="com.mysql.jdbc.Driver"/>
<propertyname="jdbcUrl"value="jdbc:
mysql:
//localhost:
3306/click"/>
<propertyname="user"value="root"/>
<propertyname="password"value="root"/>
<propertyname="minPoolSize"value="10"/>
<propertyname="maxPoolSize"value="20"/>
<propertyname="maxIdleTime"value="1800"/>
<propertyname="acquireIncrement"value="2"/>
<propertyname="maxStatements"value="0"/>
<propertyname="initialPoolSize"value="2"/>
<propertyname="idleConnectionTestPeriod"value="1800"/>
<propertyname="acquireRetryAttempts"value="30"/>
<propertyname="breakAfterAcquireFailure"value="true"/>
<propertyname="testConnectionOnCheckout"value="false"/>
</bean>
<beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"></property>
<propertyname="hibernateProperties">
<props>
<propkey="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<propkey="hibernate.show_sql">true</prop>
</props>
</property>
<propertyname="mappingResources">
<list>
<value>com/csh/beans/Person.hbm.xml</value>
</list>
</property>
</bean>
</beans>
3.配置完成后,完成dao层和manager业务层书写。
首先,我用hibernatedaosupport实现了commonDao,并采用泛型让子类来指定具体持久化类型。
viewplaincopytoclipboard?
@Repository("commonDao")
publicclassCommonDaoHibernateImpl<T>extendsHibernateDaoSupportimplementsCommonDao<T>{
privateClass<T>clazz;
@Autowired
publicvoidsetSuperSessionFactory(SessionFactorysessionFactory){
super.setSessionFactory(sessionFactory);
}
@SuppressWarnings("unchecked")
publicCommonDaoHibernateImpl(){
if(this.getClass().getGenericSuperclass()instanceofParameterizedType){
clazz=(Class<T>)((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
}
publicvoidadd(Tentity){
getHibernateTemplate().save(entity);
}
publicintcount(Stringhql,Object...objects){
Listlist=getHibernateTemplate().find(hql,objects);
returnlist.size()==1?
Integer.parseInt(list.get(0).toString()):
0;
}
publicintcount(Stringhql){
Listlist=getHibernateTemplate().find(hql);
returnlist.size()==1?
Integer.parseInt(list.get(0).toString()):
0;
}
publicvoiddelete(Tentity){
getHibernateTemplate().delete(entity);
}
@SuppressWarnings("unchecked")
publicTfind(Serializablepk){
return(T)getHibernateTemplate().get(clazz,pk);
}
publicTfind(finalStringhql,finalObject...objects){
return(T)getHibernateTemplate().execute(newHibernateCallback<T>(){
@SuppressWarnings("unchecked")
publicTdoInHibernate(Sessionsession)throwsHibernateException,
SQLException{
Queryquery=session.createQuery(hql);
setParameters(query,objects);
return(T)query.uniqueResult();
}
});
}
@SuppressWarnings("unchecked")
publicList<T>query(finalStringhql,finalintpage,finalintsize,finalObject...objects){
returngetHibernateTemplate().executeFind(newHibernateCallback(){
publicList<T>doInHibernate(Sessionsession)throwsHibernateException,SQLException{
Queryquery=session.createQuery(hql);
setParameters(query,objects);
if(page!
=0&&size!
=0){
query.setFirstResult((page-1)*size);
query.setMaxResults(size);
}
return(List<T>)query.list();
}
});
}
@SuppressWarnings("unchecked")
publicList<T>query(Stringhql){
return(List<T>)getHibernateTemplate().find(hql);
}
publicvoidupdate(Tentity){
getHibernateTemplate().update(entity);
}
privatevoidsetParameters(Queryquery,Object...objects){
for(inti=0;i<objects.length;i++){
query.setParameter(i,objects[i]);
}
}
}
然后实现personDao
viewplaincopytoclipboard?
@Repository("personDao")
publicclassPersonDaoImplextendsCommonDaoHibernateImpl<Person>implementsPersonDao{
publicList<Person>queryPerson(Stringhql,intpage,intsize,
Object...objects){
returnsuper.query(hql,page,size,objects);
}
publicPersonfindPerson(intid){
returnsuper.find(id);
}
publicvoidaddPerson(Personperson){
super.add(person);
}
publicvoidupdatePerson(Personperson){
super.update(person);
}
}
接着是业务管理器personManagerviewplaincopytoclipboard?
@Service("personManager")
publicclassPersonManagerImplimplementsPersonManager{
@Autowired
privatePersonDaopersonDao;
publicvoidsetPersonDao(PersonDaopersonDao){
this.personDao=personDao;
}
publicList<Person>queryPerson(intpage,intsize){
returnpersonDao.queryPerson("fromPerson",page,size);
}
publicList<Person>queryPersonByName(Stringname,intpage,intsize){
returnpersonDao.queryPerson("selectpfromPersonpwherep.namelike?
",page,size,"%"+name+"%");
}
publicList<Person>queryPersonByStatus(intstatus,intpage,intsize){
returnpersonDao.queryPerson("fromPersonpwherep.acti
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Apache Click快速开发整合Hibernate33和Spring30 Click 快速 开发 整合 Hibernate33 Spring30