spring4mvc+hibernate4整合
2015-09-27 12:38
495 查看
1.首先先搭建springMvc
(1)所需jar包:
spring自带:
(不了解,所以把spring4的所有jar包都加进来)
spring还需要:
以上jar包包括了spring4mvc_hibernate4中spring所有需要的包。
(2)在web.xml中加入springmvc的控制,springbeans的控制,以及中文乱码的控制
(3)在web.xml中DispatcherServlet所写的对应位置上创建springmvc-context.xml,在listener所写的位置即src目录下下创建beans.xml
springmvc-context.xml:
beans.xml:
(4)创建WebRoot/index.jsp、WebRoot/view/student/add.jsp、WebRoot/view/student/list.jsp
内容分别为
(5)在src下加入dao类com.dao.StudentDao.java,service类com.service.StudentService.java,控制类com.controller.StudentController.java
(6)访问http://127.0.0.1:8080/spring4mvc_hibernate4测试springmvc是否成功。
至此的源码:http://download.csdn.net/detail/qwcs163/9142475
(7) spring配置正确,接下来整合hibernate,首先导入hibernate4 jar包
(8)创建model层
(9)修改beans.xml,加入hibernate整合 以及事物
(10)修改dao层,service层,修改controller层。
(11)结构图及源码
下载地址:http://download.csdn.net/detail/qwcs163/9152121
(1)所需jar包:
spring自带:
(不了解,所以把spring4的所有jar包都加进来)
spring还需要:
以上jar包包括了spring4mvc_hibernate4中spring所有需要的包。
(2)在web.xml中加入springmvc的控制,springbeans的控制,以及中文乱码的控制
<?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"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!--控制springbeans--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/beans.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 解决中文乱码 (只对post提交有效) --> <filter> <filter-name>Character Encoding</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>Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--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>/WEB-INF/springmvc-context.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
(3)在web.xml中DispatcherServlet所写的对应位置上创建springmvc-context.xml,在listener所写的位置即src目录下下创建beans.xml
springmvc-context.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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" 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-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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "> <!-- springmvc 注解驱动 --> <mvc:annotation-driven/> <!-- 扫描器 --> <context:component-scan base-package="com.controller"/><!-- 重中之重 base-package一定要写到controller一层, 否则不好使 整合hibernate用getCurrentSession()会报 No Session found for current thread --> 4000 ; <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/view/"></property> <!-- 后缀 --> <property name="suffix" value=".jsp"></property> </bean> </beans>
beans.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "> <context:annotation-config /> <context:component-scan base-package="com.dao,com.service,com.controller" /> </beans>
(4)创建WebRoot/index.jsp、WebRoot/view/student/add.jsp、WebRoot/view/student/list.jsp
内容分别为
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>导航页</title> </head> <body> <a href="student/add.do">新增</a> <br/> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>新增用户页</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <form action="student/add.do" method="post"> 姓名:<input type="text" name="name"/><br/> 年龄:<input type="text" name="age"/><br/> <input type="submit" value="提交"/> </form> ${result} </body> </html>
(5)在src下加入dao类com.dao.StudentDao.java,service类com.service.StudentService.java,控制类com.controller.StudentController.java
package com.dao; import javax.annotation.Resource; import org.springframework.stereotype.Component; @Component public class StudentDao { public void save(){ System.out.println("插入"); } }
package com.service; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.dao.StudentDao; @Component public class StudentService { private StudentDao studentDao; public StudentDao getStudentDao() { return studentDao; } @Resource public void setStudentDao(StudentDao studentDao) { this.studentDao = studentDao; } public void addStudent(){ studentDao.save(); } }
package com.controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.service.StudentService; @Controller @RequestMapping("/student") public class StudentController { private StudentService studentService; public StudentService getStudentService() { return studentService; } @Resource public void setStudentService(StudentService studentService) { this.studentService = studentService; } @RequestMapping("/add.do") public String addStudent(String name,String age,Model model){ System.out.println(name+"-"+age); String result = (name==null||"".equals(name))?"":name+"新增成功"; model.addAttribute("result",result); if(name!=null && age!=null){ studentService.addStudent(); } return "student/add"; } }
(6)访问http://127.0.0.1:8080/spring4mvc_hibernate4测试springmvc是否成功。
至此的源码:http://download.csdn.net/detail/qwcs163/9142475
(7) spring配置正确,接下来整合hibernate,首先导入hibernate4 jar包
(8)创建model层
package com.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.SequenceGenerator; @Entity public class Student { private int id; private String name; private int age; @SequenceGenerator(name="sequenceGenerator",sequenceName="student_sequence",allocationSize=1) @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequenceGenerator") @Id 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; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
(9)修改beans.xml,加入hibernate整合 以及事物
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "> <context:annotation-config /> <context:component-scan base-package="com.dao,com.service,com.controller" /> <bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/> <property name="username" value="cat"/> <property name="password" value="cat"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="myDataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.max_fetch_depth">1</prop> <prop key="hibernate.use_sql_comments">true</prop> </props> </property> <property name="packagesToScan"> <list> <value>com.model</value> </list> </property> </bean> <!-- 配置Hibernate事务管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config proxy-target-class="true"> <aop:pointcut id="serviceOperation" expression="execution(public * com.service.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/> </aop:config> </beans>
(10)修改dao层,service层,修改controller层。
package com.dao; import javax.annotation.Resource; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.stereotype.Component; import com.model.Student; @Component public class StudentDao { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } @Resource public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public void save(Student s){ Session session = sessionFactory.getCurrentSession(); session.save(s); System.out.println("插入"); } }
package com.service; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.dao.StudentDao; import com.model.Student; @Component public class StudentService { private StudentDao studentDao; public StudentDao getStudentDao() { return studentDao; } @Resource public void setStudentDao(StudentDao studentDao) { this.studentDao = studentDao; } public void addStudent(Student s){ studentDao.save(s); } }
package com.controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.model.Student; import com.service.StudentService; @Controller @RequestMapping("/student") public class StudentController { private StudentService studentService; public StudentService getStudentService() { return studentService; } @Resource public void setStudentService(StudentService studentService) { this.studentService = studentService; } @RequestMapping("/add.do") public String addStudent(String name,String age,Model model){ System.out.println(name+"-"+age); String result = (name==null||"".equals(name))?"":name+"新增成功"; model.addAttribute("result",result); if(name!=null && age!=null){ Student s= new Student(); s.setName(name); s.setAge(Integer.parseInt(age)); studentService.addStudent(s); } return "student/add"; } }
(11)结构图及源码
下载地址:http://download.csdn.net/detail/qwcs163/9152121
相关文章推荐
- JDK源码研究——ReentrantLock浅析
- Java正则表达中Greedy Reluctant Possessive 的区别
- Java点滴-List<Integer> list; 中尖括号的意思
- Java内存机制
- error,exception,runtimeexce区别
- Throwable、Error、Exception、RuntimeException 区别 联系
- 怎么创作背景图片
- Unsupported major.minor version 51.0解决办法
- 重写与重载区别
- JSTL(c标签)与Struts2(s标签)标签的常用功能对比
- netbeans编辑器快捷键大全
- Java Web开发之图形验证码的生成与使用方法
- CXF错误:Unsupported major.minor version 51.0,java.lang.UnsupportedClassVersionErro
- java得到clientIP地址和MAC住址
- java int和integer的区别
- Java Web开发之MD5加密用法分析
- m个生产者和n个消费者和x个搬运者的java实现
- springMvc中406错误解决,springMvc使用json出现406 (Not Acceptable)
- JAVA中modle,dao,service,action的功能区分
- JAVA设计模式之策略模式