package com.util;
import org.springframework.orm.hibernate3.HibernateTemplate;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;
public class BasicDao<T> {
private HibernateTemplate hibernateTemplate;
private Class entityClass;
public BasicDao(){
Type genType = getClass().getGenericSuperclass();
Type[] params=((ParameterizedType)genType).getActualTypeArguments();
entityClass = (Class)params[0];
}
public void setHibernateTemplate(HibernateTemplate h){
this.hibernateTemplate=h;
}
//添加
public void insert(T entity){
hibernateTemplate.save(entity);
}
//修改
public void update(T entity){
hibernateTemplate.update(entity);
}
//删除
public void delete(T entity){
hibernateTemplate.delete(entity);
}
//查询
public T get(Serializable id){
return (T)hibernateTemplate.get(entityClass, id);
}
//按条件查询
public List<T> find(String hql,Object param){
return (List<T>)hibernateTemplate.find(hql, param);
}
public HibernateTemplate getHibernateTemplate(){
return hibernateTemplate;
}
}
UserDao code :
package com.user.dao;
import com.jdbc.bean.User;
import com.util.BasicDao;
public class UserDao extends BasicDao<User> {
}
test code :
public static void main(String[] arg0){
Test test = new Test();
UserDao userDao = new UserDao();
List results = userDao.find("from T_USER where USER_NAME LIKE ?", "%zhanghang%");
System.out.println("name password lastTime lastIp");
for(int i=0;i<results.size();i++){
System.out.println(((User)results.get(i)).getUserName()+" "+((User)results.get(i)).getPassword()+" "+((User)results.get(i)).getLastTime()+" "+((User)results.get(i)).getLastIp());
}
} xml configuration file:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
p:driverClassName="${driver}"
p:url="${url}"
p:username="${name}"
p:password="${password}"
/>
<bean id="locationSessionFactoryBean" class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:dataSource-ref="dataSource">
<property name="mappingLocations">
<list>
<value>classpath*:/hibernate/user.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" p:sessionFactory-ref="locationSessionFactoryBean"></bean>
<bean id="BasicDao" class="com.util.BasicDao" p:hibernateTemplate-ref="hibernateTemplate"> </bean>
tested by the test code when reported type conversion exception:
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.util.BasicDao]: Constructor threw exception; nested exception is java.lang.ClassCastException: java.lang.Class
at org.springframework.beans.BeanUtils.instantiateClass (BeanUtils.java: 162)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate (SimpleInstantiationStrategy.java: 76)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean (AbstractAutowireCapableBeanFactory.java: 983)
... 14 more
Caused by: java.lang.ClassCastException: java.lang.Class
at com.util.BasicDao.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance (Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance (Unknown Source)
at java.lang.reflect.Constructor.newInstance (Unknown Source)
at org.springframework.beans.BeanUtils.instantiateClass (BeanUtils.java: 147)
... 16 more
------ Solution ------------------------------------ --------
is estimated that you BasicDAO the constructor problem , you have to get the type of entity classes with annotations it, than this simple
------ Solution --- -----------------------------------------
BasicDAO should not be instantiated it, you want to instantiate should be UserDao, Spring configuration file which replaced it a try.
------ Solution ---------------------------------------- ----
public BasicDao(){
//Type genType = getClass().getGenericSuperclass();
//Type[] params=((ParameterizedType)genType).getActualTypeArguments();
//entityClass = (Class)params[0];
}
constructor code inside the first comment , and then test it. And that public T get (Serializable id) This method can also commented as used in the entityClass.
first determine where the problem lies . If you comment out the data and run the find could find no error , then you think about how this entityClass alternative access methods .
Because not used SSH framework , opinions for reference only.
------ Solution ---------------------------------------- ----
I'm not familiar with your BasicDao get inside the entity class constructor method , so I do not know what is wrong , give you a way , I also wrote type of generic DAO, access entity classes I am using annotations
first define an annotation
package com.web.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 用于标注该DAOImpl类所对应的实体类的名字是什么,以便获取相应的实体类
* @author
*
*/
@Retention(RetentionPolicy.RUNTIME)//代表在运行期保留注解
@Target(ElementType.TYPE)//代表用于类中
public @interface EntityClassName {
/**
* 标注对应的实体类名
* @return 对应的实体类名
*/
String value();
}
Then write a method in BasicDao
/**
* 获取该DAO实现类的对应的实体类
* @return 实体类
* @throws Exception
*/
private Class<?> getPOJOClass() throws Exception{
Class<?> pojoClass = null;
//先判断该实现类的实体类注解是否存在
if(this.getClass().isAnnotationPresent(EntityClassName.class)){
//如果存在获取该注解(即实体类的名字)
String className = ((EntityClassName)getClass().
getAnnotation(EntityClassName.class)).value();
//通过反射获取该实体类
pojoClass = Class.forName(className);
}
return pojoClass;
}
you use to the entity classes when using this method to get
Then in your UserDAO marked with this annotation
package com.user.dao;
import com.jdbc.bean.User;
import com.util.BasicDao;
@EntityClassName("com.web.bean.User") //标注该DAO类是对应于哪个实体类,(包名改成你自己的)
public class UserDao extends BasicDao<User> {
}
------ For reference only ----------------------------------- ----
still has wooden anybody here ?
------ For reference only -------------------------------------- -
struts1 looked on the very thing it was great with two not very good ?
------ For reference only -------------------------------------- -
amount not used struts yo ! Only spring and hibernate
------ For reference only ---------------------------------- -----
has been resolved ! ! As long as it basicDao constructor code to move the subclass constructor among Dao can ! !
没有评论:
发表评论