首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

JavaSSH+mysql 不能实现抽象类,该如何解决

2012-10-16 
JavaSSH+mysql 不能实现抽象类第一个问题: 项目有一个抽象类Java codepackage com.spm.daoimport java.ut

JavaSSH+mysql 不能实现抽象类
第一个问题: 项目有一个抽象类

Java code
package com.spm.dao;import java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.spm.model.Company;/** * 公司抽象类 * @author Administrator * */public interface CompanyDao{        @SuppressWarnings("unchecked")    public List companyNameList(String compName);        public Company companyId(Company company);            public boolean companyAdd(Company company);    public boolean companStopList(String listid);    public boolean companUpdate(Company company);        public boolean companDelete(Company company);    public int companAll();}



实现类

Java code
package com.spm.dao.impl;import java.util.List;import javax.management.Query;import org.hibernate.Session;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.spm.dao.CompanyDao;import com.spm.model.Company;/** * 公司实现类 * @author Administrator * */public class CompanyDaoImpl extends HibernateDaoSupport implements CompanyDao  {        public boolean companStopList(String listid) {        // TODO Auto-generated method stub        return false;    }        public boolean companUpdate(Company company) {        // TODO Auto-generated method stub        this.getHibernateTemplate().update(company);        return false;    }        public boolean companyAdd(Company company) {                System.out.println("添加公司xx");        System.out.println(company.getCompFName()+company.getCompName());        // TODO Auto-generated method stub        try {            this.getHibernateTemplate().save(company);            return true;        } catch (Exception e) {            // TODO: handle exception            e.printStackTrace();            return false;        }    }            public Company companyId(Company company) {        // TODO Auto-generated method stub        String Hql="from Company where compId=?";        Object[] value={company.getCompId()};        Company company2=(Company) this.getHibernateTemplate().find(Hql,value).get(0);        return company2;    }        public List companyNameList(String compName) {        // TODO Auto-generated method stub        String hql="from Company where compFName like ? or compName like ?";        //String hql="from Company where compFName like %"+compName+"% or compName like %"+compName+"%";        org.hibernate.Query query =this.getSession().createQuery(hql);        query.setString(0,"%"+compName+"%");        query.setString(1,"%"+compName+"%");                List list=query.list();        return list;    }    /**     * 刪除公司     * @param company     * @return     */    public boolean companDelete(Company company) {        // TODO Auto-generated method stub        System.out.println("删除公司xx");        try {            this.getHibernateTemplate().delete(company);            return true;        } catch (Exception e) {            // TODO: handle exception        }        return false;    }            public List<Company> companPage(int pageNo,int pagesize){                String Hql="from Company";        Session session=this.getSession();        List list =session.createQuery(Hql).setFirstResult((pageNo-1)*10).setMaxResults(pagesize).list();        //this.getSession().close();        session.close();        return list;    }    /**     * 查詢所有公司     */    public int companAll(){        String Hql="from Company";        return this.getSession().createQuery(Hql).list().size();    }    }


applicationContext.xml 配置

XML code

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="        http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd        http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop-2.5.xsd        http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-2.5.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-2.5.xsd">    <context:annotation-config />    <context:component-scan base-package="com.spm" />        <!-- 定义一个数据源 -->    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">        <property name="driverClassName" value="com.mysql.jdbc.Driver" />        <property name="url" value="jdbc:mysql://localhost/spm?useUnicode=true&amp;characterEncoding=UTF-8" />        <property name="username" value="root" />        <property name="password" value="root" />                <!-- Connection Pooling Info --><!--        <property name="initialSize" value="5" /> 初始化线程数  --><!--        <property name="maxActive" value="100" /> 取出连接最大个数,0表示没有限制,默认为8 --><!--        <property name="maxIdle" value="20"/> 最大等待连接中的数量,设 0为没有限制 --><!--        <property name="maxWait" value="1000"/> 最大等待秒数, 单位为 ms --><!--        <property name="removeAbandoned" value="true"/> 是否自我中断, 默认是 false --><!--        <property name="removeAbandonedTimeout" value="120"/> 几秒后会自我中断, removeAbandoned 必须为 true --><!--        <property name="poolPreparedStatements" value="true" />-->    </bean>        <!-- 配置SessionFactory -->    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <property name="packagesToScan">            <list>                <value>com.spm.model</value>            </list>        </property>        <property name="hibernateProperties">            <value>                hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect                hibernate.show_sql=true                hibernate.format_sql=true                hibernate.hbm2ddl.auto=update            </value>        </property>            </bean>        <!-- 定义hibernateTemplate -->    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>    <!-- 1. 事务通知 -->    <bean id="tranaction"        class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory" />    </bean>    <!-- 2. 设置事务的传播特性 -->    <tx:advice id="tranManager" transaction-manager="tranaction">        <tx:attributes>            <tx:method name="find*" read-only="true"/><!--find开头的不需要走事物-->            <tx:method name="*" propagation="REQUIRED" /><!-- 都要走事物 -->        </tx:attributes>    </tx:advice>    <!-- 3. 使用aop将事务切入到具体的DaO中 -->    <aop:config>        <aop:pointcut id="pointcut" expression="execution(* com.spm.dao.*.*(..))"/>        <aop:advisor advice-ref="tranManager" pointcut-ref="pointcut" />    </aop:config>                            <!-- 公司配置 -->    <bean id="companyDao" class="com.spm.dao.impl.CompanyDaoImpl">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>        <bean id="companyAction" class="com.spm.action.CompanyAction">        <property name="companyDao" ref="companyDao"></property>    </bean>                    </beans> 



Struts2 xml 配置

XML code
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>    <constant name="struts.enable.DynamicMethodInvocation" value="true" />    <constant name="struts.action.extension" value="do" />    <constant name="struts.i18n.encoding" value="UTF-8" />    <constant name="struts.custom.i18n.resources" value="messages" />    <constant name="struts.devMode" value="true" />        <package name="default" extends="struts-default" namespace="/">         <!-- 全局results配置 -->      <global-results>            <result name="error">/404.html</result>      </global-results>                <action name="login" class="com.spm.action.LoginAction">            <result name="success">                /WEB-INF/frame.jsp            </result>        </action>            <action name="Deletecompany" class="companyAction" method="companyDelete">            <result name="ok">/index.jsp</result>            <result name="error">/404.html</result>        </action>                    <action name="SelectIdcompany" class="companyAction" method="companyId">            <result name="ok">/showCompany.jsp</result>        </action>                <!-- 分页查询 -->        <action name="companyPage" class="companyAction" method="companyPage">            <result name="ok">/WEB-INF/company/frame_right.jsp</result>        </action>    </package></struts>  


报错情况如下
ERROR 2012-05-22 11:10:13 ContextLoader:initWebApplicationContext --- Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'companyAction' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy11 implementing com.spm.dao.CompanyDao,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.spm.dao.impl.CompanyDaoImpl] for property 'companyDao'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy11 implementing com.spm.dao.CompanyDao,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.spm.dao.impl.CompanyDaoImpl] for property 'companyDao': no matching editors or conversion strategy found



[解决办法]
<value>com/spm/model</value>

热点排行