Intelij idea中,Spring+SpringMVC+hibernate框架的搭建

1.文件目錄結構

2.spring-mvc.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: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-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
        
        
     <!-- springmvc 的配置文件,包括網站跳轉邏輯的控制 -->
	 <context:component-scan base-package="com.bjut.ssh.controller" use-default-filters="false">
	 	<!-- 默認所描所有,配置只掃描控制器 -->
	 	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	 </context:component-scan> 	
		
	<!-- 配置視圖解析器 方便頁面返回信息 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/View/"></property><!-- 前綴 -->
		<property name="suffix" value=".html"></property><!-- 後綴 -->
	</bean>

	<bean id="multipartResolver"
		  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 上傳文件大小上限,單位爲字節(10MB) -->
		<!--<property name="maxUploadSize">-->
			<!--<value>10485760</value>-->
		<!--</property>-->
		<!--&lt;!&ndash; 請求的編碼格式,必須和jSP的pageEncoding屬性一致,以便正確讀取表單的內容,默認爲ISO-8859-1 &ndash;&gt;-->
		<!--<property name="defaultEncoding">-->
			<!--<value>UTF-8</value>-->
		<!--</property>-->
		<property name="defaultEncoding" value="UTF-8" />
		<!-- 指定所上傳文件的總大小不能超過10485760000B。注意maxUploadSize屬性的限制不是針對單個文件,而是所有文件的容量之和 -->
		<property name="maxUploadSize" value="10485760000"></property>

		<property name="maxInMemorySize" value="40960"></property>
	</bean>

	<!-- 兩個標準配置 -->
	<!-- 將springMVC不能處理的請求交給tomcat -->
	<mvc:default-servlet-handler/>
	<!-- 能支持springMVC更高級的一些功能,JSR303校驗,快捷的ajax..映射動態請求  -->
	<!--<mvc:annotation-driven />-->
	<mvc:annotation-driven></mvc:annotation-driven>



</beans>      

3.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"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <!-- spring 的配置文件,這裏主要配置和業務邏輯有關 -->
    <!-- 數據源 ,事務控制 -->

    <context:component-scan base-package="com.bjut">
        <!-- 不掃描controller,其他都掃描-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


    <!--********************************************配置hibernate********************************************-->

    <!--掃描配置文件(這裏指向的是之前配置的那個config.properties)-->
    <context:property-placeholder location="classpath:/jdbc.properties" />

    <!--配置數據源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${jdbc.driver}" />  <!--數據庫連接驅動-->
        <property name="jdbcUrl" value="${jdbc.url}" />     <!--數據庫地址-->
        <property name="user" value="${jdbc.username}" />   <!--用戶名-->
        <property name="password" value="${jdbc.password}" />   <!--密碼-->
        <property name="maxPoolSize" value="40" />      <!--最大連接數-->
        <property name="minPoolSize" value="1" />       <!--最小連接數-->
        <property name="initialPoolSize" value="10" />      <!--初始化連接池內的數據庫連接-->
        <property name="maxIdleTime" value="20" />  <!--最大空閒時間-->
    </bean>


    <!--配置session工廠-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.bjut.ssh.entity" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <!--hibernate根據實體自動生成數據庫表-->
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>   <!--指定數據庫方言-->
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>     <!--在控制檯顯示執行的數據庫操作語句-->
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/device</prop>
                <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>     <!--在控制檯顯示執行的數據哭操作語句(格式)-->
            </props>
        </property>

    </bean>

    <!-- 事物管理器配置  -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

4.hibernate.cfg.xml

<?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">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/device</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
        <!--<property name="hibernate.binpath">E:\mysql-5.6.24-winx64\bin</property>-->
        <!--<property name="hibernate.connection.autocommit">true</property>-->

        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">false</property>
        <mapping resource="com/ssh/entity/xxxx.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

5.jdbc.properties

#database connection config
jdbc.url = jdbc:mysql://localhost:3306/device?characterEncoding=utf8&useSSL=true
jdbc.driver = com.mysql.jdbc.Driver
jdbc.username = root
jdbc.password = hao

#hibernate config
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto = update

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章