windows下使用Hibernate连接Mycat例子

项目结构

windows下使用Hibernate连接Mycat例子_第1张图片

使用jdk1.6和hibernate3

NewsManager.java代码如下:

package App;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import siso.wu.app.domain.News;

public class NewsManager {

    public static void main(String[] args)
        throws Exception {
        //实例化Configuration,
        Configuration conf = new Configuration() 

        //下面方法默认加载hibernate.cfg.xml文件
            .configure();
        //以Configuration创建SessionFactory
        SessionFactory sf = conf.buildSessionFactory();
        //创建Session
        Session sess = sf.openSession();
        //开始事务
        Transaction tx = sess.beginTransaction();
        //创建消息实例
        News n = new News();
        //设置消息标题和消息内容
        n.setTitle("NET开发人员");
        n.setContent("ASP.NET Web应用,以下是应用系统发布前,作为 .NET 开发人员需要检查的点");
        //保存消息
        sess.save(n);
        //提交事务
        tx.commit();
        //关闭Session
        sess.close();
        sf.close();
    }
}

News.java模型代码如下:

package siso.wu.app.domain;
public class News {
    private Integer id;
    private String title;
    private String content;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
}

News.hbm.xml映射文件





<hibernate-mapping>

    
    <class name="siso.wu.app.domain.News" table="news_table">
        
        <id name="id" type="java.lang.Integer">
            <column name="id" />
             
            <generator class="identity" />
        id>
        <property name="title" type="java.lang.String">
            <column name="title" />
        property>
        <property name="content" type="java.lang.String">
            <column name="content" />
        property>
    class>
hibernate-mapping>

hibernate配置文件hibernate.cfg.xml




<hibernate-configuration>
    <session-factory>
          
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
           
        <property name="hibernate.connection.username">testproperty>
        
        <property name="hibernate.connection.password">testproperty>

        
        <property name="hibernate.connection.url">jdbc:mysql://localhost:8066/TESTDBproperty>


         
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialectproperty>

        
        <property name="show_sql">trueproperty>
        <property name="hibernate.format_sql">trueproperty>

         
        <property name="hibernate.hbm2ddl.auto">updateproperty>

        
        <mapping resource="siso/wu/app/domain/News.hbm.xml" />

    session-factory>
hibernate-configuration>

Mycat配置,详情点击查看


<table name="news_table" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" />

运行结果

windows下使用Hibernate连接Mycat例子_第2张图片

你可能感兴趣的:(分布式开发技术,JAVA技术,JAVA,Tomcat服务器,Mycat中间件,.net平台下分布式开发技术)