程序员

使用 Spring + SpringMVC + Hibernat

2018-03-18  本文已影响0人  Winnndy

一、搭建开发环境

1、下载并安装MyEclipse

本项目使用MyEclipse 2017 CI(以下简称me),在这里推荐新手入门使用,因为me支持自动引包、向工程中添加框架等功能,避免了手动搭建框架时会遇到的一些问题。MyEclipse官方中文网,可以在这个链接中下载最新版的me。需要注意的是me并不是免费的,只有三十天的试用期,破解方法可自行百度,在第一次打开前进行破解即可,或者也可以在某宝搜索相关关键词,价格也十分低廉。另外me是自带jdk的,可以不用自己下载安装jdk,如果本机已安装jdk,一定要注意在进行项目配置时要使用统一版本的jdk。

2、下载并安装Tomcat(可选)

本项目使用Tomcat这个免费开源的web应用服务器,me本身也自带了各个版本的Tomcat,可以选择使用自带的,也可以为了方便自己管理配置等使用自行下载的Tomcat。下载地址:Apache Tomcat 本项目为了方便使用的是me自带的Tomcat v8.5。

3、搭建项目框架

具体过程如下所示:

(1)首先新建一个名为mybbs的web project,使用默认设置,连续点击两次next

(2)勾选自动生成web.xml文件选项后点击finish

(3)右键项目,在菜单中找到Configure Facets,点击Install Spring Facet,连点两次next后勾选Spring Persistence后点击finish完成Spring框架基本配置

(4)右键项目,在菜单中找到Configure Facets,点击Install Hibernate Facet,Hibernate版本选择4.1,点击next,取消勾选Create SessionFactory class,再次点击next

(5)配置数据库连接信息,这里我使用的数据库是mysql,可根据自己具体所使用的数据库进行配置,完成后点击finish完成Hibernate框架基本配置

(6)下载commons-logging.jar并引入该项目,下载mysql-connector-java-5.1.45-bin.jar并放到项目中的WebRoot/WEB-INF/lib路径下

(7)打开WebRoot下自动生成的index.jsp,点击运行将其部署到Tomcat上,如果还没有安装Tomcat,可以选择manually define a new server自行创建后使用


运行结果如下:


(8)配置web.xml文件

在controller与jsp文件、controller与数据库之间传递数据的时候,经常会出现中文乱码的情况,原因是部分默认编码格式(如iso等)不支持中文编码,所以我们需要在web.xml中添加一个filter过滤器,将传递的数据格式设置为中文,代码如下:

 <filter>  
        <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
  </filter-mapping>

在自动生成的web.xml文件中,context的配置如下:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

而实际上,自动生成的applicationContext.xml文件在src文件夹中,因此为了方便使用,我们先将applicationContext.xml文件从src文件夹中移动到WEB-INF下,再将上述代码改为:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>

接下来定义一个名为bbs的DispatcherServlet用来处理所有的 HTTP 请求和响应:

<servlet>
        <servlet-name>bbs</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
        <servlet-name>bbs</servlet-name>
        <url-pattern>/</url-pattern>
</servlet-mapping>

在管理用户登陆状态时会用到session,在这里我将超时时间设置为30分钟,也可以根据实际需求进行修改:

<session-config>
        <session-timeout>30</session-timeout>
</session-config>

现在web.xml就已经配置完成了,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>mybbs</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <filter>  
        <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
  </filter-mapping>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <servlet>
        <servlet-name>bbs</servlet-name>
        <servlet-class>
           org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>bbs</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>

(9)配置applicationContext.xml文件

打开文件后点击下方Namespaces标签,勾选context, jdbc, 和mvc后Crtl + S自动生成相关代码,如下图所示:



自动生成的代码:

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">


    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="url" value="jdbc:mysql://localhost:3306/mydb"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
            </props>
        </property>
    </bean>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

添加代码:
<context:annotation-config/>
激活那些已经在 spring 容器里注册过的 bean上面的注解;
添加代码:
<context:component-scan base-package="com.sxy.*"/>
在指定的 package 下扫描以及注册 javabean;
添加代码:

<mvc:annotation-driven>  
        <mvc:message-converters register-defaults="true">  
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">  
                <constructor-arg value="UTF-8" />  
            </bean>  
        </mvc:message-converters>  
</mvc:annotation-driven>

解决 @Controller 注解使用的前提配置和一些情况下的中文乱码问题;
在dataSource中添加属性driverClassName:
<property name="driverClassName" value="com.mysql.jdbc.Driver" ></property>
在transactionManager中添加属性dataSource:
<property name="dataSource" ref="dataSource"/>;
添加代码:

<bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        primary="false">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
</bean>

来抓取所有访问WebRoot下的JSP文件的请求;
现在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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">       
    <context:annotation-config/>
    <context:component-scan base-package="com.sxy.*"/>
    <mvc:annotation-driven>  
        <mvc:message-converters register-defaults="true">  
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">  
                <constructor-arg value="UTF-8" />  
            </bean>  
        </mvc:message-converters>  
    </mvc:annotation-driven>  
    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="url" value="jdbc:mysql://localhost:3306/mydb"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver" ></property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
            </props>
        </property>
    </bean>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        primary="false">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>
上一篇下一篇

猜你喜欢

热点阅读