一、首先通过maven加入spring、jpa和hibernate相关的jar包。如pom.xml:
Java代码  收藏代码
     
      4.0.0 
       cn.ibeans 
      ibeans 
      war 
      0.0.1-SNAPSHOT 
      myweb Maven Webapp 
      http://maven.apache.org 
                   
       
       
        1.6 
        4.11 
        3.2.2.RELEASE 
       
                   
       
       
         
         
            org.springframework 
            spring-core 
            ${spring-version} 
         
         
            org.springframework 
            spring-beans 
            ${spring-version} 
         
         
            org.springframework 
            spring-web 
            ${spring-version} 
         
         
            org.springframework 
            spring-webmvc 
            ${spring-version} 
         
         
            org.springframework 
            spring-context 
            ${spring-version} 
         
         
            org.springframework.data 
            spring-data-jpa 
            1.3.2.RELEASE 
         
         
                     
                     
         
         
            javax.servlet 
            servlet-api 
            3.0-alpha-1 
            provided 
         
         
                     
                     
         
         
            junit 
            junit 
            ${junit-version} 
            test 
         
         
                     
                     
         
         
            org.hibernate 
            hibernate-core 
            4.2.1.Final 
         
             
            org.hibernate 
            hibernate-entitymanager 
            4.2.1.Final 
         
         
            org.hibernate.common 
            hibernate-commons-annotations 
            4.0.1.Final 
         
         
            org.hibernate 
            hibernate-validator 
            5.0.0.Final 
         
         
                     
                     
         
         
            org.slf4j 
            slf4j-api 
            1.7.5 
         
         
            org.slf4j 
            jcl-over-slf4j 
            1.7.5 
         
         
            org.slf4j 
            slf4j-log4j12 
            1.7.5 
         
         
                     
                     
         
         
            mysql 
            mysql-connector-java 
            5.1.25 
         
         
                     
                     
         
         
            commons-dbcp 
            commons-dbcp 
            20030825.184428 
         
         
            commons-pool 
            commons-pool 
            20030825.183949 
         
         
            commons-collections 
            commons-collections 
            2.1 
         
         
       
                   
       
       
        ibeans 
       
     
2、配置applicationContext.xml中的实体管理工厂和事务机制:
Java代码  收藏代码
     
     
                 
        Spring公共配置  
                 
         
         
             
         
                 
         
          
             
             
             
             
                 
                    org.hibernate.cfg.ImprovedNamingStrategy 
                    true 
                    update 
                 
             
         
                     
         
         
             
         
                     
         
         
             
         
                     
         
         
         
         
                 
         
         
                     
         
         
               
                         
             
             
                 
                 
                 
                 
                 
                         
                 
                 
                 
                 
                 
                 
                 
             
                         
             
             
         
                     
         
         
               
                 
             
             
                 
                 
                 
                 
                 
             
         
                     
         
         
              
                         
             
             
                 
                 
                 
                 
             
                 
             
             
                 
                 
             
         
     
定义实体类:User.java
Java代码  收藏代码
    package cn.ibeans.web.model; 
                 
    import java.io.Serializable; 
                 
    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="i_user") 
    public class User implements Serializable{ 
        private String id; 
        private String username; 
        private String password; 
                     
        @Id 
        @GeneratedValue(generator="system-uuid") 
        @GenericGenerator(name="system-uuid",strategy="uuid") 
        public String getId() { 
            return id; 
        } 
        public void setId(String id) { 
            this.id = id; 
        } 
                     
        @Column(name="username") 
        public String getUsername() { 
            return username; 
        } 
        public void setUsername(String username) { 
            this.username = username; 
        } 
                     
        @Column(name="password") 
        public String getPassword() { 
            return password; 
        } 
        public void setPassword(String password) { 
            this.password = password; 
        } 
                     
    } 
3、定义dao接口,只需定义接口方法即可,不用加任何注解
Java代码  收藏代码
    package cn.ibeans.dao; 
                 
    import cn.ibeans.web.model.User; 
                 
    public interface UserDao { 
                 
        User findByName(String name); 
                 
        User findByLoginName(String loginName); 
                     
        void saveOrUpdate(User user); 
    } 
