hebernate与mysql调整

WBOY
发布: 2016-06-07 16:25:33
原创
1327 人浏览过

hebernate与mysql整合 1.下载mysql, 如何安装 2.开始在myeclipse 整合hebernate hebernate 需要的一些包: 配置文件:(Mysql 已开启远程连接) ?xml version='1.0' encoding='UTF-8'?!DOCTYPE hibernate-configuration PUBLIC -//Hibernate/Hibernate Config

hebernate与mysql整合

1.下载mysql,

如何安装

2.开始在myeclipse 整合hebernate

hebernate 需要的一些包:

配置文件:(Mysql 已开启远程连接)

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


<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>

	<property name="connection.username">root</property>
	<property name="connection.url">jdbc:mysql://192.168.1.135:3306/SolarWorkFlowDb?useUnicode=true&characterEncoding=utf8</property>
	<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
	<property name="myeclipse.connection.profile">mysqll</property>
	<property name="connection.password">solar</property>
	<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
	<property name="show_sql">true</property>
	<property name="format_sql">true</property>
	<property name="cache.use_query_cache">true</property>
	<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
    <mapping resource="Mytest.hbm.xml"></mapping>

</session-factory>

</hibernate-configuration>
登录后复制
 

配置文件2

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

<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="test.Person" table="SLWF_PERSON">
        <comment>测试数据</comment>
        <id name="ID" type="java.lang.Integer">
            <column name="ID"></column>
            <generator class="native"></generator>
        </id>
        
         <property name="FULL_NAME" type="java.lang.String">
            <column name="FULL_NAME"></column>
        </property>   
        
        <property name="LAST_NAME" type="java.lang.String">
            <column name="LAST_NAME"></column>
        </property>        
    </class>
</hibernate-mapping>
登录后复制

实体类:

package test;

public class Person {
	private int ID ;
	private String FULL_NAME;
	private String LAST_NAME;
	
	public Person(){}

	public Person(int iD, String fULLNAME, String lASTNAME) {
		super();
		ID = iD;
		FULL_NAME = fULLNAME;
		LAST_NAME = lASTNAME;
	}

	public int getID() {
		return ID;
	}

	public void setID(int iD) {
		ID = iD;
	}

	public String getFULL_NAME() {
		return FULL_NAME;
	}

	public void setFULL_NAME(String fULLNAME) {
		FULL_NAME = fULLNAME;
	}

	public String getLAST_NAME() {
		return LAST_NAME;
	}

	public void setLAST_NAME(String lASTNAME) {
		LAST_NAME = lASTNAME;
	}
    
	
	
}
登录后复制
测试类

package test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class TestPro {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Person p = new Person();
	
		p.setFULL_NAME("Tom");
		p.setLAST_NAME("LEE");
		Configuration cfg = new Configuration();
		SessionFactory sf = cfg.configure().buildSessionFactory();
		
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(p);
		session.getTransaction().commit();
		session.close();
		sf.close();
	}

}
登录后复制

表结构:

测试结果:

资料:点击打开链接


相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!