美文网首页
2016.10.16-hibernate demo

2016.10.16-hibernate demo

作者: LH_0811 | 来源:发表于2016-10-17 09:29 被阅读2次

    hibernate操作数据库

    首先创建对应的类

    package demo;
    import java.util.Date;
    
    public class Student {
        private int sid;
        private String sname;
        private float score;
        private Date birthday;
        public Student() {
            super();
        }
        public int getSid() {
            return sid;
        }
        public void setSid(int sid) {
            this.sid = sid;
        }
        public String getSname() {
            return sname;
        }
        public void setSname(String sname) {
            this.sname = sname;
        }
        public float getScore() {
            return score;
        }
        public void setScore(float score) {
            this.score = score;
        }
        public Date getBirthday() {
            return birthday;
        }
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
        public Student(int sid, String sname, float score, Date birthday) {
            super();
            this.sid = sid;
            this.sname = sname;
            this.score = score;
            this.birthday = birthday;
        }   
    }
    

    创建Student.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- Generated 2016-10-16 10:07:57 by Hibernate Tools 3.5.0.Final -->
    <hibernate-mapping>
        <class name="demo.Student" table="STUDENT">
            <id name="sid" type="int"> //用id标签包裹的属性代表表的主键
                <column name="SID" />
                <generator class="native" />//assigned手动复制ID,native表示自增长
            </id>
            <property name="sname" type="java.lang.String">
                <column name="SNAME" />
            </property>
            <property name="score" type="float">
                <column name="SCORE" />
            </property>
            <property name="birthday" type="java.util.Date">
                <column name="BIRTHDAY" />
            </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.password">123456</property>
      <property name="hibernate.connection.url">jdbc:mysql:///test</property>
      <property name="hibernate.connection.username">root</property>
      <!-- 设置方言 -->
      <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
      <!-- 输出sql -->
      <property name="hibernate.show_sql">true</property>
      <!-- 格式化sql输出 -->
      <property name="hibernate.format_sql">true</property>
      <!-- sql执行方式 -->
      <property name="hibernate.hbm2ddl.auto">update</property>
      <!-- 为了使用getCurrentSession -->
      <property name="hibernate.current_session_context_class">thread</property>
      
      <!-- 资源配置 映射-->
      <mapping resource="demo/Student.hbm.xml"/>
     </session-factory
    </hibernate-configuration>
    
    

    写个test类测试

    package demo;
    
    import java.util.Date;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    public class Test {
        public static void main(String[] args) {
            SessionFactory sf = new Configuration().configure().buildSessionFactory();
            Session session = sf.getCurrentSession();
            Transaction transaction = session.beginTransaction();
            Student stu = new Student(4, "test1", 100.0f, new Date());
            session.save(stu);
            transaction.commit();
        }
    }
    
    

    相关文章

      网友评论

          本文标题:2016.10.16-hibernate demo

          本文链接:https://www.haomeiwen.com/subject/bqioyttx.html