windows下使用Hibernate连接Mycat例子
来源:互联网 发布:微信发长视频软件 编辑:程序博客网 时间:2024/05/17 08:31
项目结构
使用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" />
运行结果
1 0
- windows下使用Hibernate连接Mycat例子
- Hibernate 连接Mycat例子详解
- windows下mycat的安裝使用
- 使用C#连接mycat
- windows下安装配置mycat
- Hibernate连接mycat实现多租户
- mycat初步试水(windows下Mycat安装配置)
- 【mycat】mycat在windows环境下的安装和启动
- linux下mycat安装使用
- windows下配置mycat与常见问题解决
- mycat在windows平台下的安装
- WINDOWS下使用EXPECT的简单例子
- Linux下Mycat安装配置和使用
- hibernate连接oracle的例子
- Linux下使用rdesktop连接Windows远程桌面
- linux下使用rdesktop连接远程windows
- Windows下使用secureCRT 连接linux虚拟机
- windows下使用NX远程连接Linux
- Javaweb 后台文件验证整理
- Timestamp 基础知识及时间大小比较(写得不错)
- fastJson的基本使用
- workbench只导出数据(含insert语句)
- 微信JS-SDK说明文档之JS接口开发
- windows下使用Hibernate连接Mycat例子
- Delphi 得到程序配置路径
- 口才艺术与社交礼仪
- Java.util.date 与 java.sql.date区别和转换
- 忘记数据库密码怎么办?
- iPhone游戏开发:使用到的工具和技术
- jaxb 解析多列数据的xml 映射成list
- PIQ59: Find the missing number in the increasing sequence
- 文字换行