Spring mvc+hibernate工程搭建

来源:互联网 发布:全途打单软件怎么样 编辑:程序博客网 时间:2024/05/24 05:21

大家好,Spring3 MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来越多的团队选择了Spring3 MVC了。Spring3 MVC结构简单,应了那句话简单就是美,而且他强大不失灵活,性能也很优秀。

hibernate在数据持久化的成就也是屈指可数的。下面将描述一下怎么配置这个spring mvc工程。


先来看看整个工程目录先


可以看出整个工程结构都非常清晰。下面一步步走

工程所需要的文件在附件中。


1.启动myeclipse,新建一个Web Project。选择java ee 6.0。工程名为Extbook,如下图


2.配置web.xml文件,web.xml是java web 工程的描述文件,用来配置项目属性。

现在我们打开web.xml输入以下内容

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!-- 使用ContextLoaderListener配置时,需要告诉它Spring配置文件的位置 --><!-- 如果没有指定,上下文载入器会在/WEB-INF/applicationContext.xml中找Spring配置文件 --><!-- 我们可以通过在Servlet上下文中设置contextConfigLocation参数,来为上下文载入器指定一个或多个Spring配置文件 --><!-- 注意:contextConfigLocation参数是一个用逗号分隔的路径列表,其路径是相对于Web系统的根路径的 --><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/ExtBook-servlet.xml, classpath*:/applicationContext*.xml</param-value></context-param><!-- SpringMVC的前端控制器 --><!-- 当DispatcherServlet载入后,它将从一个XML文件中载入Spring的应用上下文,该XML文件的名字取决于<servlet-name> --><!-- 这里DispatcherServlet将试图从一个叫做springmvc-servlet.xml的文件中载入应用上下文,其默认位于WEB-INF目录下 --><!-- 所以ContextLoaderListener参数值也可写成<param-value>classpath:applicationContext-*.xml</param-value> --><servlet><servlet-name>ExtBook</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>ExtBook</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- 配置上下文载入器 --><!-- 上下文载入器载入除DispatcherServlet载入的配置文件之外的其它上下文配置文件 --><!-- 最常用的上下文载入器是一个Servlet监听器,其名称为ContextLoaderListener --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><filter><filter-name>hibernateFilter</filter-name><filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class><init-param><param-name>singleSession</param-name><param-value>true</param-value></init-param></filter><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>

这里主要配置了一下工程其他配置文件位置,注册spring mvc的前端控制器,配置上下文控制器,设置hibernate的工作模式


3.配置ExtBook-servlet.xml,这个是spring mvc的关键配置,内容如下

<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"><context:annotation-config /><!-- 把标记了@Controller注解的类转换为bean --><context:component-scan base-package="org.extbook.controller" /><mvc:annotation-driven><mvc:message-converters><bean class="org.extbook.util.jackson.MappingJackson2HttpMessageConverter" /></mvc:message-converters></mvc:annotation-driven><mvc:default-servlet-handler /><!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /><!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /><bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"p:defaultEncoding="utf-8" /></beans>

这里主要配置了Controller的位置和Pojo的位置以及view层以及jackson转化器的相关属性。

