Hibernate重新学习(03)

pom.xml

1.确定项目的字符集编码

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

2.指定依赖的jar包

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.11.Final</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
    </dependency>
</dependencies>

3.由于maven默认的jre版本为1.5,调整项目jre版本,采用maven插件

<plugins>
    <plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-compiler-plugin</artifactId>
	<version>3.3</version>
	<configuration>
		<source>1.8</source>
		<target>1.8</target>
	</configuration>
    </plugin>
</plugins>

4.编写Hibernate主要配置文件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://localhost:3306/hibernate</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123456</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="show_sql">true</property>
    </session-factory>
</hibernate-configuration>

5.创建java bean

package org.java.hibernate.bean;

public class User {

	private int id;
	private String name;
	private String password;
	
	//set get方法省略
}

6.创建关系映射文件User.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-mapping>
    <class name="org.java.hibernate.bean.User" table="bd_user">
        <id name="id" type="int">
            <column name="user_id" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="user_name" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="user_pwd" />
        </property>
    </class>
</hibernate-mapping>

7.将映射文件User.hbm.xml添加到主配置文件hibernate.cfg.xml中

<mapping resource="org/java/hibernate/bean/User.hbm.xml"/>

8.编写测试代码

package org.java.hibernate.test;

//忽略依赖

public class TestUser {
	
	@Test
	public void testAddUser(){
		Configuration config = new Configuration().configure();
		ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
		                                    .applySettings(config.getProperties())
		                                    .build();
		SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
		Session session = sessionFactory.openSession();
		User user = new User();
		user.setName("zhangsan");
		user.setPassword("123456");
		Transaction transaction = session.beginTransaction();
		try {
			session.save(user);
			transaction.commit();
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}finally{
			session.close();
			sessionFactory.close();
		}
	}
}


你可能感兴趣的:(java,Hibernate)