4、定义dao实现类。用@Repository注解类,并将EntityManager作为成员变量注入,注入方法是加@PersistenceContext
Java代码  收藏代码
    package cn.ibeans.dao.impl; 
                 
    import javax.persistence.EntityManager; 
    import javax.persistence.PersistenceContext; 
                 
    import org.springframework.stereotype.Repository; 
                 
    import cn.ibeans.dao.UserDao; 
    import cn.ibeans.web.model.User; 
                 
    @Repository 
    public class UserDaoImpl implements UserDao { 
                     
        @PersistenceContext 
        private EntityManager em; 
                 
        @Override 
        public User findByName(String name) { 
            return null; 
        } 
                 
        @Override 
        public User findByLoginName(String loginName) { 
            return null; 
        } 
                 
        @Override 
        public void saveOrUpdate(User user) { 
            em.persist(user); 
        } 
                 
    } 
5、定义service类。可直接写实现类
用@Component或@Service注释类,将dao接口作为成员变量注入,注入方法是加@Autowired
如果是写方法,需在方法上加@Transactional(readOnly=false)注解
Java代码  收藏代码
    package cn.ibeans.service; 
                 
    import javax.annotation.Resource; 
                 
    import org.apache.log4j.Logger; 
    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.stereotype.Component; 
    import org.springframework.transaction.annotation.Transactional; 
                 
    import cn.ibeans.dao.UserDao; 
    import cn.ibeans.web.model.User; 
                 
    @Component 
    public class UserService { 
        private static Logger logger = Logger.getLogger(UserService.class); 
        @Autowired 
        private UserDao userDao; 
                     
        @Transactional(readOnly=false) 
        public void saveUser(User user){ 
            userDao.saveOrUpdate(user); 
            logger.debug(user.getUsername()+",保存成功!"); 
        } 
    } 
6、定义action
用@Controller注释action类,如果需为该action中所有方法指定一个共用的上层路径名,则需用@RequestMapping(value="/user")注释该类。表示该类下的所有访求均要通过/user/**来访问
将service类作为成员变量注入action。注入方法是加@Autowired
用@RequestMapping(value="/save")注释action的方法。
Java代码  收藏代码
    package cn.ibeans.web.controller; 
                 
    import javax.servlet.ServletContext; 
    import javax.servlet.http.HttpServlet; 
                 
    import org.apache.log4j.Logger; 
    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.stereotype.Controller; 
    import org.springframework.ui.Model; 
    import org.springframework.web.bind.annotation.ModelAttribute; 
    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.context.ContextLoader; 
    import org.springframework.web.context.WebApplicationContext; 
                 
    import cn.ibeans.service.UserService; 
    import cn.ibeans.web.model.User; 
                 
    @Controller 
    @RequestMapping(value="/user") 
    public class LoginController extends HttpServlet{ 
        private static Logger log = Logger.getLogger(LoginController.class); 
                     
        @Autowired 
        private UserService userService; 
                     
                 
        //@RequestMapping(value="/login")   //如果类级别上已有映射地址(/user),此处的完整请求地址是/user/login 
        public String doLogin(Model model,Object loginForm){ 
            //model.addAttribute("username",username);  //model被用来封装数据,向前台传递 
            WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext(); 
            ServletContext sc = context.getServletContext(); 
                         
            return "/welcome"; 
        } 
                     
        @RequestMapping(value="/login") 
        public String login(@ModelAttribute User user,Model model){ 
            log.info(user.getUsername()); 
            model.addAttribute(user); 
            return "/welcome"; 
        } 
                     
        @RequestMapping(value="/save") 
        public String save(@ModelAttribute User user,Model model){ 
            log.info(user.getUsername()); 
            userService.saveUser(user); 
            model.addAttribute(user); 
            return "/welcome"; 
        } 
    } 
7、发起请示的页面
请示的url应该是上下文根路径+action类路径+方法路径
Java代码  收藏代码
    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> 
      
      
      
      
     Insert title here 
      
      
         
8、转发目的页面 可以通过引用action方法里定义的mode属性来获取相关的数据。 welcome.jsp: Java代码 收藏代码 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> Insert title here ${user.username },你好!




http://ll-feng.iteye.com/blog/1908511