首页 > 代码库 > Spring-SpringMVC-Hibernate maven整合
Spring-SpringMVC-Hibernate maven整合
前面的一篇文章http://blog.csdn.net/wangdianyong/article/details/38842693曾经也写过三者之间的整合,但是当时没有用maven
导致了项目中含有大量包,看着十分不美观!
下面就使用maven重新整合一下!新手还望不吝赐教!
maevn项目的建立过程省略
看项目结构
各个配置文件代码我贴一下,不一样的地方请自行修改
database.properties
hibernate.dialect=org.hibernate.dialect.MySQLDialect driverClassName=com.mysql.jdbc.Driver validationQuery=SELECT 1 url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8 username=root password=root hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=true
spring-hibernate.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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!-- 使用C3P0数据源,MySQL数据库 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- MySQL5 --> <property name="driverClass" value=http://www.mamicode.com/"${driverClassName}">>spring-mvc.xml<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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" 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"> <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 --> <context:component-scan base-package="com.iss.controller" /> <!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <!-- <bean id="mappingJacksonHttpMessageConverter" --> <!-- class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> --> <!-- <property name="supportedMediaTypes"> --> <!-- <list> --> <!-- <value>text/html;charset=UTF-8</value> --> <!-- </list> --> <!-- </property> --> <!-- </bean> --> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" />json转换器 </list> </property> </bean> --> <mvc:annotation-driven /> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value=http://www.mamicode.com/"/" />>
spring.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:context="http://www.springframework.org/schema/context" 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 "> <!--启用注解 --> <context:annotation-config /> <context:component-scan base-package="com.iss.action,com.iss.dao.impl,com.iss.service"></context:component-scan> <!-- 引入属性文件Hibernate属性配置 --> <context:property-placeholder location="classpath:database.properties" /> <!-- 自动扫描dao和service包(自动注入) --> <!-- <context:component-scan base-package="com.iss*" /> --> </beans>
web.xml<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <display-name></display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/spring.xml,/WEB-INF/classes/spring-hibernate.xml,</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--Hibernate的session丢失解决方法 --> <filter> <filter-name>openSessionInView</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSessionInView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- struts2配置 --> <!-- <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> --> <!-- springMVC 配置 --> <servlet> <description>spring mvc servlet</description> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <description>spring mvc 配置文件</description> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/spring-mvc.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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
src/main/java中各个包的内容com.iss.controller.UserController
package com.iss.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.iss.model.User; import com.iss.service.UserService; @Controller("userController") @RequestMapping("user") public class UserController { @Autowired private UserService userService; @RequestMapping("/reg") public String saveUser(User user) { System.out.println(userService); userService.save(user); return "success"; } }
com.iss.dao.UserDaopackage com.iss.dao; import com.iss.model.User; public interface UserDao<T> { public User save(T o); }
com.iss.dao.impl.UserDaoImplpackage com.iss.dao.impl; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import com.iss.dao.UserDao; import com.iss.model.User; @Repository("userDao") public class UserDaoImpl implements UserDao<User> { @Autowired private SessionFactory sessionFactory; // // public void setSessionFactory(SessionFactory sessionFactory) { // this.sessionFactory = sessionFactory; // } // // public SessionFactory getSessionFactory() { // return sessionFactory; // } @Override public User save(User o) { Session session = sessionFactory.getCurrentSession(); session.save(o); return o; } }
com.iss.model.Userpackage com.iss.model; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "user") public class User implements Serializable { private int id; private String name; @Id @GeneratedValue(strategy = GenerationType.AUTO) public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
com.iss.service.UserServicepackage com.iss.service; import com.iss.model.User; public interface UserService { public User save(User user); }
com.iss.service.impl.UserServiceImplpackage com.iss.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.iss.dao.UserDao; import com.iss.model.User; import com.iss.service.UserService; @Service("userService") @Transactional public class UserServiceImpl implements UserService { @Autowired private UserDao<User> userDao; @Override public User save(User user) { // TODO Auto-generated method stub return userDao.save(user); } }
index,jsp<body> <form action="userAction" method="post"> <table width="207" border="0" align="center"> <tr> <td colspan="2" align="center" nowrap="nowrap">用户注册</td> </tr> <tr> <td width="68" nowrap="nowrap">用户名</td> <td width="127" nowrap="nowrap"><label> <input name="name" type="text" id="username" size="20" /> </label></td> </tr> <tr> <td colspan="2" align="center" nowrap="nowrap"><label> <input type="submit" value=http://www.mamicode.com/"注册" /> >
success.jsp<body>${user.name } </body>完成之后mvn install 部署到tomcat服务器即可!
Spring-SpringMVC-Hibernate maven整合
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。