Myeclipse环境下 Springmvc注解式与hibernate框架整合具体步骤!
来源:互联网 发布:c语言左移一位 编辑:程序博客网 时间:2024/06/07 02:23
1:首先要在项目WEB-INF 的 lib 目录下添加jdbc所需要的数据库驱动包!
2:先添加spring框架
勾选上面四个包还有一个spring 3.0 web libraries 的包别忘了勾选,总共五个包!
3:添加hibernate框架
选最新的3.3版本如图:
然后点next
应为是和springmvc做整合,所以选择下面一个,如图
点next,还是选择第二个如图:
选择之前创建好的数据库链接如图:
最后一项把勾去掉点完成就可以了,如图
这样,基本的框架就添加完成了,下面就是修改配置文件了!
4:修改各类配置文件(记得反转你要用的表,我就不演示怎么反转表了)
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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- --> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置DispatchServlet读取spring配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </init-param> </servlet> <!-- 配置DispatchServlet读取spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <!-- 配置监听器,初始化spring配置文件 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!-- 通过过滤器设置编码集合 ,防止中文乱码问题--> <filter> <filter-name>springUtf8Encoding</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>springUtf8Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
注意:(springmvc和注解式springmvc的spring配置文件的头文件不一样!)
applicationContext.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"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"><bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName"value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://127.0.0.1:3306/emp"></property><property name="username" value="root"></property><property name="password" value="root"></property></bean><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource"><ref bean="dataSource" /></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop></props></property><property name="mappingResources"><list><value>com/springmvchibernate/domain/Emp.hbm.xml</value></list></property></bean> <!-- 自动加载驱动 ,通过主键把相应的url映射到controller上--> <mvc:annotation-driven/> <!-- 设置哪些包需要使用注解 --> <context:component-scan base-package="com.emp.web"></context:component-scan> <!-- 用bean容器创建一个EmpServiceImpl对象,并为属性赋值 --> <bean id="empserviceimpl" class="com.emp.service.EmpServiceImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean></beans>
我列子项目的层次结构如图:
EmpAction.java
package com.emp.web;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import com.emp.service.EmpService;import com.springmvchibernate.domain.Emp;@Controllerpublic class EmpAction {@Autowired@Qualifier("empserviceimpl") //要与spring配置文件生成实现类的bean节点id保持一致EmpService EmpServiceImpl; //用注解注入接口的实现类对象,方便方法调用里面的方法/** * 用户登录 * @return */@RequestMapping("/login.action")//虽然这里用的是Emp对象的封装,但是springmvc的不同的是前台不需要写成emp.ename,只需要写属性名ename就可以了public String login(Emp emp ,Model m){Emp e = EmpServiceImpl.login(emp);if(e!=null){return "a.jsp";}else{m.addAttribute("msg","你输入的用户名或者密码有误");return "index.jsp";}}}
接口的代码:
EmpService.java
package com.emp.service;import com.springmvchibernate.domain.Emp;public interface EmpService {/** * 用户登录 * @param emp * @return */public Emp login(Emp emp);}
接口实现类的代码:
EmpServiceImpl.java
package com.emp.service;import java.util.List;import org.springframework.orm.hibernate3.HibernateTemplate;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.springmvchibernate.domain.Emp;public class EmpServiceImpl extends HibernateDaoSupport implements EmpService {//用户登录public Emp login(Emp emp) {HibernateTemplate ht = this.getHibernateTemplate();List l = ht.find("from Emp e where e.ename ='"+emp.getEname()+"' and e.password ='"+emp.getPassword()+"'");if(l.size()!=0){return (Emp)l.get(0);}else{return null;}}}
前台
index.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><form method="post" action="login.action"><p> 用户名:<input type="text" name="ename"></p><p> 密 码:<input type="password" name="password"></p><p> <input type="submit" value="提交" name="button1"></p></form> ${msg}</body></html>
好了,这个列子就是一个springmvc注解式加hibernate框架的整合,就是一个简单的登录.感觉最主要的部分就是web.xml和 applicationContext.xml这两个配置文件的配置要配好!
完
0 0
- Myeclipse环境下 Springmvc注解式与hibernate框架整合具体步骤!
- MyEclipse 搭建SpringMVC + Hibernate + Spring框架整合
- springMVC+Hibernate 框架整合
- SpringMVC+Hibernate框架整合
- SpringMVC+Hibernate全注解整合
- SpringMVC与Hibernate整合
- 在MyEclipse环境下 struts 2、spring、hibernate的整合
- Spring+SpringMVC+Hibernate框架整合
- Spring+SpringMVC+Hibernate框架整合
- Spring+SpringMVC+Hibernate框架整合
- Spring+Hibernate+SpringMVC整合配置(注解式)
- springMVC框架整合--(Spring+SpringMVC+hibernate)
- Spring+SpringMVC+Hibernate整合非注解版
- Spring+SpringMVC+Hibernate整合注解版
- 整合ssh框架的具体步骤
- Hibernate框架整合JPA注解基础实践
- Springmvc框架(非注解与注解)
- Myeclipse搭建SpringMVC-Spring-hibernate框架
- Android-用apktool实现多渠道自动打包
- Groovy入门教程
- python的函数参数传递
- jQuery UI使用总结
- 计算几何 欧拉定理应用
- Myeclipse环境下 Springmvc注解式与hibernate框架整合具体步骤!
- MatLab建模学习笔记1——二维绘图
- bash: 09: value too great for base (error token is 09)
- JavaSE初步学习笔记(17天)
- 【Get深一度】谐振腔中的电场(E Field[V_per_m])与磁场(H field[A_per_m])分布
- java.lang.IllegalArgumentException: column '_id' does not exist 解决方法
- maven安装本地jar包
- android、ios与服务器端php使用rsa加密解密通讯
- freemarker常见语法大全