zoukankan      html  css  js  c++  java
  • hibernate简单注释(一.1)

    ***********************************************************************************************************

    package com.ij34.web;

    import org.hibernate.*;
    import org.hibernate.boot.registry.*;
    import org.hibernate.cfg.*;
    import org.hibernate.service.*;

    import com.ij34.dao.New;

    public class test01 {
    public static void main(String[] args)throws Exception {
    //实例化Configuration
    Configuration conf=new Configuration()
    .addAnnotatedClass(com.ij34.dao.New.class)
    .setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver") //一定要加hibernate
    .setProperty("hibernate.connection.url","jdbc:mysql://localhost/hibernate") //一定要加hibernate
    .setProperty("hibernate.connection.username","root") //一定要加hibernate
    .setProperty("hibernate.connection.password","123456") //一定要加hibernate
    .setProperty("hibernate.dialect","org.hibernate.dialect.MySQL5InnoDBDialect") //一定要加hibernate
    .setProperty("show_mysql","true")
    .setProperty("hibernate.hbm2ddl.auto", "create"); //一定要加hibernate
    ServiceRegistry SR=new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
    // 以Configuration实例创建SessionFactory实例
    SessionFactory SF=conf.buildSessionFactory(SR);
    //create session
    Session session=SF.openSession();
    //start 事务
    Transaction tx=session.beginTransaction();
    New n=new New();
    n.setTitle("hello");
    n.setContent("2016 hello world 123456789");
    session.save(n);
    tx.commit();
    session.close();
    SF.close();
    }
    }

    ***************************************************************************************************

    package com.ij34.dao;
    import javax.persistence.*;
    @Entity
    @Table(name="test01")
    public class New {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String title;
    private String content;
    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 getContent() {
    return content;
    }
    public void setContent(String content) {
    this.content = content;
    }


    }

  • 相关阅读:
    ed新手使用进阶全功略
    directx的媒体对象
    Makefile详解
    Bourne Shell及shell编程(1)
    Win32CompileMSYSNew
    GNU hello学习笔记(1)——autoconf和automake
    VC++中通过MultiByteToWideChar将string|char*转换为wstring|wchar_t*
    使用 Bash shell 脚本进行功能测试[转]
    添加VLC模块
    libvlc外部api的简单整理
  • 原文地址:https://www.cnblogs.com/tk55/p/5960701.html
Copyright © 2011-2022 走看看