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&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";
}
}