标签:clip 支持 轻量级 oct over 开发 程序 encoding 统计
Hibernate框架简化了java应用程序与数据库交互的开发。 Hibernate是一个开源,轻量级的ORM(对象关系映射)工具。
Hibernate框架有很多优点。它们分别如下:
首先下载Hibernate的包,然后和数据库驱动包一起到导入到项目中。
首先看一看项目结构:
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"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"> jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 </property> <property name="connection.username">root</property> <property name="connection.password">517839</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- 加载映射描述信息 --> <mapping class="cn.test.bean.User" /> </session-factory> </hibernate-configuration>
User.java文件
package cn.test.bean; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="user")//表示对应的表名 public class User { @Id//表示主键 @Column(name="uid")//对应表中的字段名 private Integer id; @Column(name="uname")//对应表中的字段名 private String name; @Column(name="upass")//对应表中的字段名 private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", password=" + password + "]"; } }
HibernateUtil.java文件
package cn.test.util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { public static Session getSession(){ Configuration conf = new Configuration(); conf.configure("hibernate.cfg.xml");//读取连接参数和映射描述信息 SessionFactory factory = conf.buildSessionFactory(); Session session = factory.openSession(); return session; } }
UserTest.java文件
package cn.test.test; import org.hibernate.Session; import org.junit.Test; import cn.test.bean.User; import cn.test.util.HibernateUtil; public class UserTest { @Test public void testName1() throws Exception { Session session = HibernateUtil.getSession(); User user = (User)session.get(User.class, 1); if(user != null){ System.out.println(user); }else{ System.out.println("未找到记录"); } session.close(); } }
这里的方法: session.get(User.class, 1); 是Hibernate框架封装好的一个类,他表示查询数据表中主键为1的值,并且将结果反射到User的对象中。
如果是非注解的方式的话,我们只需要把上面的user.java文件替换调,并且加上User.hbm.xml文件,再在hibernate.cfg.xml文件中改一改映射关系就可以了。
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"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"> jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 </property> <property name="connection.username">root</property> <property name="connection.password">517839</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- 加载映射描述信息 --> <mapping resource="cn/test/hbm/User.hbm.xml" /> </session-factory> </hibernate-configuration>
User.hbm.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> <!-- 指定User类对应user表 --> <class name="cn.test.bean.User" table="user"> <!-- 指定no属性对应userid字段,类型为integer,主键 --> <id name="id" column="uid" type="integer"></id> <property name="name" column="uname" type="string"></property> <property name="password" column="upass" type="string"></property> </class> </hibernate-mapping>
User.java文件
package cn.test.bean;
public class User {
private Integer id;
private String name;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
}
}
HibernateUtil.java文件
package cn.test.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
public static Session getSession(){
Configuration conf = new Configuration();
conf.configure("hibernate.cfg.xml");//读取连接参数和映射描述信息
SessionFactory factory = conf.buildSessionFactory();
Session session = factory.openSession();
return session;
}
}
UserTest.java文件
package cn.test.test;
import org.hibernate.Session;
import org.junit.Test;
import cn.test.bean.User;
import cn.test.util.HibernateUtil;
public class UserTest {
@Test
public void testName1() throws Exception {
Session session = HibernateUtil.getSession();
User user = (User)session.get(User.class, 1);
if(user != null){
System.out.println(user);
}else{
System.out.println("未找到记录");
}
session.close();
}
}
标签:clip 支持 轻量级 oct over 开发 程序 encoding 统计
原文地址:http://www.cnblogs.com/HDK2016/p/7348348.html