Spring4+SpringMVC+Hibernate4整合入门与实例

2015-08-12 19:27:19来源:作者:人点击

配置web.xml

    myweb            /WEB-INF/jsp/register.jsp        /WEB-INF/jsp/login.jsp                    contextConfigLocation        classpath*:config/spring-*.xml                    org.springframework.web.context.ContextLoaderListener                    encodingFilter        org.springframework.web.filter.CharacterEncodingFilter                    encoding            UTF-8                            forceEncoding            true                        encodingFilter        /                    openSession        org.springframework.orm.hibernate4.support.OpenSessionInViewFilter                    singleSession            true                            flushMode            AUTO                        openSession        /                    myweb        myweb        springMVC        org.springframework.web.servlet.DispatcherServlet                    contextConfigLocation            classpath*:config/spring-mvc.xml                1                springMVC        /     

SpringMVC中Bean的配置spring-mvc.xml

                      

Hibernate的配置hibernate.cfg.xml

                        org.hibernate.dialect.MySQLDialect                            com.mysql.jdbc.Driver                            jdbc:mysql://localhost/myweb                            root                            jiangyu                false                update        true        true        50        30        org.hibernate.connection.C3P0ConnectionProvider        30        2        30000        120        180        3        50        1        0                            

实体层

package com.myweb.entity;import java.io.Serializable;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name = w_user)public class User implements Serializable {    private static final long serialVersionUID = 1L;    @Id    @Column(name = user_id)    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Integer id;    @Column(length = 50)    private String username;    @Column(length = 20)    private String password;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    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;    }}

数据访问层(DAO层)

接口:

package com.myweb.dao;import com.myweb.entity.User;public interface UserDao {    User save(User user);}

实现:

package com.myweb.dao.imp;import javax.inject.Inject;import org.springframework.orm.hibernate4.HibernateTemplate;import org.springframework.stereotype.Repository;import com.myweb.dao.UserDao;import com.myweb.entity.User;@Repositorypublic class UserDaoImp implements UserDao {    @Inject    private HibernateTemplate template;    @Override    public User save(User user) {        // TODO Auto-generated method stub        template.save(user);        return template.load(User.class, user.getId());    }}

服务层(Service层)

接口:

package com.myweb.service;import com.myweb.entity.User;public interface UserService {    User save(User user);}

实现:

package com.myweb.service.imp;import javax.inject.Inject;import org.springframework.stereotype.Service;import com.myweb.dao.UserDao;import com.myweb.entity.User;import com.myweb.service.UserService;@Servicepublic class UserServiceImp implements UserService {    @Inject    private UserDao userDao;    @Override    public User save(User user) {        // TODO Auto-generated method stub        return userDao.save(user);    }}

控制层(Controller层)

package com.myweb.controller;import javax.inject.Inject;import org.apache.shiro.SecurityUtils;import org.apache.shiro.authc.AuthenticationException;import org.apache.shiro.authc.UsernamePasswordToken;import org.apache.shiro.config.IniSecurityManagerFactory;import org.apache.shiro.subject.Subject;import org.apache.shiro.util.Factory;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.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import com.myweb.entity.User;import com.myweb.service.UserService;@Controller@RequestMapping(/user)public class UserController {    @Inject    private UserService userService;    @RequestMapping(value = /register.do, method = RequestMethod.POST)    public String register(User user) {        userService.save(user);        return login;    }    @RequestMapping(value = /login.do, method = RequestMethod.POST)    public String login(@ModelAttribute(user) User user) {        return self;    }}

视图成(View层)

register.jsp

<%@ page language=java contentType=text/html; charset=UTF-8 pageEncoding=UTF-8%>

login.jsp

<%@ page contentType=text/html;charset=UTF-8 language=java%>

self.jsp

<%@ page language=java contentType=text/html; charset=UTF-8    pageEncoding=UTF-8%> 

微信扫一扫

第七城市微信公众平台