Java Hibernate Mysql
demo:http://download.csdn.net/download/keen_zuxwang/9898205
添加lib库文件夹,包括hibnernate库以及连接Mysql的驱动的jar
…
hibernate-core-5.0.0.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
mysql-connector-java-5.1.40-bin.jar //mysql java 连接驱动jar
…
1、hibernate.cfg.xml – hibernate配置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="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/test
</property>
<!-- 数据库连接设置 -->
<property name="hibernate.connection.username">user-name</property>
<property name="hibernate.connection.password">user-password</property>
<!-- show_sql 生成SQL语句输出到日志以供调式 -->
<property name="hibernate.show_sql">true</property>
<!-- SQL dialect 方言 -->
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- 指定session通过当前执行的线程来跟踪和界定 -->
<property name="hibernate.current_session_contecxt_class" >
thread
</property>
<!-- 添加实体类的映射文件-->
<mapping resource="com/myhibernate/city.hbm.xml" />
</session-factory>
</hibernate-configuration>
2、city.hbm.xml — hibernate mapping xml文档实体类与数据库表的映射设置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.Net/hibernate-mapping-3.0.dtd">
<!--<hibernate-mapping package="com.myhibernate"> -->
<hibernate-mapping>
<class name="com.myhibernate.City" table="city">
<id name="id" type="java.lang.Integer"></id>
<property name="name" type="java.lang.String" />
<property name="level" type="java.lang.Integer" />
</class>
</hibernate-mapping>
3、POJO实体类
public class City {
private int id;
private String name;
private int level;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}
4、
public static void main(String a[])
{
City l = new City();
l.setId(10);
l.setName("foshan");
//l.setLevel(1);
l.setLevel(1);
City l1 = new City();
l1.setId(11);
l1.setName("dongguang");
l1.setLevel(1);
City l0 = new City();
l0.setId(12);
l0.setName("zhongshan");
l0.setLevel(1);
//Transaction tx = session.beginTransaction();
//tx.commit();
Configuration cfg = new Configuration();//配置
SessionFactory sf = cfg.configure().buildSessionFactory();//会话工厂
Session session = sf.openSession();//具体会话,包括一次事务的操作
session.beginTransaction(); //事务
session.save(l0); //add element
session.save(l1);
session.save(l);
//session.update(l1);
//session.update(l);
session.getTransaction().commit();//提交执行事务
session.close();
sf.close();
System.out.print("success!");
}
5、运行工程,Run As => Java Application
Eclipse Console 运行输出信息:
Mysql数据库运行、更新: