本文介绍使用eclipse+maven搭建Spring+SpringMvc+Hibernate项目,以登陆为例:
1、创建maven项目
2、把maven项目变为动态网站
3、搭建spring+springmvc+Hibernate项目
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.welljointgroupId>
<artifactId>testSSH2artifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<spring.version>4.2.6.RELEASEspring.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-ormartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-coreartifactId>
<version>3.6.9.Finalversion>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-entitymanagerartifactId>
<version>3.6.9.Finalversion>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.6version>
dependency>
<dependency>
<groupId>javax.servlet.jsp.jstlgroupId>
<artifactId>jstl-apiartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
dependency>
<dependency>
<groupId>commons-dbcpgroupId>
<artifactId>commons-dbcpartifactId>
<version>1.4version>
dependency>
<dependency>
<groupId>commons-poolgroupId>
<artifactId>commons-poolartifactId>
<version>1.6version>
dependency>
dependencies>
project>
package com.welljoint.entity;
/**
* @version
* @time 2018-2-28 下午6:02:03
* @describe:用户的bean
*/
public class User {
private Integer id;
private String userName;
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;
}
@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", password="
+ password + "]";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆title>
head>
<body>
<form method="post">
用户名:<input type="text" name="userName"><br>
密码:<input type="text" name="password"><br>
<input type="submit" value="登陆">
form>
body>
html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆成功title>
head>
<body>
登陆成功
body>
html>
package com.welljoint.rest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.welljoint.entity.User;
/**
* @version
* @time 2018-2-28 下午5:57:07
* @describe:控制器
*/
@Controller //注解为控制器
@RequestMapping(value="/login") //截获带有/login的请求
public class LoginController {
@RequestMapping(method=RequestMethod.GET)
public String get(){ //用来返回一个页面
return "login"; //返回指向login.jsp页面
}
@RequestMapping(method=RequestMethod.POST)
public String post(User user){ //用来处理用户的登陆请求
return "login_success";
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.welljoint" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
bean>
<mvc:resources location="/res/" mapping="/res/**"/>
beans>
<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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>testSSH2display-name>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-mvc.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
filter>
<filter-mapping>
<filter-name>CharacterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jspurl-pattern>
<trim-directive-whitespaces>truetrim-directive-whitespaces>
jsp-property-group>
jsp-config>
web-app>
package com.welljoint.service;
public interface LoginService {
public int login(String userName,String password);
}
package com.welljoint.service;
import org.springframework.stereotype.Service;
@Service
public class LoginServiceImpl implements LoginService{
public int login(String userName,String password){
return 1;
}
}
package com.welljoint.rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.welljoint.entity.User;
import com.welljoint.service.LoginService;
/**
* @version
* @time 2018-2-28 下午5:57:07
* @describe:控制器
*/
@Controller //注解为控制器
@RequestMapping(value="/login") //截获带有/login的请求
public class LoginController {
@Autowired
LoginService loginService; //注入service层
@RequestMapping(method=RequestMethod.GET)
public String get(){ //用来返回一个页面
return "login"; //返回指向login.jsp页面
}
@RequestMapping(method=RequestMethod.POST)
public String post(User user){ //用来处理用户的登陆请求
if (loginService.login(user.getUserName(), user.getPassword())==1) {
return "login_success"; //登陆成功,跳转到login_success.jsp页面
}
return "login";
}
}
package com.welljoint.dao;
import com.welljoint.entity.User;
public interface UserDao {
public User find(String userName,String password);
}
package com.welljoint.dao;
import org.springframework.stereotype.Repository;
import com.welljoint.entity.User;
@Repository
public class UserDaompl implements UserDao{
public User find(String userName,String password){
return new User();//假数据,后期要通过Hibernate去数据库取
}
}
package com.welljoint.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.welljoint.dao.UserDao;
@Service
public class LoginServiceImpl implements LoginService{
@Autowired
private UserDao userDao;
public int login(String userName,String password){
return userDao.find(userName, password)==null?0:1;
}
}
对于这一节,是难点,<敲黑板>,3.9节前做的是前奏,都没有涉及到数据库操作,关于3.10节,大家可以用Hibernate也可以用Mybatis,这一章我们讲Hibernate,我的另一篇文章讲Mybatis,也欢迎大家看。
<hibernate-mapping package="com.welljoint.entity">
<class name="User" table="user">
<id name="id">
<generator class="native"/>
id>
<property name="userName" length="32" not-null="true" unique="true"/>
<property name="password" length="64" not-null="true"/>
class>
hibernate-mapping>
<beans default-autowire="byName"
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="com.welljoint" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/ssh?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root" />
bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>
<prop key="hibernate.format_sql">trueprop>
<prop key="hibernate.hbm2ddl.auto">updateprop>
props>
property>
<property name="mappingLocations" value="classpath:/com/welljoint/entity/*.hbm.xml" />
bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
bean>
<tx:annotation-driven transaction-manager="transactionManager" />
beans>
<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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>testSSH2display-name>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-mvc.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
filter>
<filter-mapping>
<filter-name>CharacterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jspurl-pattern>
<trim-directive-whitespaces>truetrim-directive-whitespaces>
jsp-property-group>
jsp-config>
web-app>
package com.welljoint.dao;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.welljoint.entity.User;
@Repository
public class UserDaompl extends HibernateDaoSupport implements UserDao{
//HibernateDaoSupport来操作数据库更加方便
//用来注入sessionFactory(不注入会报错)
@Resource(name = "sessionFactory")
public void setSessionFactoryOverride(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
@Override
public User find(String userName,String password){
//注意:以下是HQL语句
List> users = getHibernateTemplate().find("from User where userName=? and password=?",userName,password);
return users.size()>0?(User)users.get(0):null;//假数据,后期要通过Hibernate去数据库取
}
}
http://download.csdn.net/download/tiandixuanwuliang/10264025
本文原地址:http://mp.blog.csdn.net/mdeditor/79403783