博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring4+SpringMVC+Hibernate4整合入门与实例
阅读量:2493 次
发布时间:2019-05-11

本文共 7098 字,大约阅读时间需要 23 分钟。

配置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"%>
个人中心欢迎你!!! ${user.username}

转载地址:http://ythrb.baihongyu.com/

你可能感兴趣的文章
CocoaPods安装和使用教程
查看>>
Beginning Auto Layout Tutorial
查看>>
block使用小结、在arc中使用block、如何防止循环引用
查看>>
iPhone开发学习笔记002——Xib设计UITableViewCell然后动态加载
查看>>
iOS开发中遇到的问题整理 (一)
查看>>
Swift code into Object-C 出现 ***-swift have not found this file 的问题
查看>>
为什么你的App介绍写得像一坨翔?
查看>>
RTImageAssets插件--@3x可自动生成@2x图片
查看>>
iOS开发的一些奇巧淫技
查看>>
常浏览的博客和网站
查看>>
Xcode 工程文件打开不出来, cannot be opened because the project file cannot be parsed.
查看>>
iOS在Xcode6中怎么创建OC category文件
查看>>
5、JavaWeb学习之基础篇—标签(自定义&JSTL)
查看>>
8、JavaWEB学习之基础篇—文件上传&下载
查看>>
reRender属性的使用
查看>>
href="javascript:void(0)"
查看>>
h:panelGrid、h:panelGroup标签学习
查看>>
f:facet标签 的用法
查看>>
<h:panelgroup>相当于span元素
查看>>
java中append()的方法
查看>>