bean对象主要代码如下所示:
public class Userinfo {
private int id;
private String name;
private String pass;
}
userinfo.hbm.xml如下所示:
<?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>
<class name="com.mth.bean.Userinfo" table="userinfo">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="name"></property>
<property name="pass"></property>
</class>
</hibernate-mapping>
dao层对象主要代码如下所示:
package com.mth.dao;
import java.io.Serializable;
import java.util.List;
import com.mth.bean.Userinfo;
public interface IUserDao {
public void saveUser(Userinfo user);
public void deleUser(Serializable id);
public void updateUser(Userinfo user);
public Userinfo getById(Serializable id);
public List<Userinfo> getAll();
}
daoimpl主要代码如下所示:
package com.mth.DaoImpl;
import java.io.Serializable;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.mth.bean.Userinfo;
import com.mth.dao.IUserDao;
public class IUserDaoImpl extends HibernateDaoSupport implements IUserDao {
public void deleUser(Serializable id) {
Userinfo user = this.getHibernateTemplate().get(Userinfo.class, id);
this.getHibernateTemplate().delete(user);
}
public List<Userinfo> getAll() {
List<Userinfo> list = this.getHibernateTemplate().find("from Userinfo");
return list;
}
public Userinfo getById(Serializable id) {
Userinfo user = this.getHibernateTemplate().get(Userinfo.class, id);
return user;
}
public void saveUser(Userinfo user) {
this.getHibernateTemplate().save(user);
}
public void updateUser(Userinfo user) {
this.getHibernateTemplate().update(user);
}
}
Service层主要代码如下所示:
package com.mth.service;
import java.util.List;
import com.mth.bean.Userinfo;
import com.mth.dao.IUserDao;
public class UserService {
private IUserDao dao;
public IUserDao getDao() {
return dao;
}
public void setDao(IUserDao dao) {
this.dao = dao;
}
public void deleUser(int id) {
dao.deleUser(id);
}
public List<Userinfo> getAll() {
List<Userinfo> list = dao.getAll();
return list;
}
public Userinfo getById(int id) {
Userinfo user = dao.getById(id);
return user;
}
public void saveUser(Userinfo user) {
dao.saveUser(user);
}
public void updateUser(Userinfo user) {
dao.updateUser(user);
}
}
Control层代码如下所示:
package com.mth.controler;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.mth.bean.Userinfo;
import com.mth.service.UserService;
@Controller
@RequestMapping("/userinfo")
public class MyControler {
private UserService userSer;
public UserService getUserSer() {
return userSer;
}
@Resource(name = "userSer")
public void setUserSer(UserService userSer) {
this.userSer = userSer;
}
@RequestMapping("/save")
public String saveUser(Userinfo user, HttpServletRequest req) {
userSer.saveUser(user);
req.setAttribute("req", user);
return "redirect:showAll.action";
}
@RequestMapping("/showAll")
public String getAll(HttpServletRequest req) {
List<Userinfo> list = userSer.getAll();
req.setAttribute("list", list);
return "showAll";
}
@RequestMapping("/getById")
public String getById(int id, HttpServletRequest req) {
Userinfo user = userSer.getById(id);
req.setAttribute("user", user);
return "showuser";
}
@RequestMapping("/update")
public String update(Userinfo user) {
System.out.println(user);
userSer.updateUser(user);
return "redirect:showAll.action";
}
@RequestMapping("/delete")
public String delete(int id) {
userSer.deleUser(id);
return "redirect:showAll.action";
}
}
web.xml配置文件中的主要配置代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!--
防止session提前关闭处理
将Hibernate的Session生命延长,为了解决由于Hibernate延迟加载,Session过早关闭问题
-->
<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>*.action</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
springmvc配置文件中的主要配置代码:
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl">
</property>
<property name="username" value="lee"></property>
<property name="password" value="lee"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/mth/bean/userinfo.hbm.xml</value>
</list>
</property>
</bean>
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com"></context:component-scan>
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 数据bean -->
<bean id="userDao" class="com.mth.DaoImpl.IUserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userSer" class="com.mth.service.UserService">
<property name="dao" ref="userDao"></property>
</bean>
<!-- 配置文件实现事物 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="tx" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="save*" />
<tx:method name="dele*" />
<tx:method name="update*" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.mvc.service.*.*(..))"
id="pt" />
<aop:advisor advice-ref="tx" pointcut-ref="pt" />
</aop:config>
</beans>
表单中的action或者超链接如下设置
<body>
<form action="userinfo/save.action" method="post">
name:
<input type="text" name="name">
<br />
pass:
<input type="text" name="pass">
<br />
<input type="submit" value="注册">
<br />
</form>
</body>
<!-- 获取list遍历 获得后台数据 超链接action如下配置 -->
<c:forEach items="${list}" var="user">
<tr>
<td>
${user.id }
</td>
<td>
${user.name }
</td>
<td>
${user.pass}
</td>
<td>
<a href="userinfo/getById.action?id=${user.id}">更新</a>
</td>
<td>
<a href="userinfo/delete.action?id=${user.id}">删除</a>
</td>
</tr>
</c:forEach>