项目结构
使用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映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--name实体类的 包名+ 类名 table数据库的表名-->
<class name="siso.wu.app.domain.News" table="news_table">
<!-- name对应实体类的属性id -->
<id name="id" type="java.lang.Integer">
<column name="id" />
<!--主键的生成策略 native可以适应多种数据库 increment mysql自动增长策略 sequence oracle自动增长策略 -->
<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
<?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>
<!--配置数据库的驱动程序,Hibernate在连接数据库时,需要用到数据库的驱动程序-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!--用户名-->
<property name="hibernate.connection.username">test</property>
<!--密码-->
<property name="hibernate.connection.password">test</property>
<!-- 连接mycat逻辑数据库 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:8066/TESTDB</property>
<!-- SQL方言,这边设定的是MySQL -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- 显示SQL语句 -->
<property name="show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- ddl语句自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!--映射文件 -->
<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" />
运行结果