SpringMVC+Spring+Hibernate4框架整合

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zgsxhdzxl/article/details/55656211


Spring架包下载地址:http://repo.spring.io/release/org/springframework/spring/

Hiberna架包下载地址:http://hibernate.org/orm/


需要的框架截图


搭建环境基本配置

一、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: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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	
	
	<!-- 配置数据源 -->
	<context:property-placeholder location="classpath:database.properties" />
	
	<!-- 配置 dataSource-->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
	</bean>
	
	<!-- 配置sessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<!-- 配置数据源属性 -->  
        <property name="dataSource" ref="dataSource"></property>  
        
        <!-- 配置扫描的实体包(pojo) -->  
        <property name="namingStrategy">  
            <bean class="org.hibernate.cfg.ImprovedNamingStrategy"></bean>  
        </property>  
        <property name="packagesToScan" value="com.zxl.entity"></property>  
         
        <property name="hibernateProperties">  
            <props>  
            <!-- 数据库的方言 -->  
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>  
                <prop key="hibernate.show_sql">true</prop>  
                <prop key="hibernate.format_sql">true</prop>  
                <prop key="hibernate.hbm2ddl.auto">update</prop>  
            </props>  
        </property>  
	</bean>
	
	<!-- 配置Hibernate 的事物管理器 -->  
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory"></property>  
    </bean>  
</beans>
二、springmvc.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">
	
	<!-- 配置扫描包 -->
	<context:component-scan base-package="com" /> 
	
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/views"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- <mvc:default-servlet-handler />   -->
	
	<!-- 配置开启注解 -->  
    <mvc:annotation-driven/> 
</beans>
三、database.properties配置

jdbc.user=scott
jdbc.password=tiger
jdbc.driverClass=oracle.jdbc.driver.OracleDriver
jdbc.jdbcUrl=jdbc:oracle:thin:@192.168.1.6:1521:ORCL

四、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>
  
  <!-- 配置Spring IOC容器 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <!-- 配置Spring监听 -->  
  <listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  </listener> 
  
      <!-- 配置SpringMVC -->  
  <servlet>  
    <servlet-name>springMVC</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <init-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:springmvc.xml</param-value>  
    </init-param>  
    <load-on-startup>1</load-on-startup>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>springMVC</servlet-name>  
    <url-pattern>/</url-pattern>  
  </servlet-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>  
    <init-param>  
        <param-name>forceEncoding</param-name>  
        <param-value>true</param-value>  
    </init-param>  
  </filter>  
  <filter-mapping>  
    <filter-name>encodingFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
  </filter-mapping>  
  
  <!-- 为了使用SpringMVC框架实现REST风格,需要配置  HiddenHttpMethodFilter-->  
    <filter>  
        <filter-name>hiddenHttpMethodFilter</filter-name>  
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>  
    </filter>  
    <filter-mapping>  
        <filter-name>hiddenHttpMethodFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
</web-app>

测试

一、数据源测试

public class ConnectionTest {

	private ApplicationContext ac = null;
	
	@Test
	public void DataSourceTest() throws SQLException {
		
		ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		DataSource dataSource = ac.getBean(DataSource.class);
		System.out.println("打开数据源" + dataSource.getConnection().toString());
		SessionFactory sessionFactory = ac.getBean(SessionFactory.class);

	}
}
二、dao类测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:springmvc.xml", "classpath*:applicationContext.xml" }) 
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
@Transactional
public class SpringDaoTest {

	@Autowired
	private IUserDao userDao;
	
	@Test
	public void QueryTest() {
		try {
			List<UserEntity> userEntityList  = userDao.queryAll();
			System.out.println(userEntityList.get(0).getUserId());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	
	}
}

问题列表

一、缺少dom4j-1.6.1.jar


二、缺少jboss-logging-3.1.1.GA


三、缺少jta-1.1


四、缺少hibernate-jpa-2.0-api-1.0.1.Final


五、缺少javassist-3.4.GA.jar


猜你喜欢

转载自blog.csdn.net/zgsxhdzxl/article/details/55656211