从零开始搭建SpringMVC+Spring+Hibernate环境

来源:互联网 发布:用友nc java 下载 编辑:程序博客网 时间:2024/06/05 19:00

以前在自己电脑上面撸代码时,感觉也没啥,因为对自己电脑很熟悉,而且也有以前的项目可以参考,现在到了公司,给你一台新电脑,从零开始搭建环境,并要成功把项目跑起来,突然感觉无从下手,配置过程中也出现许多错误,最后经过不懈努力,终于把新电脑的画风改为自己熟悉的模式了!

1.基本环境搭建

  • jdk下载(1.8版本)
    http://www.oracle.com/technetwork/java/javase/downloads/index.html
  • tomcat下载(9.0版本)
    http://tomcat.apache.org/
  • mysql下载(最新的5.7.20没有64位的,我安装一直出错,只好先安装个5.5版本的)
    https://dev.mysql.com/downloads/mysql/
    这里写图片描述
  • 最后就是其他工具的安装:IDEA(下载付费版,功能多,再破解),Navicat,Maven等。
    以上安装过程在其他大神博客里面都写的很详细,我这里也就不再重复写了

2.SpringMVC+Spring+Hibernate项目配置

  • 打开IDEA,新建SSH项目
    这里写图片描述
    最下面还勾选一个Hibernate
    这里写图片描述
    点击Next,选择项目存放的位置,再Finish,最后如下图
    这里写图片描述
    IDEA真的很智能,创建项目时就把所需jar包下载下来了,不需要自己手动导入。

  • 工具环境配置,例如添加tomcat等,其他文章里面也有详解

  • 重点来了,配置基本包结构
    这里写图片描述
    导入可能需要用到的jar包
    这里写图片描述

  • 核心:web.xml配置

    <!--加载spring配置文件-->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:/config/applicationContext.xml</param-value>    </context-param>    <!--上下文监听-->    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <!--加载SpringMVC配置文件-->    <servlet>        <servlet-name>dispatcher</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:/config/dispatcher-servlet.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>dispatcher</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>    <!--字符配置为UTF-8-->    <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>    <!--项目启动时,加载欢迎页-->    <welcome-file-list>        <welcome-file>/index.jsp</welcome-file>    </welcome-file-list>
  • 核心:applicationContext.xml文件配置
    <!--配置自动扫描的包-->    <context:component-scan base-package="com.demo">        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>    </context:component-scan>    <!--开启事务注解-->    <tx:annotation-driven/>    <!--加载属性文件-->    <context:property-placeholder location="classpath:/config/db.properties"/>    <!--导入数据源-->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="driverClass" value="${jdbc.driver}"/>        <property name="jdbcUrl" value="${jdbc.url}"/>        <property name="user" value="${jdbc.username}"/>        <property name="password" value="${jdbc.password}"/>    </bean>    <!--配置bean实例,若使用@Component注解就可以不用写-->    <bean id="user" class="com.demo.entity.User"/>    <!--配置jdbcTemplate-->    <!--配置 EntityManagerFactory-->    <bean id="entityManagerFactory"          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">        <property name="dataSource" ref="dataSource"/>        <!-- 配置 JPA 提供商的适配器. 可以通过内部 bean 的方式来配置 -->        <property name="jpaVendorAdapter">            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>        </property>        <!-- 配置实体类所在的包 -->        <property name="packagesToScan" value="com.demo.entity"/>        <!-- 数据库基本配置 -->        <property name="jpaProperties">            <props>                <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.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>            </props>        </property>    </bean>    <!-- 配置 JPA 使用的事务管理器 -->    <bean id="transactionManager"          class="org.springframework.orm.jpa.JpaTransactionManager">        <property name="entityManagerFactory" ref="entityManagerFactory"/>    </bean>    <!-- 配置支持基于注解是事务配置 -->    <tx:annotation-driven transaction-manager="transactionManager"/>

其中db.properties属性文件内容为:

#jdbc c3p0 configjdbc.driver = com.mysql.jdbc.Driverjdbc.url = jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=UTF-8jdbc.username = rootjdbc.password = admin#hibernate confighibernate.dialect = org.hibernate.dialect.MySQLDialecthibernate.show_sql = truehibernate.format_sql = falsehibernate.hbm2ddl.auto = update
  • 核心:dispatcher-servlet.xml文件配置(SpringMVC配置)
    <!--配置自动扫描的包-->    <context:component-scan base-package="com.demo.controller">        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>    </context:component-scan>    <!--配置视图解析器:把返回的方法解析为实际视图-->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/views/html/"/>        <property name="suffix" value=".jsp"/>    </bean>    <!--根路径加载静态资源文件-->    <mvc:resources location="/WEB-INF/views/css/" mapping="/views/css/**" />    <mvc:resources location="/WEB-INF/views/js/" mapping="/views/js/**" />    <mvc:resources location="/WEB-INF/views/img/" mapping="/views/img/**" />    <mvc:resources location="/WEB-INF/views/html/" mapping="/views/html/**" />    <!--两个必要配置-->    <mvc:default-servlet-handler/>    <mvc:annotation-driven/>
  • 属性文件配置已完成,下面就可以自己写代码测试SSH环境是否搭建成功

3.结果检验

这里写图片描述

这里写图片描述

这里写图片描述

行了,已完成!
革命尚未成功,大侠还需努力!

原创粉丝点击