posts - 93,  comments - 2,  trackbacks - 0
----------------------------------------------一.WEB项目的执行流程---------------------------------
1.web项目的运行流程大致如下:    
    启动tomcat,先到web.xml里面查看<context-param><listener><filter><servlet>等等几个tag,查看里面的配置,查找相应的配置文件。
如下列文件所示:
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>SpringMVC12</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>
 
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:config/springAnnotation-*.xml</param-value>
  </context-param>
 
 
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 
  <servlet>
   <servlet-name>springMVC</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath*:config/springAnnotation-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
 
  <filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
  <init-param>
   <param-name>forceEncoding</param-name>
   <param-value>true</param-value>
  </init-param>
 </filter>
 <!-- encoding filter for jsp page -->
 <filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 
  <servlet-mapping>
   <servlet-name>springMVC</servlet-name>
   <url-pattern>/</url-pattern>
  </servlet-mapping>
 
  <filter>
    <filter-name>openSession</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  </filter>
 
  <filter-mapping>
   <filter-name>openSession</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

首先查看那几个标签,找到对应的配置文件,如<context-param>中配置的classpath*:config/springAnnotation-*.xml。


---------------------------------------2.到配置文件查看相关信息----------------------------------------------
(1)<context-param>里的classpath*:config/springAnnotation-*.xml,即在config目录下匹配springAnnotation-*.xml的文件



(1.1)查看springAnnotation-core.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">
]>

<beans>
 
 <import resource="classpath*:com/zyh/web/controller/spring/springAnnotation-import.xml"/>
</beans>

对应的路径如下com/zyh/web/controller/spring/springAnnotation-import.xml
(1.2)查看springAnnotation-import.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">
]>

<beans>
 <bean id="userDao" class="com.zyh.web.controller.dao.UserDAO">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 
 <bean id="userManagerBase" class="com.zyh.web.controller.service.UserManager">
  <property name="userDao" ref="userDao"></property>
 </bean>
 
 <bean id="userManager" parent="transactionBese">
  <property name="target" ref="userManagerBase"></property>
 </bean>
 
</beans>

对应关于就已经配置在容器里面了。
(2.1)查看springAnnotation-hibernate.xml(配置数据源,sessionFactory,和事物)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">
]>

<beans>
 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql://localhost/test1?useUnicode=true"/>
  <property name="username" value="root"/>
  <property name="password" value="root"/>
 </bean>
 
 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hiberante.format_sql">true</prop>
   </props>
  </property>
  <property name="configLocations">
   <list>
    <value>
     classpath*:com/zyh/web/controller/hibernate/hibernate.cfg.test.xml
    </value>
   </list>
  </property>
 </bean>
 
 <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 
 <bean id="transactionBese" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">
  <property name="transactionManager" ref="transactionManager"></property>
  <property name="transactionAttributes">
   <props>
    <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="get*">PROPAGATION_NEVER</prop>
   </props>
  </property>
 </bean>
</beans>

 配置的对应的 classpath*:com/zyh/web/controller/hibernate/hibernate.cfg.test.xml
    

(3)springAnnotation-servlet.xml,内容如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans
 xmlns:context="http://www.springframework.org/schema/context
 xmlns:p="http://www.springframework.org/schema/p
 xmlns:mvc="http://www.springframework.org/schema/mvc
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
 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.xsd 
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
     <!-- 注解扫描包 -->
 <context:component-scan base-package="com.zyh.web.controller" />
 <!-- 开启注解 -->
 
 <mvc:annotation-driven/>
 
 <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> -->
 <!-- 静态资源访问 -->
  <mvc:resources location="/img/" mapping="/img/**"/> 
  <mvc:resources location="/js/" mapping="/js/**"/>  
 

 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/"></property>
  <property name="suffix" value=".jsp"></property>
 </bean>
 
 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="utf-8" />
       <property name="maxUploadSize" value="10485760000" />
       <property name="maxInMemorySize" value="40960" />
 </bean>
 </beans> 



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans
xmlns:context="http://www.springframework.org/schema/context
xmlns:p="http://www.springframework.org/schema/p
xmlns:mvc="http://www.springframework.org/schema/mvc
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
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.xsd 
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
     <!-- 注解扫描包 -->
<context:component-scan base-package="com.zyh.web.controller" />
<!-- 开启注解 -->

<mvc:annotation-driven/>

