1.在myeclipse以下创建一个javaproject或者webproject,我创建的时webproject,用的myeclipse2013

2.导入spring的依赖包

3.导入hibernate的依赖包

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvajkwMzgyOTE4Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

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语句

最新文章

  1. Windows平台手动卸载Oracle Server【完整+干净】
  2. Android 沉浸式状态栏 实现方式一
  3. hdu 2795 线段树(纵向)
  4. TCP移动端跟服务器数据交互
  5. 使用JAP(基类)父类注解
  6. bnuoj 4207 台风(模拟题)
  7. 1015. Reversible Primes (20)
  8. PAT-乙级-1022. D进制的A+B (20)
  9. 【MFC初学】
  10. hive on spark的安装及问题
  11. python 函数返回值(总结)
  12. 我面试过没有上万人也有十几个,简历要这么写才有hr要你
  13. Selenium WebDriver中鼠标事件
  14. 我是怎样和Linux系统结缘并通过红帽RHCE认证的
  15. hdu6441 Find Integer (费马大定理)
  16. 解决微信小程序要求的TLS版本必须大于等于1.2的问题
  17. hive元数据研究
  18. ICE简介及C++程序例子(转)
  19. Altium Ddesigner 栅格 含义
  20. ansible debug模块学习笔记

热门文章

  1. 手机对支持128G扩展内存的介绍
  2. soapui icon以及resource的理解
  3. 国内物联网平台初探(六) ——庆科云FogCloud
  4. SwiftUI 官方教程
  5. Super超级ERP系统---(3)基础信息管理--商品管理
  6. LayoutInflater源码解析
  7. Codeforces Round #453
  8. error:informix Unable to load translation shared library 解决方案
  9. Spring aop(实验写法)
  10. Thread pool引起的程序连接数据库响应慢