zoukankan      html  css  js  c++  java
  • hibernate框架模板(可复制修改)

    简易搭建jar包

    User类

    package com.littlepage.test;
    
    public class User {
        private int uid;
        private String uname;
        private String uage;
        private String uaddress;
        
        
        public int getUid() {
            return uid;
        }
        public void setUid(int uid) {
            this.uid = uid;
        }
        public String getUname() {
            return uname;
        }
        public void setUname(String uname) {
            this.uname = uname;
        }
        public String getUage() {
            return uage;
        }
        public void setUage(String uage) {
            this.uage = uage;
        }
        public String getUaddress() {
            return uaddress;
        }
        public void setUaddress(String uaddress) {
            this.uaddress = uaddress;
        }
        
    }

    User.hbm.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
        
    <hibernate-mapping>
        <class name="com.littlepage.test.User" table="t_User">
            <id name="uid" column="uid">
                <generator class="native"></generator>
            </id>
            <property name="uname" column="uname"></property>
            <property name="uage" column="uage"></property>
            <property name="uaddress" column="uaddress"></property>
        </class>
    </hibernate-mapping>

    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>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql:///hibernateday01</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password">root</property>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="hibernate.show_sql">true</property>
            <property name="hibernate.format_sql">true</property>
            <property name="hibernate.hbm2ddl.auto">update</property>
            <property name="connection.characterEncoding">utf8</property>
            <mapping resource="com/littlepage/test/User.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>

    Test代码

    package com.littlepage.test;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import org.junit.Test;
    
    public class TestHibernate {
        @Test
        public void addTest() {
            Configuration cfg=new Configuration().configure();
            SessionFactory sf=cfg.buildSessionFactory();
            Session session=sf.openSession();
            Transaction ts=session.beginTransaction();
            User user=new User();
            user.setUname("littlepage");
            user.setUage("18");
            user.setUaddress("China");
            session.save(user);
            ts.commit();
            session.close();;
            sf.close();
        }
    }

    Test结果截图

  • 相关阅读:
    ComboBox.DoubleClick事件
    mktime 夏令时
    STL String的使用[转]
    加在电源后至进入操作系统前的计算机的行为
    C语言数据类型大小分析(基于VC2005编译器)
    linux线程同步之条件变量
    windows 下架设svn服务器(转载+修改) (非利用Google项目托管)
    浅尝《Windows核心编程》之内核对象
    C——数组与指针
    如何用U盘做系统启动盘WINPE 并且 利用WINPE安装Ghost
  • 原文地址:https://www.cnblogs.com/littlepage/p/9630291.html
Copyright © 2011-2022 走看看