前言:
1. 用于配置Spring的 .xml 插件
- 为了编写时有一些提示、自动生成一些配置信息,可以给 eclipse增加支持 spring的插件:spring tool suite,https://spring.io/tools/sts/all,下载然后安装(不推荐这种方式,容易出错)
插件版本尽量保持与 eclipse版本一致。如果插件版本没有与eclipse一样的,那么更新eclipse对应的版本
- 直接下载sts工具(相当于一个集合了spring tools的Eclipse):https://spring.io/tools/sts/
用法与eclipse一样
-
预览
2. 用于配置hibernate的 .xml 插件 安装教程
下载地址:http://download.jboss.org/jbosstools/updates/stable/kepler/
-
预览
Spring 与 SpringMVC的关系
Spring:Spring 是一个轻量级的控制反转( IOC )和面向切面( AOP )的容器框架。
SpringMVC:SpringMVC 属于 SpringFrameWork 的后续产品,已经融合在 Spring Web Flow 里面。 Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
Spring是一个父容器,SpringMVC是一个子容器。
在Web开发中,Controller全部在SpringMVC中扫描,除了Controller以外的Bean,全部在Spring中扫描(例如:Service、Dao等)
Spring中扫描Service、Dao以及其他组件,数据源定义、SessionFactory、事务定义[事务属性,事务切入点,声明式事务等]等 在Spring容器中配置
Controller在SpringMVC中扫描,视图解析器、静态资源访问、JSR303校验、Aiax、文件上传等 在SpringMVC中配置
1、加入Spring
-
Spring所用的 jar包
spring-aop 开发aop特性
spring-beans 处理bean
spring-context 处理上下文
spring-core 核心jar
spring-expression spring表达式
commons-loggin 日志
-
1.1 加入jar包 : 下面是整合所需要用到的jar包 (包括spring, c3p0, hibernate, logging, jackson等)
-
1.2 配置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>SSHProject</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> <!-- 1、启动 spring 的容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 2、SpringMVC的前端控制器,拦截所有请求 --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 3、字符编码过滤器,一定要放在所有过滤器之前 --> <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>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
-
dp.propeties配置文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://localhost:3306/sshtest jdbc.username=root jdbc.password= jdbc.initialPoolSize=5 jdbc.maxPoolSize=10 #...还需什么的自己百度进行配置吧,这里就不继续写了
2、加入Hibernate
-
1). 同时建立持久化类,和其对应的 .hbm.xml文件,生成对应的数据表
-
2). Spring整合Hibernate
-
3). 步骤 :
-
1. 加入jar包 (前面已加入)
-
在类路径下加入 hibernate.cfg.xml文件,在其中配置hibernate的基本属性
<?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> <!-- 配置hibernate 的基本属性 --> <!-- hibernate 所使用的数据库方言 --> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- 执行操作时是否在控制台打印SQL --> <property name="show_sql">true</property> <!-- 是否对 SQL 进行格式化 --> <property name="format_sql">true</property> <!-- 指定自动生成数据表的策略 --> <property name="hbm2ddl.auto">update</property> <!-- 指定关联的 .hbm.xml 配置文件 --> <mapping resource="com/ssh/entity/Department.hbm.xml"/> <mapping resource="com/ssh/entity/Employee.hbm.xml"/> </session-factory> </hibernate-configuration>
-
建立持久化类,和其对应的 .hbm.xml文件
如果这里看不了GIF,请在线看 https://i.loli.net/2020/03/11/Dz3UVaxBnk5bKGh.gif
<id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <!-- 主键生成策略: increment:自动增长; assigned:程序提供; native:由程序自己判断是程序提供主键值还是由数据库生成主键; identity:使用数据的自动增长.如mysql的auto_increment; sequence:使用序列. -->
<property name="departmentName" type="java.lang.String"> <column name="DEPARTMENT_NAME" /> </property> <!-- property中的元素属性: name:实体类中的属性名称; type:实体类中对应属性的数据类型; column:数据库中表对应的列名. -->
-
-
1. 和Spring进行整合
-
4.1 加入 c3p0 和 mysql驱动
-
4.2 在 Spring的配置文件中配置:数据源,SessionFactory,声明式事务
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 1、在Spring容器中,扫描除了Controller之外的其他组件 --> <context:component-scan base-package="com.tgb.web"> <!-- 不扫描Controller注解 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 2.1、加载 db.properties 配置文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 2.2、配置C3P0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 配置连接数据库的基本信息 --> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean> <!-- 3、配置Hibernate的SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <property name="mappingLocations" value="classpath:com/ssh/entity/*.hbm.xml"></property> </bean> <!-- 4、配置 Spring的声明式事务 --> <!-- 4.1 配置 hibernate的事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 4.2 配置事务属性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 4.3 配置事务切入点,再把事务属性和事务切入点关联起来 --> <aop:config> <aop:pointcut expression="execution(* com.ssh.service.*.*(..))" id="txPointcut"/> <aop:advisor advice-ref="txPointcut" pointcut-ref="txPointcut"/> </aop:config> </beans>
-
-
启动项目,会看到生成对应的数据表
3、加入SpringMVC
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 配置扫描器: 扫描有注解的包 -->
<context:component-scan base-package="com.tgb.web.controller"></context:component-scan>
<!-- 配置试图解析器(InternalResourceViewResolver) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--
解决静态资源访问:该注解会让 springmvc: 接收一个请求,并且该请求没有对应的 @requestMapping 时 ,
将该请求交给服务器默认的 servlet(也就是tomcat)去处理(直接访问)
-->
<mvc:default-servlet-handler />
<!--
此配置是 springmvc 的基础配置,很多功能都需要通过该注解来协调。
如JSR303校验,快捷ajax请求等...映射动态请求
-->
<mvc:annotation-driven/>
<!--
配置 CommonsMultipartResolver,用于实现文件上传 ,将其加入 springIOC 容器
springIOC 容器在初始化时,会自动寻找一个 id="multipartResolver" 的 bean 并将其加到 IOC 容器
-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 上传单个文件的最大值,单位是 byte。这里设置为5M -->
<property name="maxUploadSize" value="5242880"></property>
<property name="maxInMemorySize" value="4096" />
</bean>
</beans>
4、完成功能
Spring+SpringMVC+Hibernate整合完成
Spring+Hibernate整合学习: https://www.bilibili.com/video/av27201253