Hibernate第一次作业(第一个demo)

首先感谢于老师百忙之中抽出时间看我得博客,这是我的第一次作业。第一个Hibernate的demo。

编译器是IDEA

首先集成maven:


新建一个maven项目:



连接到数据库:




下面加入Hibernate功能:








maven里面的部分jar:

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.2.9.Final</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.41</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>


  </dependencies>


pom.xml代码:

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
    <!--hibernate核心jar包-->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.2.9.Final</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <!--数据库连接驱动-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.41</version>
    </dependency>

  </dependencies>




集成之后的项目结构:





代码:

package com.haibo.dao;

import com.haibo.po.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
/**
 * Created with IDEA.
 * User:haibo.
 * DATE:2018/5/4/004
 */
public class UserDAOImpl implements IUserDAO {
    private Configuration cfg;
    private SessionFactory sf;
    public UserDAOImpl(){
        cfg=new Configuration().configure("hibernate.cfg.xml");
        sf=cfg.buildSessionFactory();
    }
    @Test
    public void add() {
        Session session=sf.openSession();
        Transaction trans=session.getTransaction();
        trans.begin();
        User user=new User();
        user.setUserName("hhiber");
        user.setPassword("demo");
        user.setGender("三");
        session.save(user);
        trans.commit();
        session.close();
    }

hibernate.cfg.xml


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

   <session-factory>
    <property name="connection.url">jdbc:mysql://localhost:3306/user</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

       <property name="connection.username">root</property>
       <property name="connection.password">root</property>

       <mapping class="com.haibo.po.User"/>

    <!-- DB schema will be updated if needed -->
    <!-- <property name="hbm2ddl.auto">update</property> -->
  </session-factory>
</hibernate-configuration>



向数据库添加数据成功:




出现的问题:

1:jar包 容易导错,

2:xml文档的标签顺序;mapping在property后面




由于实验要求,按老师的要求又用myeclipse重新写了一遍:


user。hbm。xml
<hibernate-mapping package="com.Entity.User">
	<class name="User" table="user">
		<id name="userId" column="userId">
			<generator class="identity" /></id>
		<property name="userName" column="userName"/>
		<property name="password" column="password"/>
		<property name="gender" column="gender"/>
	</class>
</hibernate-mapping>


参考博客:https://blog.csdn.net/violet_echo_0908/article/details/50839373点击打开链接

猜你喜欢

转载自blog.csdn.net/lettyisme/article/details/80191070