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

SSH的学习有关问题?不太通求教。20110729

2012-03-12 
SSH的学习问题?不太通求教。20110729开始111111package cn.itcast.beanpublic class Employee {private St

SSH的学习问题?不太通求教。20110729
开始
111111
package cn.itcast.bean;

public class Employee {

private String username;
private String password;
private Gender gender = Gender.MAN;

public Employee() {
//TODO
}

public Employee(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
}




222222
package cn.itcast.service;

import java.util.List;

import cn.itcast.bean.Employee;

public interface EmployeeService {
public void save(Employee employee);
public void update(Employee employee);
public Employee find(String username);
public void delete(String... username);
public List<Employee> list(Employee employee);
}



333333
package cn.itcast.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bean.Employee;
import cn.itcast.service.EmployeeService;

@Service @Transactional
public class EmployeeServiceBean implements EmployeeService {

@Resource SessionFactory factory;


public void save(Employee employee) {
factory.getCurrentSession().persist(employee);
}

public void update(Employee employee) {
factory.getCurrentSession().update(employee);

}

@Transactional(propagation=Propagation.NOT_SUPPORTED)
public Employee find(String username) {
return (Employee)factory.getCurrentSession().get(Employee.class, username);
}


public void delete(String... username) {
for (String name : username) {
factory.getCurrentSession().delete(factory.getCurrentSession().load(Employee.class, name));
}

}

@SuppressWarnings("unchecked")
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public List<Employee> list(Employee employee) {
return factory.getCurrentSession().createQuery("from Employee").list();

}

}


444444
package junit.test;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.bean.Employee;
import cn.itcast.service.EmployeeService;

/**
 * @author Administrator
 *
 */
public class EmployeeTest {
private static EmployeeService employeeService;
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {

try {
ApplicationContext act = new ClassPathXmlApplicationContext(
"beans.xml");
employeeService = (EmployeeService) act.getBean("employeeServiceBean");
} catch (Exception e) {
e.printStackTrace();
}
}

@Test
public void save() {
employeeService.save(new Employee("liming","12345"));
}
@Test
public void update() {
new ClassPathXmlApplicationContext("beans.xml");
}
@Test
public void delete() {


new ClassPathXmlApplicationContext("beans.xml");
}
@Test
public void list() {
new ClassPathXmlApplicationContext("beans.xml");
}
@Test
public void find() {
new ClassPathXmlApplicationContext("beans.xml");
}
}




555555
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itcast.bean">
  <class name="Employee">
  <id name="username" length="20"></id>
  <property name="password" length="20" not-null="true"/>
  <property name="gender" length="5" not-null="true">
  <type name="org.hibernate.type.EnumType">
  <param name="enumClass">cn.itcast.bean.Gender</param>
  <param name="type">12</param>
  </type>
  </property>
  </class>
</hibernate-mapping>


666666
<?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: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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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">

<context:component-scan base-package="cn.itcast"></context:component-scan>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&amp;characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
<property name="initialSize" value="1"/>
<property name="maxActive" value="500"/>
<property name="maxIdle" value="2"/>
<property name="minIdle" value="1"/>
</bean>



<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>cn/itcast/bean/Employee.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false


hibernate.format_sql=false
</value>
</property>
</bean>

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:annotation-driven transaction-manager="txManager"/>

</beans>


Struts


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts SYSTEM "http://struts.apache.org/dtds/struts-2.0.dtd" PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN">
-<struts> 
<!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->
 <constant value="UTF-8" name="struts.i18n.encoding"/> 
<!-- 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。 如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
 <constant value="do" name="struts.action.extension"/> 
<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
 <constant value="false" name="struts.serve.static.browserCache"/> 
<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
 <constant value="true" name="struts.configuration.xml.reload"/> 
<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
 <constant value="true" name="struts.devMode"/> 
<!-- 默认的视图主题 -->
 <constant value="simple" name="struts.ui.theme"/> 
<constant value="spring" name="struts.objectFactory"/> 
-<package name="person" extends="struts-default" namespace="/person"> -
<global-results> 
<result name="message">/WEB-INF/page/message.jsp</result> 
</global-results> -
<action name="action_*" method="{1}" class="personList"> 
<result name="list">/WEB-INF/page/personlist.jsp</result> 
<result name="add">/WEB-INF/page/addperson.jsp</result> 
</action> </package> </struts>




888888
WEB.XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 瀵筍pring瀹瑰櫒杩涜瀹炰緥鍖?-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

  <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>


<url-pattern>/*</url-pattern>
</filter-mapping>
  <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>









[解决办法]
关键部分挂了
[解决办法]
贴异常 说问题
[解决办法]
看看这个: http://blog.csdn.net/cai5/article/details/6565523

问问题,也应该把问题说清楚啊,老大 ,异常信息没有 ,只看到光秃秃的源码 ,看都累死人的

热点排行