<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> -->
<!-- 静态资源访问 -->
  <mvc:resources location="/img/" mapping="/img/**"/> 
  <mvc:resources location="/js/" mapping="/js/**"/>  

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/"></property>
  <property name="suffix" value=".jsp"></property>
</bean>

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="utf-8" />
       <property name="maxUploadSize" value="10485760000" />
       <property name="maxInMemorySize" value="40960" />
</bean>
</beans> 


springAnnotation-hibernate.xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">
]>

<beans>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql://localhost/test1?useUnicode=true"/>
  <property name="username" value="root"/>
  <property name="password" value="root"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hiberante.format_sql">true</prop>
   </props>
  </property>
  <property name="configLocations">
   <list>
    <value>
     classpath*:com/zyh/web/controller/hibernate/hibernate.cfg.test.xml
    </value>
   </list>
  </property>
</bean>

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

<bean id="transactionBese" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">
  <property name="transactionManager" ref="transactionManager"></property>
  <property name="transactionAttributes">
   <props>
    <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="get*">PROPAGATION_NEVER</prop>
   </props>
  </property>
</bean>
</beans>

这个写的有点乱,理清思路,其实很简单,就是配置spring容器,里面扫描了所有的bean及一些依赖关系,hibernate配置数据源建立数据库连接,SessionFactory用于管理数据库,还有就是Springmvc用于控制视图与后台之间的页面转向,传递数据,发送请求。
配置工作就到此结束,然后就是写bean,dao,service,control与联系配置文件的对应关系,实际开发中当然不会把配置文件都写好才来写java代码。我的做法是同时进行,漏了就相应的加上。

-----------------------------------------------------------3对应的实现--------------------------------------
3.1 为了简单,我们建立一个单表的添删改查,就一个对象吧User,对应的表T_User,里面有userName和age两个属性,当然还要建立一个主键id,如下列代码所示:
com.zyh.web.controller.entity.User

package com.zyh.web.controller.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="T_USER")
public class User {

 @GeneratedValue(generator = "system-uuid") 
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(length=32)
 @Id
 private String id;
 
 @Column(length=32)
 private String userName;
 
 @Column(length=32)
 private String age;
 
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getUserName() {
  return userName;
 }
 public void setUserName(String userName) {
  this.userName = userName;
 }
 public String getAge() {
  return age;
 }
 public void setAge(String age) {
  this.age = age;
 }

}
3.2 dao层结构如下:接口IUserDAO与实现类UserDAO,我们实现添删改查,查询包括一个User和所以User
com.zyh.web.controller.dao.IUserDAO

package com.zyh.web.controller.dao;

import java.util.List;

import com.zyh.web.controller.entity.User;

public interface IUserDAO {

 public void addUser(User user);
 
 public List<User> getAllUser();
 
 public User getUser(String id);
 
 public boolean delUser(String id);
 
 public boolean updateUser(User user);
}
实现类UserDAO:

package com.zyh.web.controller.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.zyh.web.controller.entity.User;

public class UserDAO implements IUserDAO {

 private SessionFactory sessionFactory;
 public void setSessionFactory(SessionFactory sessionFactory) {
  this.sessionFactory = sessionFactory;
 }

 @Override
 public void addUser(User user) {
  // TODO Auto-generated method stub
  sessionFactory.getCurrentSession().save(user);
 }

 @Override
 public List<User> getAllUser() {
  // TODO Auto-generated method stub
  String hql = "from User";
  Query query =  sessionFactory.getCurrentSession().createQuery(hql);
  return query.list();
 }

 @Override
 public User getUser(String id) {
  // TODO Auto-generated method stub
  String hql = "from User u where u.id=?";
  Query query = sessionFactory.getCurrentSession().createQuery(hql);
  query.setString(0, id);
  return (User) query.uniqueResult();
 }

 @Override
 public boolean delUser(String id) {
  // TODO Auto-generated method stub
  String hql = "delete User u where u.id=?";
  Query query = sessionFactory.getCurrentSession().createQuery(hql);
  query.setString(0, id);
  return (query.executeUpdate()>0);
 }

 @Override
 public boolean updateUser(User user) {
  // TODO Auto-generated method stub
  String hql = "update User u set u.userName=?,u.age=? where u.id=?";
  Query query = sessionFactory.getCurrentSession().createQuery(hql);
  query.setString(0, user.getUserName());
  query.setString(1, user.getAge());
  query.setString(2, user.getId());
  return (query.executeUpdate()>0);
 }

}

