首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

Spring3.0MVC和Hibernate基于annotation引文的整合

2013-11-09 
Spring3.0MVC和Hibernate基于annotation注解的整合springmvc和hibernate的annotation集合:首先web.xml?xm

Spring3.0MVC和Hibernate基于annotation注解的整合
springmvc和hibernate的annotation集合:
首先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>hibernateAspringmvc</display-name>  <context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:applicationContext*.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><!-- this is 'spring' name for your spring-servlet.xml --><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>*.xl</url-pattern></servlet-mapping>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list></web-app>


然后是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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"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.0.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-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/jdbc         http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"default-autowire="byName" default-lazy-init="true"><!-- this pack must include xxx-servlet.xml's pack. --><context:component-scan base-package="org.xlaohe1" /><bean id="dataSource"value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/test" /><property name="username" value="root" /><property name="password" value="root" /></bean><bean id="sessionFactory"ref="dataSource" />          <!-- 这几句在spring hibernate的注解整合中可以不需要 因为下面的2就是扫描指定路劲下的实体进行映射 -->                <!-- 1======================= --><property name="namingStrategy"><bean /></property><property name="annotatedClasses"><!-- the must have. before this is mapping,now is entity --><list><value>org.xlaohe1.model.User</value></list></property>                <!-- 1======================= --><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.show_sql">false</prop><prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop><prop key="hibernate.cache.use_query_cache">false</prop><prop key="hibernate.jdbc.batch_size">50</prop><prop key="hibernate.cache.use_second_level_cache">false</prop></props></property>                <!-- 2======================= -->                <!-- 自动扫描指定位置下的实体文件进行映射 --><property name="packagesToScan" value="org.xlaohe1.model" />                 <!-- 2======================= --></bean><bean id="transactionManager"ref="sessionFactory" /></bean><tx:annotation-driven transaction-manager="transactionManager" /></beans>  


spring-serlvet.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">    <!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->      <context:component-scan base-package="org.xlaohe1.web"/>        <!--启动Spring MVC的注解功能,完成请求和注解POJO的映射      <bean           p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>-->                    <bean id="viewResolver" value="org.springframework.web.servlet.view.JstlView"/>              <property name="prefix" value="/WEB-INF/jsp/"/>              <property name="suffix" value=".jsp"/>          </bean>            </beans>  


entity:
package org.xlaohe1.model;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 = "users", catalog = "test")public class User {private Integer id;private String username;private String password;private Integer age;public User() {super();}@Id@GeneratedValue(strategy=GenerationType.AUTO)@Column(name = "id")public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}@Column(name = "username")public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}@Column(name = "password")public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Column(name = "age")public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}}


userdaoimpl
package org.xlaohe1.dao.impl;import java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import org.springframework.stereotype.Repository;import org.xlaohe1.dao.IUserDao;import org.xlaohe1.model.User;@Repositorypublic class UserDaoImpl extends HibernateDaoSupport implements IUserDao {@SuppressWarnings("unchecked")@Overridepublic List<User> findAllUsers() {String hql = "FROM User";return getHibernateTemplate().find(hql);}}


userserviceimpl
package org.xlaohe1.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import org.xlaohe1.dao.IUserDao;import org.xlaohe1.model.User;import org.xlaohe1.service.IUserService;@Service @Transactionalpublic class UserServiceImpl implements IUserService {@Autowired IUserDao userDao;@Override//@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)public List<User> findAllUsers() {return userDao.findAllUsers();}}


usercontroller
package org.xlaohe1.web;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import org.xlaohe1.model.User;import org.xlaohe1.service.IUserService;@Controllerpublic class UserController {@AutowiredIUserService userService;public UserController() {}@RequestMapping(value = "/show")public ModelAndView myMethod(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {List<User> ulst = userService.findAllUsers();modelMap.put("users", ulst);return new ModelAndView("showUser", modelMap);}@RequestMapping(value = "/t")public ModelAndView t() {return new ModelAndView("t");}} 





有意见和建议请留下..
<servlet> <!-- this is 'spring' name for your spring-servlet.xml --> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
<%@page import="org.springframework.ui.ModelMap"%><%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>在此处插入标题</title></head><body><br /><table border="1"><c:forEach var="list" items="${users}"><tr><td>${list.id }</td><td>${list.username }</td><td>${list.password }</td><td>${list.age }</td></tr></c:forEach></table>showUser page.${loginUser }<%=request.getAttribute("loginUser") %><a href="t.xl">remove</a></body></html> <context:component-scan base-package="org.xlaohe1.web"/>
这个不能写错,
你的dao和service的注解要写对
最后你的controoler的Autowired是接口的不是实现
主要你的Autowired地方看看 <context:component-scan base-package="org.xlaohe1.web"/>
这个不能写错,
你的dao和service的注解要写对
最后你的controoler的Autowired是接口的不是实现
主要你的Autowired地方看看

可否把你的lib包用到的jar 贴出来啊? 配置,代码真的一样。 估计和包有关系吧。

热点排行