千家信息网

SSH系列:(3)Hibernate

发表于:2024-11-17 作者:千家信息网编辑
千家信息网最后更新 2024年11月17日,(1)引入jar包(2)配置(3)测试1、引入jar包引入mysql jar包mysql-connector-java-5.1.38-bin.jar引入c3p0 jar包c3p0-0.9.1.2.ja
千家信息网最后更新 2024年11月17日SSH系列:(3)Hibernate


(1)引入jar包

(2)配置

(3)测试


1、引入jar包


引入mysql jar包


mysql-connector-java-5.1.38-bin.jar


引入c3p0 jar包


c3p0-0.9.1.2.jar


引入hibernate相关jar包 (hibernate-distribution-3.6.0.Final)


antlr-2.7.6.jar

commons-collections-3.1.jar

dom4j-1.6.1.jar

hibernate3.jar

hibernate-jpa-2.0-api-1.0.0.Final.jar

javassist-3.12.0.GA.jar

jta-1.1.jar

slf4j-api-1.6.1.jar



2、配置


2.1、添加实体类:Person.java

package com.rk.test.entity;/** * 实体层Person类 DTO *  * */public class Person {        private String pId;        private String pName;        private int pVersion;        public String getpId() {                return pId;        }        public void setpId(String pId) {                this.pId = pId;        }        public String getpName() {                return pName;        }        public void setpName(String pName) {                this.pName = pName;        }        public int getpVersion() {                return pVersion;        }        public void setpVersion(int pVersion) {                this.pVersion = pVersion;        }        @Override        public String toString() {                return "Person [pId=" + pId + ", pName=" + pName + ", pVersion="                                + pVersion + "]";        }        }


2.2、添加映射文件:Person.hbm.xml

                                                                                                        


2.3、添加Hibernate配置文件:hibernate.cfg.xml

                        com.mysql.jdbc.Driver        jdbc:mysql:///tax_sys             root        root                        org.hibernate.dialect.MySQL5Dialect                                                true                                false                                update                                                thread                                                  


3、测试

测试两方面:第一是能从数据库读取一条数据,第二是能向数据库保存一条数据

package com.rk.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.Before;import org.junit.Test;import com.rk.test.entity.Person;public class TestHibernate {        private SessionFactory sf;                @Before        public void init()        {                sf = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();        }                @Test        public void test()        {                Session session = sf.getCurrentSession();                session.beginTransaction();                                Person p = (Person) session.get(Person.class, "4028d081564a762001564a76221e0000");                System.out.println(p);                                Person p2 = new Person();                p2.setpName("Tomcat");                session.save(p2);                session.getTransaction().commit();                        }}




0