3.3 service层结构如下:接口IUserManager与实现类UserManager,我们实现添删改查,查询包括一个User和所以User
com.zyh.web.controller.service.IUserManager

package com.zyh.web.controller.service;

import java.util.List;

import com.zyh.web.controller.entity.User;

public interface IUserManager {
 
 public void addUser(User user);
 
 public List<User> getAllUser();
 
 public boolean delUser(String id);
 
 public User getUser(String id);
 
 public boolean updateUser(User user);
}
实现类UserManager.java

package com.zyh.web.controller.service;

import java.util.List;

import com.zyh.web.controller.dao.IUserDAO;
import com.zyh.web.controller.entity.User;

public class UserManager implements IUserManager {
 
 private IUserDAO userDao;
 
 public void setUserDao(IUserDAO userDao) {
  this.userDao = userDao;
 }

 @Override
 public void addUser(User user) {
  // TODO Auto-generated method stub
  userDao.addUser(user);
 }

 @Override
 public List<User> getAllUser() {
  // TODO Auto-generated method stub
  return userDao.getAllUser();
 }

 @Override
 public boolean delUser(String id) {
  // TODO Auto-generated method stub
  return userDao.delUser(id);
 }

 @Override
 public User getUser(String id) {
  // TODO Auto-generated method stub
  return userDao.getUser(id);
 }

 @Override
 public boolean updateUser(User user) {
  // TODO Auto-generated method stub
  return userDao.updateUser(user);
 }

}

4.Control层结构如下:实现类UserController.java,代码如下:

package com.zyh.web.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zyh.web.controller.entity.User;
import com.zyh.web.controller.service.IUserManager;

@Controller
@RequestMapping("/user")
public class UserController {
 @Resource(name="userManager")
 private IUserManager userManager;
 
 @RequestMapping("/toAddUser")
 public String toAddUser(){
  return "/addUser";
 }
 
 @RequestMapping("/addUser")
 public String addUser(User user){
  userManager.addUser(user);
  return "redirect:/user/getAllUser";
 }
 
 @RequestMapping("/getAllUser")
 public String getAllUser(HttpServletRequest request){
  List<User> user = userManager.getAllUser();
  request.setAttribute("user", user);
  return "/userManager";
 }
 
