Spring+hibernate 简单整合代码
来源:互联网 发布:华为sim卡网络解锁nck 编辑:程序博客网 时间:2024/05/16 00:27
1.SessionFatory注入 spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">--
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/demo">
</property>
<property name="username" value="caterpillar"></property>
<property name="password" value="123456">
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
destroy-method="close">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/po/User.hbm.xml</value>
</list>
</property>
</bean>
<bean id="userDAO" class="com.po.UserDAO">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
/*
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
</property>
<property name="url"
value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs">
</property>
<property name="username" value="sa"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/po/Dep.hbm.xml</value>
<value>com/po/Emp.hbm.xml</value>
<value>com/po/Emplove.hbm.xml</value>
<value>com/po/Love.hbm.xml</value>
</list>
</property>
</bean>
<bean id="EmploveDAO" class="com.po.EmploveDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
*/
</beans>
2.SpringHibernateDemo.java文件
package com.po;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringHibernateDemo
{
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
IUserDAO userDAO = (IUserDAO)context.getBean(userDAO);
User user = new User();
user.setName("accp");
user.setAge(new Integer(20));
userDAO.insert(User);
user = userDAO.find(new Integer(1));
System.out.println("name:"+user.getName());
}
}
3.HibernateTemplate UserDAO.java文件
package com.po;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.hibernateTemplate;
public class UserDAO implements IUserDAO
{
private HibernateTemplate hibernateTemplate;
public void setSessionFactory(SessionFactory sessionFactory){
hibernateTemplate = new HibernateTemplate(sessionFactory)
}
public void insert(User user){
hibernateTemplate.save(user);
}
public User find(Integer id){
User user = (User) hibernateTemplate.get(User.class,id);
return user;
}
}
- Spring+hibernate 简单整合代码
- spring+hibernate简单整合
- Spring整合Hibernate简单示例
- Spring整合Hibernate简单示例
- spring + hibernate + annotation 简单整合
- **spring+hibernate + c3p0 代码整合 **
- struts spring hibernate整合简单实现
- Struts2 Spring Hibernate 的简单整合
- Struts+Spring+Hibernate整合的简单实例
- SpringMVC+Spring+Hibernate 简单的整合
- 简单Spring MVC + Hibernate 环境搭建整合
- hibernate和spring的简单整合
- 简单的ssh(spring+springmvc+hibernate)整合
- spring与Hibernate整合代码截图
- Struts2+Hibernate+Spring 整合示例==》Spring整合Struts2简单示例==》Spring整合Hibernate简单示例
- Spring:Spring整合Hibernate
- 【Spring】Spring+hibernate整合
- Eclipse + MyEclipse整合Struts+Spring+Hibernate简单例子开发
- Photoshop菜单中英文对照
- C++程序员转行Symbian开发测试题
- MFC窗口的颜色和字体的设置
- 地磅称量系统之(1)创建空白解决方案
- Delphi7学习(数据库连接操作)
- Spring+hibernate 简单整合代码
- 学习IEEE1588 Precision Time Protocol V2
- 美华裔青年创办社交网站获主流媒体关注(图)
- 使用 javascript HTML DOM 高亮显示页面特定字词
- .net反射简介
- evc vc字符串转换处理一:(绝对精华,收集所有的例子)
- Java web start
- 在linux下设置开机自动启动程序的方法
- 提高jsp程序的运行效率