4.在src目录创建applicationContext.xml文件,并输入以下内容

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"><context:annotation-config /><context:component-scan base-package="org.extbook" />  <!-- 自动扫描所有注解该路径 --><context:property-placeholder location="classpath:/hibernate.properties" /><!-- Hibernate 参数配置 --><bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- 自动扫描指定Package中的@Entity Class --><property name="packagesToScan" value="org.extbook.pojo" /><property name="hibernateProperties"><props><prop key="hibernate.dialect">${dataSource.dialect}</prop><prop key="hibernate.generate_statistics">true</prop><prop key="hibernate.connection.release_mode">auto</prop><prop key="hibernate.hbm2ddl.auto">${dataSource.hbm2ddl.auto}</prop><prop key="hibernate.show_sql">${dataSource.show_sql}</prop></props></property></bean><bean id="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /><property name="dataSource" ref="dataSource" /></bean><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="${dataSource.driverClassName}" /><property name="url" value="${dataSource.url}" /><property name="username" value="${dataSource.username}" /><property name="password" value="${dataSource.password}" /></bean><!-- 定义AOP拦截器 --><aop:config proxy-target-class="true"><aop:advisor pointcut="execution(* org.extbook..*Service.*(..))"advice-ref="txAdvice" /><aop:advisor pointcut="execution(* org.extbook..*DAO.*(..))"advice-ref="txAdvice" /></aop:config><!-- 基本事务定义,使用transactionManager作事务管理,默认get*方法的事务为readonly,其余方法按默认设置 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="get*" read-only="true" /><tx:method name="load*" read-only="true" /><tx:method name="find*" read-only="true" /><tx:method name="search*" read-only="true" /><tx:method name="*" /></tx:attributes></tx:advice></beans>
这里是一些spring的配置,里面有注释
5.同样新建applicationContext-view.xml,内容如下

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd        http://www.springframework.org/schema/tx          http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- 【配置视图解析器】 --><!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 --><!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 --><bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean></beans>  

这个是视图解析器

6.同样新建属性文件hibernate.properties设置数据库连接,内容如下

dataSource.password=123456dataSource.username=rootdataSource.databaseName=extbookdataSource.driverClassName=com.mysql.jdbc.DriverdataSource.dialect=org.hibernate.dialect.MySQL5DialectdataSource.serverName=localhost:3306dataSource.url=jdbc:mysql://localhost:3306/extbookdataSource.properties=user=${dataSource.username};databaseName=${dataSource.databaseName};serverName=${dataSource.serverName};password=${dataSource.password}dataSource.hbm2ddl.auto=updatedataSource.show_sql=true

这个需要根据你的数据库来更改,特别是密码。我的数据库是mysql

7.属性文件log4j.properties,配置log4j日志工作模式

log4j.rootLogger=DEBUG, A1log4j.appender.A1.layout=org.apache.log4j.PatternLayoutlog4j.appender.A1.layout.ConversionPattern=>>> %d %5p [%t] (%F:%L) - %m%nlog4j.appender.A1.DatePattern='.'yyyy-MM-ddlog4j.appender.A1=org.apache.log4j.ConsoleAppender

8.新建如下图所示的包和类


内容如下,实体类News.java

package org.extbook.pojo;import java.io.Serializable;import java.util.Date;import javax.persistence.Basic;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 = "News")public class News implements Serializable {private static final long serialVersionUID = 1L;@Id@Basic(optional = false)@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(nullable = false)private Integer id;@Column(length = 100)private String name;@Column(length = 10000)private String content;@Column(length = 1000)private String remark;@Columnprivate Date createTime;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public String getRemark() {return remark;}public void setRemark(String remark) {this.remark = remark;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}}

数据库操作EntityDao.java以及其实现文件EntityDaoImpl.java

package org.extbook.dao;import java.util.List;public interface EntityDao {public List<Object> createQuery(final String queryString);public Object save(final Object model);public void update(final Object model);public void delete(final Object model);}

