搭建SpringMVC+Hibernate

1.首先加入相关的jar包,配置web.xml文件,如下:

<?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)

<?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)

<?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)

<?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类

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.以上就是集成的步骤,如果需要源码请在下方留言,我会及时查收。

上一篇:(原创)使用C#开发高性能PLC上位机监控系统服务器应用程序


下一篇:通过监控线程状态来保证socket服务器的稳定运行