• Hibernate个人学习笔记(2)


    新增改查的操作

    一、cfg.xml配置

    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

    <!-- Generated by MyEclipse Hibernate Tools. -->
    <hibernate-configuration>

    <session-factory>
    <property name="dialect">
    org.hibernate.dialect.MySQLDialect
    </property>
    <property name="connection.url">
    jdbc:mysql://localhost:3306/test
    </property>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>
    <property name="connection.driver_class">
    com.mysql.jdbc.Driver
    </property>
    <property name="myeclipse.connection.profile">
    com.mysql.jdbc.Driver
    </property>

    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.format_sql">true</property>

    <mapping resource="com/neusoft/Dao.hbm.xml" />
    </session-factory>

    </hibernate-configuration>

    二、先实例化对象

    public class User {
    private int id;
    private String username;
    private String password;
    private String nicheng;

    public User() {
    super();
    // TODO Auto-generated constructor stub
    }

    public User(int id, String username, String password, String nicheng) {
    super();
    this.id = id;
    this.username = username;
    this.password = password;
    this.nicheng = nicheng;
    }

    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    public String getUsername() {
    return username;
    }

    public void setUsername(String username) {
    this.username = username;
    }

    public String getPassword() {
    return password;
    }

    public void setPassword(String password) {
    this.password = password;
    }

    public String getNicheng() {
    return nicheng;
    }

    public void setNicheng(String nicheng) {
    this.nicheng = nicheng;
    }

    }

    三、配置实例化配置文件

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!--
    Mapping file autogenerated by MyEclipse Persistence Tools
    -->
    <hibernate-mapping>
    <class name="com.neusoft.user" table="user" catalog="test">
    <id name="id" type="java.lang.Integer">
    <column name="id" />
    <generator class="assigned"></generator>
    </id>
    <property name="username" type="java.lang.String">
    <column name="username" length="10" />
    </property>
    <property name="password" type="java.lang.String">
    <column name="password" length="10" />
    </property>
    <property name="nicheng" type="java.lang.String">
    <column name="nicheng" length="20" />
    </property>
    </class>
    </hibernate-mapping>

    四、session 工具类

    package com.neusoft.HibernateTest;

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.tool.hbm2ddl.SchemaExport;


    public class UserUtils {
    //读取Hibernate.cfg.xml文件
    private static SessionFactory factory;
    static{
    try{
    //读取hibernate.cfg.xml文件
    Configuration cfg = new Configuration().configure();
    //建立SessionFactory
    factory = cfg.buildSessionFactory();
    }catch(Exception e){
    e.printStackTrace();
    }
    }

    //打开Session
    public static Session getSession(){
    return factory.openSession();
    }

    //关闭Session
    public static void closeSession(Session session){
    if(session != null ){
    if(session.isOpen()){
    session.close();
    }
    }
    }

    public static SessionFactory getSessionFactory(){
    return factory;
    }
    }

    五:

    新增

    public class Save extends TestCase{
    //新增
    public void test(){


    Session session = null;
    try {
    session = UserUtils.getSession();
    session.beginTransaction();
    Dao dao = new Dao();
    dao.setUsername("qwe");
    dao.setPassword("asd");
    dao.setNicheng("zxc");
    session.save(dao);
    session.getTransaction().commit();
    } catch (Exception e) {
    e.printStackTrace();
    session.getTransaction().rollback();
    }finally{
    UserUtils.closeSession(session);
    }
    }
    }

    查询:

    public class Load extends TestCase{
    //查詢
    public void test(){
    Session session = null;
    try {
    session = UserUtils.getSession();
    session.beginTransaction();
    Dao dao = (Dao) session.load(Dao.class, 1);
    System.out.println(dao.getUsername());
    System.out.println(dao.getPassword());
    System.out.println(dao.getNicheng());
    session.getTransaction().commit();
    } catch (Exception e) {
    e.printStackTrace();
    session.getTransaction().rollback();
    }finally{
    UserUtils.closeSession(session);
    }
    }
    }

    删除:

    public class Delete extends TestCase{
    //刪除
    public void test(){
    Session session = null;
    try {
    session = UserUtils.getSession();
    session.beginTransaction();
    Dao dao = (Dao) session.load(Dao.class, 1);
    session.delete(dao);
    session.getTransaction().commit();
    } catch (Exception e) {
    e.printStackTrace();
    session.getTransaction().rollback();
    }finally{
    UserUtils.closeSession(session);
    }
    }
    }

    修改:

    public class Undate extends TestCase {
    //修改
    public void test(){
    Session session = null;
    try {
    session = UserUtils.getSession();
    session.beginTransaction();
    Dao dao = (Dao) session.load(Dao.class, 1);
    dao.setUsername("lll");
    session.update(dao);
    session.getTransaction().commit();
    } catch (Exception e) {
    e.printStackTrace();
    session.getTransaction().rollback();
    }finally{
    UserUtils.closeSession(session);
    }
    }
    }


    作者:冬瓜茶饮料
    出处:http://www.cnblogs.com/dongguacha/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    java窗口按钮位置设置
    使用java语言编写窗口按钮
    添加无参的构造方法
    冒泡排序
    多态
    首页列表显示全部问答,完成问答详情页布局。
    制作首页的显示列表。
    发布功能完成。
    登录之后更新导航
    完成登录功能,用session记住用户名
  • 原文地址:https://www.cnblogs.com/dongguacha/p/5280100.html
Copyright © 2020-2023  润新知