关键词:
1.在myeclipse以下创建一个javaproject或者webproject,我创建的时webproject,用的myeclipse2013
2.导入spring的依赖包
3.导入hibernate的依赖包
4.在src文件夹以下配置hibernate的核心文件hibernate.cfg.xml
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate‘s automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<!-- <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> -->
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="org/hibernate/domain/Event.hbm.xml"/>
<!-- annotation的配置方式 -->
<mapping class="org.hibernate.domain.Teacher"/>
</session-factory>
</hibernate-configuration>
5.在WEB-INFO文件夹以下配置spring的核心配置文件applicationContext
<?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.xsd">
<!-- <bean id="..." class="..."> collaborators and configuration for this
bean go here </bean> <bean id="..." class="..."> collaborators and configuration
for this bean go here </bean> more bean definitions go here -->
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="DaoImp" class="com.persistent.DaoImp">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
6.编写持久化类
package org.hibernate.domain;
import javax.persistence.Entity;
import javax.persistence.Id;
//定义一个实体类
@Entity
public class Teacher {
private int id;//id
private String title;//职场
private String name;//姓名
//定义一个主键
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
7.编写保存之久话的类和方法
package com.persistent;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.domain.Teacher;
public class DaoImp {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void save(){
Session session=sessionFactory.getCurrentSession();
session.beginTransaction();
Teacher teacher = new Teacher();
teacher.setId(6);
teacher.setName("hibernate!!");
teacher.setTitle("we will");
session.save(teacher);//利用annotation进行插入操作
session.getTransaction().commit();
}
}
8.在hibernate.cfg.xml文件里配置
<!-- annotation的配置方式 -->
<mapping class="org.hibernate.domain.Teacher"/>
9.进行測试
package com.demo.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.persistent.DaoImp;
public class TestMain2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context =
new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");
/*Hello hello=(Hello) context.getBean("hello");
hello.display();*/
DaoImp di = (DaoImp)context.getBean("DaoImp");
di.save();
}
}
10.能够看查看数据库,已经有数据了,控制台也打印出了sql语句
spring4.*中整合hibernate
1.Spring整合Hibernate整合什么?1).有IOC容器来管理Hibernate的SessionFactory2).让Hibernate使用上Spring的声明式事务2.整合步骤:1).加入hibernate①.jar包②.添加hibernate的配置文件:hibernate.cfg.xml③.编写了持久化类对应的.hbm.xml文件。2).加入Spring①.j... 查看详情
spring4整合hibernate5以及出现的问题解决办法
每一次的学习,都是一小步一小步的进行的,学习语言,重要的是能把helloworld写出来以及在学习过程中出现的问题能够及时的记录并总结spring目前最新的版本是4.3,而hibernate是5.2最新版本的学习是要花费很大的勇气进行的... 查看详情
mybatis3+spring4+springmvc4整合
...1 创建一个Web项目。 2 导入Mybatis3、Spring4、SpringMVC4、连接数据库(我使用的数据库是mysql)的jar包。 我所用的包:650)this 查看详情
深入浅出jms--spring和activemq整合的完整实例
前言这篇博文,我们基于spring+JMS+ActiveMQ+Tomcat,做一个Spring4.1.0和ActiveMQ5.11.1整合实例,实现了Point-To-Point的异步队列消息和PUB/SUB(发布/订阅)模型,简单实例,不包含任何业务。环境准备工具JDK1.6或1.7Spring4.1.0ActiveMQ5.11.1Tomcat7.x目... 查看详情
spring4.0+struts2整合,服务器启动时发生的异常
Nomappingfoundfordependency[type=java.lang.String,name=‘struts.objectFactory.spring.enableAopSupport‘]inpublicorg.apache.struts2.spring.StrutsSpringObjectFactory(java.lang.String,java.lang.String,java 查看详情
spring4.3.7整合mongodb3.2.1
前几天写了个mongodb原生的工具类,参考“http://ylcodes01.blog.51cto.com/5607366/1933342”,项目里需要做分布式,所以现在集成到spring中,今天结合spring-mongodb写了一些常用的工具。650)this.width=650;"src="https://s4.51cto.com/wyfs02/M01/98/96/wKioL1k-Xu... 查看详情
springmvc4+spring4+hibernate4框架整合(代码片段)
SpringMVC4+Spring4+Hibernate4框架搭建中。。。作者:vashon时间:2016-09-03前言现在很多企业流行用mybatis+spring+springmvc框架,但是mybatis或ibatis编写SQL较麻烦且对于数据库的移植性不好,你... 查看详情
javaee框架——mybatis和spring整合
...#xff1f;我为大家演示一下简单的交互过程。整合版本:spring4.3.2和 查看详情
mybatis学习总结——mybatis3.x与spring4.x整合
一、搭建开发环境1.1、使用Maven创建Web项目 执行如下命令:mvnarchetype:create-DgroupId=me.gacl-DartifactId=spring4-mybatis3-DarchetypeArtifactId=maven-archetype-webapp-DinteractiveMode=false创建失败:Maven命令行创建项目时Couldnotf 查看详情
[转]mybatis3.x与spring4.x整合
原文地址:http://www.cnblogs.com/xdp-gacl/p/4271627.html一、搭建开发环境1.1、使用Maven创建Web项目 执行如下命令:mvnarchetype:create-DgroupId=me.gacl-DartifactId=spring4-mybatis3-DarchetypeArtifactId=maven-archetype-webapp-Dint 查看详情
mybatis3.x与spring4.x整合
一、搭建开发环境1.使用maven创建web项目2.编辑pom.xml:<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 查看详情
eclipse整合spring4+struts2.5+hibernate4.2
本次搭建的SSH项目源码已上传到百度云盘。没有使用MAVEN,下载下来,在applicationContext配置下mysql,执行下test.sql脚本就可以运行了。链接:http://pan.baidu.com/s/1nvqOcPj密码:yv19 1.配置Struts2 a.拷贝Struts2jar包 b.设置JSP编码... 查看详情
j2ee开发框架搭建-springmvc4+spring4+hibernate4整合
1.打开hqhop-framework-parent项目下的pom.xml文件。加入springmvc4,spring4,hibernate4,以及数据源druid的依赖包,插件,依赖包版本<!--datasource相关jar?--><dependency> <groupId>com.alibaba</groupId> <artifactId 查看详情
ssh整合(代码片段)
我这里使用的是spring4.2.2+struts2的2.3.24+hibernate5.0.7进行的SSH整合注意:struts2和hibernate的核心jar包中都有一个jar包版本不一样叫javassist这个会导致报错 需要删掉一个 导入jar包什么的就不说了直接说说配置 首先spring配置文... 查看详情
struts2.3.34+hibernate4.x+spring4.x整合二部曲之下部曲
1导入jar包本文最后会给出项目的地址,各位无须看急。 2配置web.xml文件<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:sch 查看详情
struts2.3.15+hibernate4.x+spring4.x整合二部曲之上部曲
1导入jar包可以复制jar包或maven导入,本文最后会给出github地址 2导入log4j.properties文件og4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.errlog4j.appender.stdout.layout=org.apache.lo 查看详情
sshstruts2.3+spring4.3+hibernate3整合(代码片段)
新建webproject工程目录如下图(包括:包结构;配置文件,测试包)src:(源文件,domain,dao,service,aciton)test(主要放置junit测试类)config文件(主要包含配置文件)目的:以上各文件分类的目的是为了各层之间结构简单... 查看详情
springboot框架的搭建
一、优点:1.简化了配置,是基于Spring4的一套快速开发整合包,减少复杂度而SpringMVC基于Spring的一个MVC框架2.会有一个statrter整合包,减少样板代码3.自动配置Spring4.开箱即用,没有代码生成,也无需xml配置具体:你没有做任... 查看详情