package org.extbook.dao;import java.util.List;import org.hibernate.SessionFactory;import org.springframework.stereotype.Repository;@Repositorypublic class EntityDaoImpl implements EntityDao {private SessionFactory sessionFactory;public EntityDaoImpl(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}public EntityDaoImpl() {}public SessionFactory getSessionFactory() {return sessionFactory;}public void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}@Overridepublic List<Object> createQuery(String queryString) {// TODO Auto-generated method stubreturn null;}@Overridepublic Object save(Object model) {getSessionFactory().getCurrentSession().save(model);return null;}@Overridepublic void update(Object model) {// TODO Auto-generated method stub}@Overridepublic void delete(Object model) {// TODO Auto-generated method stub}}

业务逻辑类NewsService.java

package org.extbook.service;import java.util.List;import javax.annotation.Resource;import org.extbook.dao.EntityDao;import org.extbook.dao.EntityDaoImpl;import org.extbook.pojo.News;import org.hibernate.SessionFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;@Servicepublic class NewsService {@Resourceprivate EntityDao entityDao;@Autowiredpublic void setSessionFactory(SessionFactory sessionFactory) {entityDao = new EntityDaoImpl(sessionFactory);}@Transactionalpublic List<Object> getStudentList(){StringBuffer sff = new StringBuffer();sff.append("select a from ").append(News.class.getSimpleName()).append(" a ");List<Object> list = entityDao.createQuery(sff.toString());return list;}public void save(News st){entityDao.save(st);}public void delete(Object obj){entityDao.delete(obj);}}

控制器NewsController.java

package org.extbook.controller;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.extbook.pojo.News;import org.extbook.service.NewsService;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;@Controller@RequestMapping(value="/NewsController") public class NewsController {protected final transient Log log = LogFactory.getLog(NewsController.class);@Resourceprivate NewsService studentService;@RequestMapping(value="/load")@ResponseBodypublic Object load() {News news = new News();news.setName("aa");studentService.save(news);Map<String, String> map = new HashMap<String, String>();map.put("kk", "vv");return map;}       public String newslist(){           System.out.println("userlist() is invoked ------------");           return "newslist";       }        @RequestMapping(value="/newslist")    protected ModelAndView newslist(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {List<News> newsList = new ArrayList<News>();News news1 = new News();news1.setName("新闻1");news1.setContent("新闻内容1");news1.setCreateTime(new Date());News news2 = new News();news2.setName("新闻2");news2.setContent("新闻内容2");news2.setCreateTime(new Date());newsList.add(news1);newsList.add(news2);return new ModelAndView("newslist", "newsList", newsList);}}

还有一个jackson工具

MappingJackson2HttpMessageConverter.java

/* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.extbook.util.jackson;import java.io.IOException;import java.nio.charset.Charset;import java.text.SimpleDateFormat;import java.util.List;import org.springframework.http.HttpInputMessage;import org.springframework.http.HttpOutputMessage;import org.springframework.http.MediaType;import org.springframework.http.converter.AbstractHttpMessageConverter;import org.springframework.http.converter.HttpMessageNotReadableException;import org.springframework.http.converter.HttpMessageNotWritableException;import org.springframework.util.Assert;import com.fasterxml.jackson.annotation.JsonInclude;import com.fasterxml.jackson.core.JsonEncoding;import com.fasterxml.jackson.core.JsonGenerator;import com.fasterxml.jackson.databind.JavaType;import com.fasterxml.jackson.databind.ObjectMapper;/** * Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter} that can read and write JSON using <a href="http://jackson.codehaus.org/">Jackson 2's</a> * {@link ObjectMapper}. * <p> * This converter can be used to bind to typed beans, or untyped {@link java.util.HashMap HashMap} instances. * <p> * By default, this converter supports {@code application/json}. This can be overridden by setting the {@link #setSupportedMediaTypes(List) supportedMediaTypes} property. *  * @author Arjen Poutsma * @author Keith Donald * @since 3.1.2 * @see org.springframework.web.servlet.view.json.MappingJackson2JsonView */public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConverter<Object> {public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");private ObjectMapper objectMapper = new ObjectMapper();private boolean prefixJson = false;/** * Construct a new {@code BindingJacksonHttpMessageConverter}. */public MappingJackson2HttpMessageConverter() {// super(new MediaType("application", "json", DEFAULT_CHARSET));super(new MediaType("text", "html", DEFAULT_CHARSET)); // 解决上传文件时页面错误// 全局JSON配置objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);}/** * Set the {@code ObjectMapper} for this view. If not set, a default {@link ObjectMapper#ObjectMapper() ObjectMapper} is used. * <p> * Setting a custom-configured {@code ObjectMapper} is one way to take further control of the JSON serialization process. For example, an extended * {@link org.codehaus.jackson.map.SerializerFactory} can be configured that provides custom serializers for specific types. The other option for refining the serialization process is to use * Jackson's provided annotations on the types to be serialized, in which case a custom-configured ObjectMapper is unnecessary. *  * @param objectMapper */@SuppressWarnings("javadoc")public void setObjectMapper(ObjectMapper objectMapper) {Assert.notNull(objectMapper, "ObjectMapper must not be null");this.objectMapper = objectMapper;}/** * Return the underlying {@code ObjectMapper} for this view. *  * @return ObjectMapper */public ObjectMapper getObjectMapper() {return this.objectMapper;}/** * Indicate whether the JSON output by this view should be prefixed with "{} &&". Default is false. * <p> * Prefixing the JSON string in this manner is used to help prevent JSON Hijacking. The prefix renders the string syntactically invalid as a script so that it cannot be hijacked. This prefix does * not affect the evaluation of JSON, but if JSON validation is performed on the string, the prefix would need to be ignored. *  * @param prefixJson */public void setPrefixJson(boolean prefixJson) {this.prefixJson = prefixJson;}@Overridepublic boolean canRead(Class<?> clazz, MediaType mediaType) {JavaType javaType = getJavaType(clazz);return (this.objectMapper.canDeserialize(javaType) && canRead(mediaType));}@Overridepublic boolean canWrite(Class<?> clazz, MediaType mediaType) {return (this.objectMapper.canSerialize(clazz) && canWrite(mediaType));}@Overrideprotected boolean supports(Class<?> clazz) {// should not be called, since we override canRead/Write insteadthrow new UnsupportedOperationException();}@Overrideprotected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {JavaType javaType = getJavaType(clazz);try {return this.objectMapper.readValue(inputMessage.getBody(), javaType);} catch (IOException ex) {throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);}}@Overrideprotected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());JsonGenerator jsonGenerator = this.objectMapper.getJsonFactory().createJsonGenerator(outputMessage.getBody(), encoding);try {if (this.prefixJson) {jsonGenerator.writeRaw("{} && ");}this.objectMapper.writeValue(jsonGenerator, object);} catch (IOException ex) {throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);}}/** * Return the Jackson {@link JavaType} for the specified class. * <p> * The default implementation returns {@link ObjectMapper#constructType(java.lang.reflect.Type)}, but this can be overridden in subclasses, to allow for custom generic collection handling. For * instance: *  * <pre class="code"> * <p> * protected JavaType getJavaType(Class<?> clazz) { * if (List.class.isAssignableFrom(clazz)) { * return objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, MyBean.class); * } else { * return super.getJavaType(clazz); * } * } * </pre> *  * @param clazz the class to return the java type for * @return the java type */protected JavaType getJavaType(Class<?> clazz) {return objectMapper.constructType(clazz);}/** * Determine the JSON encoding to use for the given content type. *  * @param contentType the media type as requested by the caller * @return the JSON encoding to use (never <code>null</code>) */protected JsonEncoding getJsonEncoding(MediaType contentType) {if (contentType != null && contentType.getCharSet() != null) {Charset charset = contentType.getCharSet();for (JsonEncoding encoding : JsonEncoding.values()) {if (charset.name().equals(encoding.getJavaName())) {return encoding;}}}return JsonEncoding.UTF8;}}

至此java部分就完了,下面编写newlist.jsp和index.jsp两个文件

index.jsp内容如下

<%@ page language="java" pageEncoding="UTF-8"%>  <a href="<%=request.getContextPath()%>/NewsController/newslist.htm">Visit "/newslist.htm" to my SpringMVC demo page</a>  <br/>  <br/>  

newlist.jsp内容如下

<%@ page language="java" pageEncoding="UTF-8"%>  <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  <h2>Welcome to my SpringMVC demo page</h2>  <c:forEach items="${newsList}" var="news">      <c:out value="${news.name}"/><br/>      <c:out value="${news.content}"/><br/>  </c:forEach>  

搞定,启动mysql数据库,启动tomcat。在浏览器输入

http://localhost:8080/ExtBook/

看到


点击链接可以看到


mvc工作正常。

输入http://localhost:8080/ExtBook/NewsController/load

看到


数据库,json都正常,以后我们将用这部分来做接口程序。


可能说的有些遗漏,稍后我上传源码,记得配置好自己的数据库

工程下载

http://download.csdn.net/detail/yunji3344/5259157

原创粉丝点击