2 zyhlearnjava zyhlearnjava 于 2017.01.16 11:17 提问

ssh整合问题,怎么办,求指导

tomcat启动正常,showsql 也打开了,用jsp输出数据做测试,数据是从数据库查询的,现在页面没数据输出,控制台也没有sql语句出来

13个回答

u013829202
u013829202   Rxr 2017.01.16 13:01

没报错吗? 你debug有数据吗?

u013829202
u013829202 你的hbm.xml。class的name要用java的全限定名。 <class name="这个地方用全限定名" table="t_dept">
10 个月之前 回复
u011806486
u011806486 回复宏飞英翔: 对
10 个月之前 回复
u011806486
u011806486 测试看看
10 个月之前 回复
zyhlearnjava
zyhlearnjava 没报错,我是自学的,web项目不会debug
11 个月之前 回复
static_coder
static_coder   2017.01.16 14:33

看项目有没有部署到服务器上?

zyhlearnjava
zyhlearnjava 部署上去了,写了测试类,可以查询,就是jsp没有输出查询结果,而且控制台也没sql语句出来
11 个月之前 回复
zyhlearnjava
zyhlearnjava   2017.01.17 01:05

我在浏览器直接调action,org.hibernate.MappingException: Unknown entity: 出现这个错误

zyhlearnjava
zyhlearnjava 回复智_永无止境: 引入了啊,我看看可不可以把代码发上来
11 个月之前 回复
static_coder
static_coder 那你看看这个实体在什么地方有么有引入?在配置文件中看看映射又不是没有引入?
11 个月之前 回复
qq_24972061
qq_24972061   2017.01.17 08:27

用debug进行调试呗。。。。

zyhlearnjava
zyhlearnjava 好的,谢谢,我还不会用debug,我是自学的,我去找找资料
11 个月之前 回复
zyhlearnjava
zyhlearnjava   2017.01.17 14:18

图片说明这是结构

zyhlearnjava
zyhlearnjava   2017.01.17 15:01

我用junit测试,可以查询输出到控制台。我把别人的源代码拷过来也是这个样子,我用浏览器直接调action,浏览器报错,访问页面又没数据图片说明

zyhlearnjava
zyhlearnjava   2017.01.17 15:03

dept.hbm.xml

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<!-- 

  This mapping demonstrates content-based discrimination for the
  table-per-hierarchy mapping strategy, using a formula
  discriminator.

-->

<hibernate-mapping  package="entity">

    <class name="Dept" table="t_dept">
        <!-- 主键,映射 -->
        <id name="id" column="deptId">
            <generator class="native"/>
        </id>
        <!-- 非主键,映射 -->
        <property name="name" column="deptName"></property>

    </class>


</hibernate-mapping>

employee.hbm.xml

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<!-- 

  This mapping demonstrates content-based discrimination for the
  table-per-hierarchy mapping strategy, using a formula
  discriminator.

-->
<hibernate-mapping  package="entity">   
    <class name="Employee" table="t_employee">
        <!-- 主键,映射 -->
        <id name="id" column="empId">
            <generator class="native"/>
        </id>
        <!-- 非主键,映射 -->
        <property name="empName" ></property>
        <property name="salary" ></property>
        <!-- 多对一 -->
        <many-to-one name="dept" column="dept_id" class="Dept"></many-to-one>
    </class>
</hibernate-mapping>
zyhlearnjava
zyhlearnjava   2017.01.17 15:06

bean-base.xml

 <?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx.xsd">


    <!-- 所有配置的公共部门 -->

    <!-- 1) 连接池实例 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mydb"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
        <property name="initialPoolSize" value="3"></property>
        <property name="maxPoolSize" value="6"></property>
    </bean>

    <!-- 2) SessionFactory实例创建 -->
    <!-- 所有的配置都由spring维护(项目中不需要hibernate.cfg.xml啦) -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!-- a. 连接池 -->

        <property name="dataSource" ref="dataSource"></property>

        <!-- b. hibernate常用配置: 方言、显示sql、自动建表等 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>

        <property name="mappingLocations">
            <list>
                <value>entity/*.hbm.xml</value>
            </list>
        </property>

    </bean>

    <!-- 3) 事务配置 -->
    <!-- # 事务管理器 -->
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- # 事务增强 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="*" read-only="false"/>
        </tx:attributes>
    </tx:advice>
    <!-- # AOP配置 -->
    <aop:config>
        <aop:pointcut expression="execution(* service.*.*(..))" id="pt"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>

</beans>     

bean-action.xml

 <?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="employeeAction" class="action.EmployeeAction" scope="prototype">
        <property name="employeeService" ref="employeeService"></property>
    </bean>

</beans>     










bean-service.xml

 <?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx.xsd">


<bean id="employeeService" class="service.EmployeeService">
    <property name="employeeDao" ref="employeeDao"></property>
</bean>

</beans>     










bean-dao.xml

 <?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx.xsd">


    <bean id="employeeDao" class="dao.EmployeeDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

</beans>     










zyhlearnjava
zyhlearnjava   2017.01.17 15:11

struts.xml

 <?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>
   <package name="action" extends="struts-default" >
        <action name="show" class="action.EmployeeAction" method="execute">
            <result name="success">/index.jsp</result>
        </action>    
    </package>   
</struts>

web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ssh</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- Struts2配置 -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- spring配置 -->
  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/bean*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>
zyhlearnjava
zyhlearnjava   2017.01.17 15:14

employeeAction.java

 package action;





import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import entity.Employee;
import service.EmployeeService;
import javax.persistence.Entity;



public class EmployeeAction extends ActionSupport{
    private EmployeeService employeeService;    
    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }
    @Override
    public String execute() {
        // TODO Auto-generated method stub
        int empId=2;
        Employee emp = employeeService.findById(empId);
        Map<String, Object> request= (Map<String, Object>) ActionContext.getContext().get("request");
        request.put("emp", emp);

        return "success";
    }

    //这是做测试用的,没有配置进配置文件,与主体无关
    public Employee find(int id) {
        // TODO Auto-generated method stub

        Employee emp = employeeService.findById(id);
        return emp;
    }
}

employeeService.java

 package service;

import java.io.Serializable;

import org.hibernate.SessionFactory;

import dao.EmployeeDao;
import entity.Dept;
import entity.Employee;

public class EmployeeService {
    private EmployeeDao employeeDao;
    public void setEmployeeDao(EmployeeDao employeeDao) {
        this.employeeDao = employeeDao;
    }
    public Employee findById(Serializable id) {
        Employee emp = employeeDao.findById(id);
        return emp;
    }
}

employeeDao.java

 package dao;

import java.io.Serializable;

import org.hibernate.SessionFactory;

import entity.Dept;
import entity.Employee;

public class EmployeeDao {
    private SessionFactory sessionFactory;  
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    public Employee findById(Serializable id) {
//      return (Employee) sessionFactory.openSession().get(Employee.class, id);
        return (Employee)sessionFactory.getCurrentSession().get(Employee.class, id);
    }
}
共13条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!