hibernate 单独配置

Hibernate配置:

  • 数据库连接配置文件,路径随意

名称为hibernate.cfg.xml

// 读取默认配置文件
cfg = new Configuration().configure();

 名称为自定义

// 读取自定义配置文件
cfg = new Configuration().configure("自定义.xml");

 hibernate.cfg.xml文件示例

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC  
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  
<hibernate-configuration>  
    <session-factory>  
        <!-- 设置数据库驱动 -->  
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
        <!-- 设置数据库URL -->  
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>  
        <!-- 数据库用户名 -->  
        <property name="hibernate.connection.username">root</property>  
        <!-- 数据库密码 -->  
        <property name="hibernate.connection.password">whenjun</property>  
        <!-- 指定对应数据库的方言,hibernate为了更好适配各种关系数据库,针对每种数据库都指定了一个方言dialect -->  
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
          
        <!-- 映射文件 -->  
        <mapping resource="com/whenjun/entity/hbm/User.hbm.xml"/>  
    </session-factory>  
</hibernate-configuration>

entity映射文件名称随意,一般为 entity.hbm.xml,路径随意

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC   
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
     
<!-- 设置是否使用代理对象 default-lazy="false" --> 
<hibernate-mapping default-lazy="false">  
    <class name="com.whenjun.entity.User">  
        <id name="id">  
        	<!-- hibernate自己生成uuid -->
            <!-- <generator class="uuid.hex"/>   -->
        </id>  
        <property name="username"/>  
        <property name="password"/>  
    </class>  
</hibernate-mapping>  

 entity类

@Entity
@Table(name="user")
public class User implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id
	@Column(name = "id", nullable = false)
	private String id;
	
	@Column(length=255) 
	private String username;
	
	@Column(length=255) 
	private String password;
	
	public User(){}
	
	public User(String id,String username,String password){
		this.id=id;
		this.username=username;
		this.password=password;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	@Override
	public String toString(){
		return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
	}
}

 test

public class JTest {

	private Configuration cfg = null;

	private SessionFactory factory = null;

	private Session session = null;

	@Before
	public void before() {
		// 读取配置文件
		cfg = new Configuration().configure();

		factory = cfg.buildSessionFactory();

		session = factory.openSession();

		System.out.println("session开启成功");
	}

	@After
	public void after() {
		if (session != null) {
			if (session.isOpen()) {
				// 关闭session
				session.close();
				System.out.println("session关闭");
			}
		}
		if(factory!=null){
			factory.close();
			System.out.println("sessionFactory关闭");
		}
	}
/****
	 * 新增
	 * 
	 * @date 2017年5月4日
	 * @author wanwenjun
	 */
	@Test
	public void save() {
		try {
			session.beginTransaction();

			String id = UUID.randomUUID().toString();
			
			System.out.println(id);
			User user = new User();
			user.setId(id);
			user.setUsername("搜索");
			user.setPassword("2321");

			session.save(user);
			
			// 提交事务
			session.getTransaction().commit();

			System.out.println("添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			// 回滚事务
			session.getTransaction().rollback();
			System.out.println("添加失败,回滚");
		}
	}
}

 

猜你喜欢

转载自whenjun.iteye.com/blog/2372636
今日推荐