搭建SpringMVC+Hibernate

1.首先加入相關的jar包,配置web.xml文件,以下:java

<?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" 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>springMVC_REST_hibernate</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 引入dispatcherServlet -->
  <servlet>
     <servlet-name>mySpringMVC</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <!--引用springMVC的配置文件-->
     <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springMVC.xml</param-value>
    </init-param>
  </servlet>
  
  <servlet-mapping>
     <servlet-name>mySpringMVC</servlet-name>
     <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- 亂碼過濾器,只是處理post請求 -->
  <filter>
    <filter-name>charEncoding</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>charEncoding</filter-name>
    <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <!-- 對於put和delete是須要經過post請求轉換 -->
 <filter>
   <filter-name>to_method</filter-name>
   <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
 </filter>
 <filter-mapping>
 <filter-name>to_method</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>

2.配置springMVC.xml文件(springMVC.xml)mysql

<?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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 掃包 -->
<context:component-scan base-package="com.ysq"/>

<!-- 視圖轉換器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <!-- 跳轉的url的前綴和後綴 -->
   <property name="prefix" value="/WEB-INF/jsp/"></property>
   <property name="suffix" value=".jsp"></property>
</bean>
<!-- 直接訪問WEB-INF下的文件 -->
<mvc:view-controller path="/toaddUser" view-name="addUser"/>
<!-- 訪問靜態資源 -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>

</beans>

3.配置hibrenate配置文件,並導入相關的sql信息(hibernate.cfg.xml)web

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
   <!-- 設置數據庫鏈接 -->
  <!--  <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
   <property name="hibernate.connection.username">scott</property>
   <property name="hibernate.connection.password">tiger</property>
   <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> -->
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
  
  <!-- 設置方言 -->
   <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
   <!--  <property name="dialect">org.hibernate.dialect.OracleDialect</property> -->
  <!-- 設置經常使用的屬性 -->
  <property name="show_sql">true</property>
  <property name="format_sql">true</property>
  
  <!-- 引用 -->
  <mapping resource="com/ysq/vo/user.hbm.xml"/>
  
  </session-factory>



</hibernate-configuration> 

4.配置實體類的mapper文件(user.hbm.xml)spring

<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping package="com.ysq.vo">
   
   <class name="User" table="users">
      <id name="uid">
      <generator class="native"/>
      </id>
      <property name="name" column="uname"/>
      <property name="password"/>
   
   </class>
   
</hibernate-mapping>

5.建立鏈接sessionFactory類sql

package com.ysq.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class SessionFactoryUtils {
	
	private static SessionFactory sessionFactory;
	
	private SessionFactoryUtils() {
	}
	
	static{
		Configuration config = new Configuration().configure("hibernate.cfg.xml");
		sessionFactory =  config.buildSessionFactory();
	}
	
	public static Session getSession(){
		 
		return sessionFactory.openSession();
	}
	
	public static void main(String[] args) {
		System.out.println(getSession()== null);
	}

}

6.以上就是集成的步驟,若是須要源碼請在下方留言,我會及時查收。數據庫

相關文章
相關標籤/搜索