浅谈SpringMVC+Spring3+Hibernate4开发环境搭建
程序员文章站
2024-03-09 13:31:41
早期的项目比较简单,多是用jsp 、servlet + jdbc 直接搞定,后来使用 struts1(struts2)+spring+hibernate, 严格按照分层概念...
早期的项目比较简单,多是用jsp 、servlet + jdbc 直接搞定,后来使用 struts1(struts2)+spring+hibernate, 严格按照分层概念驱动项目开发,这次又使用 spring mvc取代struts来进行开发。
mvc已经是现代web开发中的一个很重要的部分,下面介绍一下springmvc+spring3+hibernate4的开发环境搭建
先大致看一下项目结构:
具体的代码不再演示,主要是走了一个很平常的路线,mvc-servcie-dao-hibernate的结构,源码可以下载,主要看一下配置文件。解释见注释
web.xml
<?xml version="1.0" encoding="utf-8"?> <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_2_5.xsd" id="webapp_id" version="2.5"> <display-name>springmvc</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置spring --> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath*:config/spring-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!-- 配置springmvc --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>classpath*:config/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 设置字符集 --> <filter> <filter-name>encodingfilter</filter-name> <filter-class>org.springframework.web.filter.characterencodingfilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceencoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 控制session的开关 --> <filter> <filter-name>opensession</filter-name> <filter-class>org.springframework.orm.hibernate4.support.opensessioninviewfilter</filter-class> </filter> <filter-mapping> <filter-name>opensession</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
spring-servlet.xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 注解扫描的包 --> <context:component-scan base-package="com.jialin" /> <!-- 开启注解方案1 --> <!-- 注解方法处理 --> <!-- <bean class="org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter" /> --> <!-- 注解类映射处理 --> <!-- <bean class="org.springframework.web.servlet.mvc.annotation.defaultannotationhandlermapping"></bean> --> <!-- 开启注解方案2 --> <mvc:annotation-driven /> <!-- 静态资源访问,方案1 --> <mvc:resources location="/img/" mapping="/img/**" /> <mvc:resources location="/js/" mapping="/js/**" /> <!-- 静态资源访问,方案2 --> <!-- <mvc:default-servlet-handler/> --> <!-- 视图解释类 --> <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/"></property> <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 --> <property name="suffix" value=".jsp"></property> </bean> <!-- 上传文件bean --> <!-- <bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver"> <property name="defaultencoding" value="utf-8" /> <property name="maxuploadsize" value="10485760000" /> <property name="maxinmemorysize" value="40960" /> </bean> --> </beans>
spring-hibernate.xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 配置数据源 --> <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="com.mysql.jdbc.driver" /> <property name="url" value="jdbc:mysql://127.0.0.1/springmvc" /> <property name="username" value="root" /> <property name="password" value="123456" /> </bean> <!-- 配置hibernate sessionfactory--> <bean id="sessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean"> <property name="datasource" ref="datasource" /> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.mysqldialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hiberante.format_sql">true</prop> </props> </property> <property name="configlocations"> <list> <value> classpath*:config/hibernate/hibernate.cfg.xml </value> </list> </property> </bean> <!-- 事务管理器 --> <bean id="transactionmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager"> <property name="sessionfactory" ref="sessionfactory"></property> </bean> <!-- 事务代理类 --> <bean id="transactionbese" class="org.springframework.transaction.interceptor.transactionproxyfactorybean" lazy-init="true" abstract="true"> <property name="transactionmanager" ref="transactionmanager"></property> <property name="transactionattributes"> <props> <prop key="add*">propagation_required,-exception</prop> <prop key="update*">propagation_required,-exception</prop> <prop key="insert*">propagation_required,-exception</prop> <prop key="modify*">propagation_required,-exception</prop> <prop key="delete*">propagation_required,-exception</prop> <prop key="del*">propagation_required,-exception</prop> <prop key="get*">propagation_never</prop> </props> </property> </bean> </beans>
spring-core.xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 引入其他配置文件,可以为多个 --> <import resource="classpath*:config/spring/spring-user.xml"/> </beans>
spring-user.xml
<?xml version="1.0" encoding="utf-8"?> <!doctype beans public "-//spring//dtd bean 2.0//en" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [ <!entity contextinclude system "org/springframework/web/context/web-inf/contextinclude.xml"> ]> <beans> <!-- spring bean --> <bean id="userdao" class="com.jialin.dao.userdao"> <property name="sessionfactory" ref="sessionfactory"></property> </bean> <bean id="usermanagerbase" class="com.jialin.service.usermanager"> <property name="userdao" ref="userdao"></property> </bean> <!-- parent为transactionbese,表示支持事务 --> <bean id="usermanager" parent="transactionbese"> <property name="target" ref="usermanagerbase"></property> </bean> </beans>
hibernate.cfg.xml
<!doctype hibernate-configuration public "-//hibernate/hibernate configuration dtd 3.0//en" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 引入需要映射的类 --> <mapping class="com.jialin.entity.user"/> </session-factory> </hibernate-configuration>
下面再来看看controller
package com.jialin.controller; import java.io.ioexception; import java.io.printwriter; import java.util.list; import javax.annotation.resource; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import com.jialin.entity.user; import com.jialin.service.iusermanager; @controller //类似struts的action @requestmapping("/user") public class usercontroller { @resource(name="usermanager") // 获取spring配置文件中bean的id为usermanager的,并注入 private iusermanager usermanager; @requestmapping("/adduser") // 请求url地址映射,类似struts的action-mapping public string adduser(user user){ if(usermanager.adduser(user)) { // 重定向 return "redirect:/user/getalluser"; }else { return "/fail"; } } @requestmapping("/updateuser") public string updateuser(user user,httpservletrequest request){ if (usermanager.updateuser(user)) { user = usermanager.getoneuser(user); request.setattribute("user", user); return "/useredit"; }else { return "/fail"; } } @requestmapping("/deluser") public void deluser(user user,httpservletresponse response){ string result = "{\"result\":\"error\"}"; if(usermanager.deluser(user)){ result = "{\"result\":\"success\"}"; } printwriter out = null; response.setcontenttype("application/json"); try { out = response.getwriter(); out.write(result); } catch (ioexception e) { e.printstacktrace(); } } @requestmapping("/toadduser") public string toadduser(){ return "/useradd"; } @requestmapping("/toupdateuser") public string toupdateuser(user user,httpservletrequest request){ user user1=usermanager.getoneuser(user); request.setattribute("user1", user1); return "/useredit"; } @requestmapping("/getalluser") public string getalluser(httpservletrequest request){ list userlist=usermanager.getalluser(); request.setattribute("userlist", userlist); return "/usermain"; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读