http://xlaohe1.iteye.com/blog/1139028
一下代码不完整的,可参考
http://blog.csdn.net/jlh2/article/details/4807801
springmvc和hibernate的annotation集合:
首先web.xml
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代码
<?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"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" 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"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 这几句在spring hibernate的注解整合中可以不需要 因为下面的2就是扫描指定路劲下的实体进行映射 -->
<!-- 1======================= -->
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
</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"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
spring-serlvet.xml
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 class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> -->
<mvc:annotation-driven/>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
entity:
Java代码
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
Java代码
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;
@Repository
public class UserDaoImpl extends HibernateDaoSupport implements IUserDao {
@SuppressWarnings("unchecked")
@Override
public List<User> findAllUsers() {
String hql = "FROM User";
return getHibernateTemplate().find(hql);
}
}
userserviceimpl
Java代码
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 @Transactional
public class UserServiceImpl implements IUserService {
@Autowired IUserDao userDao;
@Override
//@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
public List<User> findAllUsers() {
return userDao.findAllUsers();
}
}
usercontroller
Java代码
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;
@Controller
public class UserController {
@Autowired
IUserService 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");
}
}
<context:annotation-config />这个配置告诉springmvc,springmvc相关的bean中使用注解来进行表示
<context:component-scan base-package="com.jlh2.study.web.module"/>这个配置告诉springmvc对com.jlh2.study.web.module进行扫描,并创建其中的javaBe an并注入到spring容器中。
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> 启动springmvc的注解映射功能
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /> 自动将访问url映射到同名的Controller上面
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> 启动springmvc的注解功能