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

  • 相关阅读:
    51-maven私有库神坑之:“Downloading: http://repo.maven.apache.org/maven2/”
    50-maven 仓库配置
    49-2017年第八届蓝桥杯国赛试题及详解(Java本科B组)
    21-matlab 迷宫题
    20-matlab全排列-函数调用
    52-python 画图二维
    51-python3 pandas读写excel
    48-设置tomcat虚拟路径的两种方法(Eclipse、tomcat、IDEA)
    19-matlab知识点复习二
    【剑指offer】二叉搜索树的后序遍历序列
  • 原文地址:https://www.cnblogs.com/littlepage/p/9630291.html
Copyright © 2020-2023  润新知