eclipse使用hibernate连接mysql
2017-02-18 00:00
274 查看
Eclipse 使用hibernate连接mysql
1 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="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://127.0.0.1:3306/maodongbang?useUnicode=true&characterEncoding=UTF-8
</property>
<property name="connection.username">root</property>
<property name="connection.password">mft131415138</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="hbm2ddl.auto">create</property>
<mapping resource="com/cn/Students.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2 Students.xml
package com.cn;
public class Students {
private String name;
}
3 students.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-2-18 14:14:44 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.cn.Students" table="STUDENTS">
<id name="name" type="java.lang.String">
<column name="NAME" />
<generator class="assigned" />
</id>
<property name="pass" type="java.lang.String">
<column name="PASS" />
</property>
</class>
</hibernate-mapping>
4 Test.java
package com.cn;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
public class Test {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init(){
Configuration config=new Configuration().configure();
//创建服务注册对象
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//创建会话工厂对象
sessionFactory=config.buildSessionFactory(serviceRegistry);
//会话对象
session=sessionFactory.openSession();
//启动事物
transaction=(Transaction) session.beginTransaction();
}
@After
public void destory(){
}
注意事项:junit不要用4.0
<mapping resource="填写正确路径">
junit测试类导入的包必须是hibernate的
注意此3点可以节约许多时间
1 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="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://127.0.0.1:3306/maodongbang?useUnicode=true&characterEncoding=UTF-8
</property>
<property name="connection.username">root</property>
<property name="connection.password">mft131415138</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="hbm2ddl.auto">create</property>
<mapping resource="com/cn/Students.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2 Students.xml
package com.cn;
public class Students {
private String name;
public Students(String name, String pass) { this.name = name; this.pass = pass; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } private String pass; public Students() { } @Override public String toString() { return "Students [name=" + name + ", pass=" + pass + "]"; }
}
3 students.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-2-18 14:14:44 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.cn.Students" table="STUDENTS">
<id name="name" type="java.lang.String">
<column name="NAME" />
<generator class="assigned" />
</id>
<property name="pass" type="java.lang.String">
<column name="PASS" />
</property>
</class>
</hibernate-mapping>
4 Test.java
package com.cn;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
public class Test {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init(){
Configuration config=new Configuration().configure();
//创建服务注册对象
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//创建会话工厂对象
sessionFactory=config.buildSessionFactory(serviceRegistry);
//会话对象
session=sessionFactory.openSession();
//启动事物
transaction=(Transaction) session.beginTransaction();
}
@After
public void destory(){
transaction.commit(); session.close(); sessionFactory.close(); } @org.junit.Test public void test() { Students s =new Students("12","12"); session.save(s); }
}
注意事项:junit不要用4.0
<mapping resource="填写正确路径">
junit测试类导入的包必须是hibernate的
注意此3点可以节约许多时间
相关文章推荐
- eclipse使用hibernate连接mysql的crup
- eclipse使用hibernate连接mysql不使用transaction提交事物
- 使用Eclipse连接DB2数据库(利用Hibernate Tools for Eclipse插件 )
- Eclipse+CDT环境下使用C语言连接MySQL——详细讲解
- eclipse中通过使用Tomcat配置数据资源来连接MySQL
- 使用Eclipse连接SQL Server 2008 及 MySQL
- Eclipse 使用 jdbc 连接 mysql
- Eclipse+CDT环境下使用C语言连接MySQL——详细讲解
- 使用Hibernate连接MySQL数据库,MySQL连接超时断开的问题
- 使用Hibernate连接MySQL数据库,MySQL连接超时断开的问题
- ubuntu16.04下eclipse使用 jdbc连接本地mysql出现:Communications link failure错误
- 【转载】Eclipse+CDT环境下使用C语言连接MySQL——详细讲解
- 使用eclipse连接mysql database explorer连接不上的解决办法以及配置
- 使用Eclipse远程连接MySQL,以定时查询数据为示例
- 使用Hibernate连接MySQL
- 初学者使用hibernate连接MySQL出现unable to create connection check your url问题
- 使用hibernate连接mysql遇到的编码问题
- MySQL之—— 使用Hibernate连接MySQL数据库,MySQL连接超时断开的问题
- 使用Eclipse下的JAVA连接MySQL
- 使用Hibernate连接MySQL数据库,MySQL连接超时断开的问题