 @RequestMapping("/delUser")
 public void delUser(String id,HttpServletResponse response){
  String result = "{\"result\":\"error\"}";
  if(userManager.delUser(id)){
   result = "{\"result\":\"success\"}";
  }
  response.setContentType("application/json");
  PrintWriter pw;
  try {
   pw = response.getWriter();
   pw.write(result);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 @RequestMapping("/getUser")
 public String getUser(String id,HttpServletRequest request){
  User user = userManager.getUser(id);
  request.setAttribute("user", user);
  return "editUser";
 }
 
 @RequestMapping("/updateUser")
 public String updateUser(User user,HttpServletRequest request){
  if(userManager.updateUser(user)){
   userManager.getUser(user.getId());
   request.setAttribute("user", user);
   return "/editUser";
  }else{
   return "/error";
  }
 }
}

--

 
--

package com.zyh.web.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zyh.web.controller.entity.User;
import com.zyh.web.controller.service.IUserManager;

@Controller
@RequestMapping("/user")
public class UserController {
 @Resource(name="userManager")
 private IUserManager userManager;
 
 @RequestMapping("/toAddUser")
 public String toAddUser(){
  return "/addUser";
 }
 
 @RequestMapping("/addUser")
 public String addUser(User user){
  userManager.addUser(user);
  return "redirect:/user/getAllUser";
 }
 
 @RequestMapping("/getAllUser")
 public String getAllUser(HttpServletRequest request){
  List<User> user = userManager.getAllUser();
  request.setAttribute("user", user);
  return "/userManager";
 }
 
 @RequestMapping("/delUser")
 public void delUser(String id,HttpServletResponse response){
  String result = "{\"result\":\"error\"}";
  if(userManager.delUser(id)){
   result = "{\"result\":\"success\"}";
  }
  response.setContentType("application/json");
  PrintWriter pw;
  try {
   pw = response.getWriter();
   pw.write(result);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 @RequestMapping("/getUser")
 public String getUser(String id,HttpServletRequest request){
  User user = userManager.getUser(id);
  request.setAttribute("user", user);
  return "editUser";
 }
 
 @RequestMapping("/updateUser")
 public String updateUser(User user,HttpServletRequest request){
  if(userManager.updateUser(user)){
   userManager.getUser(user.getId());
   request.setAttribute("user", user);
   return "/editUser";
  }else{
   return "/error";
  }
 }
}

 

 

 

 



 

package com.zyh.web.controller.service;

import java.util.List;

import com.zyh.web.controller.dao.IUserDAO;
import com.zyh.web.controller.entity.User;

public class UserManager implements IUserManager {
 
 private IUserDAO userDao;
 
 public void setUserDao(IUserDAO userDao) {
  this.userDao = userDao;
 }

 @Override
 public void addUser(User user) {
  // TODO Auto-generated method stub
  userDao.addUser(user);
 }

 @Override
 public List<User> getAllUser() {
  // TODO Auto-generated method stub
  return userDao.getAllUser();
 }

 @Override
 public boolean delUser(String id) {
  // TODO Auto-generated method stub
  return userDao.delUser(id);
 }

 @Override
 public User getUser(String id) {
  // TODO Auto-generated method stub
  return userDao.getUser(id);
 }

 @Override
 public boolean updateUser(User user) {
  // TODO Auto-generated method stub
  return userDao.updateUser(user);
 }

}

--

package com.zyh.web.controller.service;

import java.util.List;

import com.zyh.web.controller.entity.User;

public interface IUserManager {
 
 public void addUser(User user);
 
 public List<User> getAllUser();
 
 public boolean delUser(String id);
 
 public User getUser(String id);
 
 public boolean updateUser(User user);
}

 

--
3.2 dao层结构如下:接口IUserDAO与实现类UserDAO,我们实现添删改查,查询包括一个User和所以User
com.zyh.web.controller.dao.IUserDAO
--

package com.zyh.web.controller.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.zyh.web.controller.entity.User;

public class UserDAO implements IUserDAO {

 private SessionFactory sessionFactory;
 public void setSessionFactory(SessionFactory sessionFactory) {
  this.sessionFactory = sessionFactory;
 }

 @Override
 public void addUser(User user) {
  // TODO Auto-generated method stub
  sessionFactory.getCurrentSession().save(user);
 }

 @Override
 public List<User> getAllUser() {
  // TODO Auto-generated method stub
  String hql = "from User";
  Query query =  sessionFactory.getCurrentSession().createQuery(hql);
  return query.list();
 }

 @Override
 public User getUser(String id) {
  // TODO Auto-generated method stub
  String hql = "from User u where u.id=?";
  Query query = sessionFactory.getCurrentSession().createQuery(hql);
  query.setString(0, id);
  return (User) query.uniqueResult();
 }

 @Override
 public boolean delUser(String id) {
  // TODO Auto-generated method stub
  String hql = "delete User u where u.id=?";
  Query query = sessionFactory.getCurrentSession().createQuery(hql);
  query.setString(0, id);
  return (query.executeUpdate()>0);
 }

 @Override
 public boolean updateUser(User user) {
  // TODO Auto-generated method stub
  String hql = "update User u set u.userName=?,u.age=? where u.id=?";
  Query query = sessionFactory.getCurrentSession().createQuery(hql);
  query.setString(0, user.getUserName());
  query.setString(1, user.getAge());
  query.setString(2, user.getId());
  return (query.executeUpdate()>0);
 }

}

 

 

 




 

package com.zyh.web.controller.dao;

import java.util.List;

import com.zyh.web.controller.entity.User;

public interface IUserDAO {

 public void addUser(User user);
 
 public List<User> getAllUser();
 
 public User getUser(String id);
 
 public boolean delUser(String id);
 
 public boolean updateUser(User user);
}

--

package com.zyh.web.controller.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="T_USER")
public class User {

 @GeneratedValue(generator = "system-uuid") 
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(length=32)
 @Id
 private String id;
 
 @Column(length=32)
 private String userName;
 
 @Column(length=32)
 private String age;
 
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getUserName() {
  return userName;
 }
 public void setUserName(String userName) {
  this.userName = userName;
 }
 public String getAge() {
  return age;
 }
 public void setAge(String age) {
  this.age = age;
 }

}

--


 

 

 

 

 

 


 

posted on 2013-06-01 10:49 Terry Zou 阅读(4145) 评论(0)  编辑  收藏 所属分类: Tomcat+Eclipse

只有注册用户登录后才能发表评论。


网站导航:
 
<2013年6月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

Java

搜索

  •  

最新随笔

最新评论

阅读排行榜

评论排行榜