首页 技术 正文
技术 2022年11月14日
0 收藏 626 点赞 3,463 浏览 5778 个字

时隔半年,再次发布配置类的相关Blog,因为左手受伤原因先做一个简述。

首先利用idea创建一个Spring+SpringMVC+Hibernate项目,注意的是因为我们要完全放弃Hibernate以及SessionFactory配置文件,所以Hibernate不需勾选配置文件。

全部三个框架都要用Annotation的方式简化配置。

按我的个人习惯,把配置文件集中在web.xml和applicationContext.xml(集成Spring和SpringMVC)

目录结构:

Spring4托管Hibernate5并利用HibernateTemplate进行数据库操作

web.xml 几乎不需要修改,你也可以按自己的习惯调一下配置文件名,路径规则等。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</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>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

application.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"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.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.cielo.*"/>
<mvc:default-servlet-handler />
<mvc:annotation-driven />
<tx:annotation-driven transaction-manager="transactionManager"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.cielo.entity.HelloEntity</value>
<value>com.cielo.entity.TestEntity</value>
</list>
</property>
</bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/temp" />
<property name="username" value="cielo" />
<property name="password" value="hahaschool" />
</bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean></beans>

接下来我分别说明每一段的意义

<context:annotation-config />

加入此行可以引用@Transactional标签,后面讲解

<context:component-scan base-package="com.cielo.*"/>
<mvc:default-servlet-handler />
<mvc:annotation-driven />
<tx:annotation-driven transaction-manager="transactionManager"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

这段为SpringMVC配置文件,可以参考上一篇博客。

接下来我讲解一下用Spring托管Hibernate的流程。

众所周知,当仅使用Hibernate操作数据库时,如果仅仅读数据库,需要调用Session,而如果需要写数据库,则还需调用Transaction。在纯Hibernate中,Session来自于手动生成的SessionFactory。而Transaction则作为Session的一个成员。

事实上,对于Spring托管的Hibernate项目也是如此。如果我们想使用HibernateTemplate,则需要构建这个Bean并向其注入一个SessionFactory(或者用Annotation去注入,但当你有多个dao时,这并不简洁)。

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

而构建这个SessionFactory,我们需要注入数据源dataSource或者用一个configuration文件去注入。由于我们不想使用hibernate.cfg.xml,我们选择用dataSource注入。由于dataSource由jdbc提供,本身不能像hibernate配置文件那样全面,还好Spring提供了其他的property便于配置。

 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.cielo.entity.HelloEntity</value>
<value>com.cielo.entity.TestEntity</value>
</list>
</property>
</bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/temp" />
<property name="username" value="cielo" />
<property name="password" value="hahaschool" />
</bean>

最后,就是配置transaction了,我选择用Annotation的方法去配置,这样配置文件里就可以很简单

    <bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

当需要使用Transaction时,可以在方法名上标注@Transactional

一个使用例子:

@Repository
public class HelloDao{
@Autowired
public HibernateTemplate hibernateTemplate;
@Transactional
public void add(HelloEntity helloEntity){
hibernateTemplate.save(helloEntity);
}
}

关于hibernateTemplate的用法,可以直接查看接口定义,这样省去了大量的重复代码。

选择hibernateTemplate而不是直接继承HibernateSupportDao是为了保持耦合度不破坏,这也是目前更推荐的做法,实际上确实会相对麻烦一些。

最后放一个demo:HibernateTemplateDemo

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902