SS1.1

    xiaoxiao2021-03-26  101

    主流的ORM框架 Hibernate iBatis TopLink 手工创建第一个Hibernate应用 1.创建Hibernate的配置文件 2.创建持久化类 3.创建对象 - 关系映射文件 4.通过Hibernate API编写访问数据库的代码

    1.创建Hibernate的配置文件hibernate.cfg.xml

    <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration     PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration>     <session-factory>  <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  <property name="connection.url">jdbc:mysql://localhost:3306/testdb</property>  <property name="connection.username">root</property>  <property name="connection.password">123456</property>  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  <property name="show_sql">true</property>  <property name="hbm2ddl.auto">create</property>  <property name="current_session_context_class">thread</property>  <mapping resource="net/nw/vo/Student.hbm.xml" />     </session-factory> </hibernate-configuration>

    2.创建持久化类

    package net.nw.vo; public class Student { private int sid; private String sname; 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; } }

    3.创建对象 - 关系映射文件,注意: 别忘记在hibernate.cfg.xml配置文件中添加<mapping resource="net/nw/vo/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"> <!-- hibernate映射,包名对应java的包名 --> <hibernate-mapping package="net.nw.vo">     <!-- 指定对实体类net.nw.vo.Student与数据库关系表student的映射关系,当两者相同时,可以不指定table -->     <class name="net.nw.vo.Student" table="student">          <!-- id指定实体属性(name="sid")与关系表主键(column="sid")之间映射关系 -->          <id name="sid" column="sid" type="int"></id>          <!-- 实体属性与关系表字段之间映射关系,如果两者名字相同,column可以不写 -->          <property name="sname" column="sname" type="string"></property>     </class> </hibernate-mapping>

    4. 创建log4j.properties文件

    # Configure logging for testing: optionally with log file log4j.rootLogger=WARN, stdout # log4j.rootLogger=WARN, stdout, logfile log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.logfile=org.apache.log4j.FileAppender log4j.appender.logfile.File=target/spring.log log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

    5.通过Hibernate API编写访问数据库的代码

    package net.nw.dao; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import net.nw.vo.Student; public class HibernateTest { public static void main(String[] args) { // TODO Auto-generated method stub SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); try{ Student s = new Student(); s.setSid(1); s.setSname("zhangsan"); session.save(s); tx.commit(); }catch(Exception ex){ tx.rollback(); ex.printStackTrace(); }finally{ if(session !=null){ session.close(); } } } }

    6. 创建数据库testdb

    7.测试运行程序代码,运行效果截图如下:

    本项目源码下载地址

    mysql-connector-java-5.1.40.zip包:http://download.csdn.net/detail/zhengzizhi/9747510

    项目源码:http://download.csdn.net/detail/zhengzizhi/9747513

    转载请注明原文地址: https://ju.6miu.com/read-660918.html

    最新回复(0)