`

2 springmvc+hibernate

 
阅读更多

springMVC-servlet.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/aop http://www.springframework.org/schema/aop/spring-aop-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/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">

	<context:annotation-config />
	<!-- 把标记了@Controller注解的类转换为bean -->
	<context:component-scan base-package="com.oys.**.controller" />
	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
	<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp" />
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" />

</beans>  

 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">
	<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:conf/applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	<!-- 默认放在classpath下,就不需要这里指定 -->
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:conf/log4j.properties</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>


	<!--*********  Hibernate过滤器  *********-->
	<filter>
		<filter-name>hibernateFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
		<init-param>
			<param-name>singleSession</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>hibernateFilter</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>
	<!--*********  编码格式过滤器  *********-->
	<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>*.do</url-pattern>
	</filter-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 log4j.properties

 

 

  
# log4j.rootLogger=[level],appenderName,appenderName,appenderName
log4j.rootLogger=error, stdout,logfile
log4j.logger.com.oys.base.dao=debug,dao  
log4j.logger.com.oys.base.controller=error,dao  

#stdout configure
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern= %d %p [%c] - %m%n

#logfile configure
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.Encoding=UTF-8
log4j.appender.logfile.File=../logs/weboa.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern= %d %p [%c] - %m%n

log4j.appender.dao=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dao.Encoding=UTF-8
log4j.appender.dao.File=../logs/dao.log
log4j.appender.dao.layout=org.apache.log4j.PatternLayout
log4j.appender.dao.layout.ConversionPattern= %d %p [%c] - %m%n

 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:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:annotation-config />
	<context:component-scan base-package="com.oys" />
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<!--
				<prop key="hibernate.hbm2ddl.auto">
					create
				</prop>-->
			</props>
		</property>
		<!-- annotatedScanPackages可以自动搜索某个package的全部标记@Entity class -->
		<property name="packagesToScan" value="com.oys.**.domain" />
	</bean>

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver">
		</property>
		<property name="url" value="jdbc:mysql://localhost:3306/myoa?useUnicode=true&amp;characterEncoding=UTF-8">
		</property>
		<property name="username" value="root"></property>
		<property name="password" value="system"></property>
	</bean>

	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" />
	
</beans>

 

package test;

import java.util.List;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.oys.base.domain.User;
import com.oys.base.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:conf/applicationContext.xml" })
//@ContextConfiguration(locations="classpath:conf/applicationContext.xml") // 加载配置
public class TestServiceTest {
	@Resource
	private UserService userService;

	@Test
	public void findAllUser() {
		List<User> list=userService.findAllUser();
		System.out.println(list.size());
	}

}

 

package com.oys.base.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.oys.base.dao.UserDao;
import com.oys.base.domain.User;

@Service("userService")
public class UserService {
	private UserDao userDao;

	@Autowired
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	public List<User> findAllUser() {
		return userDao.findAllUser();
	}
}

 

package com.oys.base.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "BASE_USER")
public class User implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4686756250462517944L;
	@Id
	@Column(name = "RESOURCEID", length = 40)
	@GenericGenerator(name = "idGenerator", strategy = "uuid")
	@GeneratedValue(generator = "idGenerator")
	public String resourceId;

	/**
	 * 登陆账号
	 */
	@Column(name = "ACCOUNT", unique = false, nullable = true, insertable = true, updatable = true, length = 40)
	private java.lang.String account;

	/**
	 * 真实姓名
	 */
	@Column(name = "REAL_NAME", unique = false, nullable = true, insertable = true, updatable = true, length = 40)
	private java.lang.String realName;

	public String getResourceId() {
		return resourceId;
	}

	public void setResourceId(String resourceId) {
		this.resourceId = resourceId;
	}

	public java.lang.String getAccount() {
		return account;
	}

	public void setAccount(java.lang.String account) {
		this.account = account;
	}

	public java.lang.String getRealName() {
		return realName;
	}

	public void setRealName(java.lang.String realName) {
		this.realName = realName;
	}

	public java.lang.String getPassword() {
		return password;
	}

	public void setPassword(java.lang.String password) {
		this.password = password;
	}

	public java.lang.String getRepass() {
		return repass;
	}

	public void setRepass(java.lang.String repass) {
		this.repass = repass;
	}

	public java.lang.String getRboxSex() {
		return rboxSex;
	}

	public void setRboxSex(java.lang.String rboxSex) {
		this.rboxSex = rboxSex;
	}

	public java.lang.String getPhone() {
		return phone;
	}

	public void setPhone(java.lang.String phone) {
		this.phone = phone;
	}

	/**
	 * 密码
	 */
	@Column(name = "PASSWORD", unique = false, nullable = true, insertable = true, updatable = true, length = 32)
	private java.lang.String password;

	@Transient
	private java.lang.String repass;

	/**
	 * 性别
	 */
	@Column(name = "RBOX_SEX", unique = false, nullable = true, insertable = true, updatable = true, length = 4)
	private java.lang.String rboxSex;

	/**
	 * 电话
	 */
	@Column(name = "PHONE", unique = false, nullable = true, insertable = true, updatable = true, length = 20)
	private java.lang.String phone;

}

 

package com.oys.base.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.oys.base.domain.User;
@Repository
public class UserDao extends BaseDao {
	private static Logger log=Logger.getLogger(BaseDao.class); 
	public List<User> findAllUser() {
		String hql = "from User t ";
		log.info(hql);
		return getHibernateTemplate().find(hql);
	}

}

 

package com.oys.base.dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class BaseDao extends HibernateDaoSupport{
	private static final Log log = LogFactory.getLog(BaseDao.class);
	@Autowired
	public void setSessionFactory0(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}
}

 

package com.oys.base.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.oys.base.domain.User;
import com.oys.base.service.UserService;

@Controller
@RequestMapping("/test.do")
public class TestController {
	private static Logger log = Logger.getLogger(TestController.class);
	@Resource
	private UserService userService;

	@RequestMapping(params = "method=login")
	// 请求url地址映射,类似Struts的action-mapping
	public String testLogin(String username, String password, HttpServletRequest request) {
		List<User> list = userService.findAllUser();
		log.error("大小"+list.size());
		return "loginSuccess";
	}
}

 

 

分享到:
评论

相关推荐

    基于Spring+SpringMVC+Hibernate的图书管理系统

    系统采用技术:Spring+SpringMVC+Hibernate+jQuery+Ajax+面向接口编程。 简介:该图书管理项目实现了三种角色的功能,分别是超级管理员,图书管理员和读者。在系统中,实现了图书管理,统计管理,系统管理和系统监控...

    springMVC+maven+hibernate框架

    springMVC+maven+hibernate框架,搭建的过程 可以参考 java进阶(五)------springMVC---springMVC+Hibernate+maven完整搭建运行步骤 http://blog.csdn.net/zzq900503/article/details/49892783

    新SSH(Spring+SpringMVC+Hibernate)框架结构的java web案例实践

    随着struts的安全问题的暴露,原由Struts2+spring+hibernate构成的SSH2已经被越来越多的开发者所弃用,反而,由Spring+SpringMVC+Hibernate构成的SSH框架越来越受欢迎!这里提供了一个案例代码,希望对大家搭建环境...

    Spring+SpringMVC+Hibernate框架集成详解

    Spring+SpringMVC+Hibernate 框架集成详解 本文档旨在详细介绍 Spring、SpringMVC 和 Hibernate 框架的集成,旨在帮助开发人员快速了解这三个框架的集成过程。 Spring 框架 Spring 框架是一个 Java 语言的开源...

    基于注解的springMVC+hibernate+oracle包含数据库查询

    本文将详细解析基于注解的SpringMVC+Hibernate+Oracle数据库集成的实现过程及其核心知识点。 首先,SpringMVC是Spring框架的一部分,它是一个轻量级的MVC(Model-View-Controller)框架,用于处理HTTP请求和响应。...

    SpringMVC+Hibernate+annotation+Easyui权限管理系统

    SpringMVC+Hibernate+annotation+Easyui权限管理系统,Session过期控制(包括Ajax请求的过期控制),访问权限控制(权限控制到按钮),hibernate数据库用户名与密码加密,其它的不说了,绝对物有所值

    springmvc+hibernate+easyui

    在"springmvc+hibernate+easyui"项目中,可能使用EasyUI的DataGrid组件展示数据列表,结合SpringMVC的ModelAndView或Model对象,将后端查询结果转化为JSON格式,通过Ajax请求传递到前端,最终动态渲染数据列表。...

    idea工具创建的Spring+SpringMVC+Hibernate+maven项目

    标题中的"idea工具创建的Spring+SpringMVC+Hibernate+maven项目"指的是使用IntelliJ IDEA这个集成开发环境(IDE)构建的一个Java Web项目,该项目整合了四个关键的技术框架:Spring、SpringMVC、Hibernate以及Maven...

    springMvc+hibernate Demo

    这个 "springMvc+hibernate Demo" 压缩包文件很可能是为了展示如何将这两个框架整合在一起,创建一个完整的数据驱动的 Web 应用。 **Spring MVC 框架** Spring MVC 是 Spring 框架的一部分,设计用于构建可维护、松...

    springmvc+hibernate+oracle基础框架

    《构建基于SpringMVC、Hibernate和Oracle的基础框架》 在现代企业级应用开发中,SpringMVC、Hibernate和Oracle的组合被广泛应用于构建强大的后端系统。这些技术各自扮演着关键角色,共同构成了一个高效、稳定的开发...

    springmvc+hibernate完整项目

    SpringMVC和Hibernate是Java开发中的两个重要框架,SpringMVC负责处理Web应用的请求-响应,而Hibernate则是一个强大的对象关系映射(ORM)工具,用于简化数据库操作。这个"springmvc+hibernate完整项目"是一个整合了...

    Maven搭建SpringMVC+Hibernate项目源码

    Maven搭建SpringMVC+Hibernate项目源码,包括Hibernate二级缓存Ehcache的搭建等等,博文地址:http://blog.csdn.net/fengshizty/article/details/43635305

    SpringMVC+Hibernate+Spring

    【SpringMVC+Hibernate+Spring】整合框架详解 在当今的Java Web开发中,SpringMVC、Hibernate和Spring是三个非常关键的组件,它们分别负责不同的职责:SpringMVC作为表现层框架,处理HTTP请求与响应;Hibernate作为...

    SpringMVC+HIbernate登录程序并进行增删改查

    SpringMVC+Hibernate登录程序并进行增删改查是一个经典的Web开发示例,它整合了两个重要的Java技术框架——SpringMVC和Hibernate,用于构建基于Java的动态网站应用。SpringMVC是Spring框架的一部分,主要负责处理...

    Maven整合Spring+SpringMVC+Hibernate+SpringDataJPA

    在现代Java Web开发中,"Maven整合Spring+SpringMVC+Hibernate+SpringDataJPA"是一个常见的架构组合,被广泛应用于构建企业级应用程序。这个组合通常被称为"SSM",其中"M"代表Maven,"S"代表Spring,包括Spring核心...

    基于maven3+spring3+springMVC+hibernate3+mysql的图书管理小系统

    基于maven3+spring3+hibernate3+mysql的图书管理系统demo,主要为了练习在intellij上开发maven项目的体验和重新练习一下配置springmvc+hibernate+spring

    Spring+SpringMVC+Hibernate+Maven框架搭建及配置文件源码

    在"Spring+SpringMVC+Hibernate+Maven框架搭建及配置文件源码"这个项目中,开发者可能已经包含了这些框架的配置文件,例如Spring的applicationContext.xml、SpringMVC的servlet-context.xml、Hibernate的hibernate....

    SpringMVC+Hibernate整合的登录Demo 导入即可直接运行适合初次接触整合

    2. **Hibernate框架**:Hibernate是一个强大的ORM框架,能够简化数据库操作,通过Java对象与数据库表进行映射。在Demo中,Hibernate会处理用户登录时的数据库查询,例如验证用户名和密码。你将学习如何配置Hibernate...

    spring3+springmvc+jpa+hibernate多数据源

    "spring3+springmvc+jpa+hibernate多数据源"是一个示例项目,它演示了如何在一个应用中集成Spring 3、Spring MVC、JPA 2.0以及Hibernate,以实现对多个数据源的支持。下面将详细介绍这些技术及其集成的关键点。 **...

    Spring+SpringMVC+Hibernate+JSP框架搭建

    "Spring+SpringMVC+Hibernate+JSP框架搭建"就是一个经典的Java Web开发解决方案,它整合了四个核心组件来实现全面的后端功能和用户界面交互。下面我们将深入探讨这四个组件以及它们如何协同工作。 1. **Spring框架*...

Global site tag (gtag.js) - Google Analytics