web整合Spring和Hibernate
Posted 大-雄
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了web整合Spring和Hibernate相关的知识,希望对你有一定的参考价值。
上一篇是简单整合web和Spring, 这一篇是整合hibernate:
连接池c3p0: spring5.0, hibernate5.0
jars:
-----------------------------
web.xml,增加高亮部分,不然会报
Could not obtain transaction-synchronized Session for current thread
如果不加,则需要在用的时候显示声明式事务:并且改为opensession:
Session s = t.getSessionFactory().openSession();
s.beginTransaction();
int id = (int) s.save(u);
s.getTransaction().commit();
具体参考:https://blog.csdn.net/caiwenfeng_for_23/article/details/45242545
http://www.yihaomen.com/article/java/466.htm
<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_3_0.xsd" version="3.0">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>SpringOpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SpringOpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.donghua.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/UserServlet</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</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" 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.xsd">
<bean id="userService" class="com.donghua.UserService">
<property name="name" value="Test Name"/>
</bean>
<!-- 一、配置数据库连接池 ComboPooledDataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- Connection properties -->
<property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="jdbcUrl" value="jdbc:sqlserver://localhost:1433;databaseName=test2" />
<property name="user" value="sa" />
<property name="password" value="sapassword" />
<!-- Pool properties-->
<property name="minPoolSize" value="2" />
<property name="maxPoolSize" value="10" />
<property name="maxStatements" value="50" />
<property name="idleConnectionTestPeriod" value="3000" />
<property name="loginTimeout" value="300" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<!-- 引入xxx.hbm.xml-->
<value>/META-INF/hbm/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props >
<!-- 配置hibernate的其他配置-->
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="userDao" class="com.donghua.UserDAO">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
---------------------UserSevlet
public class UserServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public UserServlet() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
WebApplicationContext con = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
UserService us = (UserService) con.getBean("userService");
response.getWriter().println(us.getName());
us.sayHello();
User u = new User("Allen"+System.currentTimeMillis(), 23);
UserDAO t = (UserDAO)con.getBean("userDao");
Session s = t.getSessionFactory().getCurrentSession();//若上面web.xml没有配置,此处应用opensession和声明式事务解决
int id = (int) s.save(u);
System.out.println(u);
User u2 = s.get(User.class, 29);
System.out.println(u2);
//User u3 = s.find(User.class, 1);
u = s.load(User.class, 29);
System.out.println(u);
}
}
----------------------user.hbm.xml
<hibernate-mapping package="com.donghua.model">
<class name="User" table="T_USER">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="name" column="NAME"/>
<property name="age" column="AGE"/>
</class>
<!-- CREATE TABLE T_USER(
ID int IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
NAME Varchar(40) NULL,
AGE INT NULL
PRIMARY KEY CLUSTERED
(
ID ASC
)
) -->
</hibernate-mapping>
浏览器输入则可以保存和查询User
以上是关于web整合Spring和Hibernate的主要内容,如果未能解决你的问题,请参考以下文章
JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)Spring整合web开发整合Junit4测试