• 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结果截图

  • 相关阅读:
    jQuery实现鼠标点击Div区域外隐藏Div
    JS判断输入值为正整数
    trim()不兼容ie的问题及解决方法
    傻问题就用傻办法:解决问题有时候不需要探究根源,依据表象就能直接解决
    /vendor/lib64/libOpenCL.so在安卓应用中无访问权限的解决办法
    复数域上的人工神经网络与量子计算
    中国移动CMCC家庭路由器的默认登陆账号
    717. 1-bit and 2-bit Characters
    219. Contains Duplicate II
    1346. Check If N and Its Double Exist
  • 原文地址:https://www.cnblogs.com/littlepage/p/9630291.html
Copyright © 2020-2023  润新知