最近在学习hibernate5,经历了比较痛苦的入门,踩了不少坑。好在,结果还是搞出了自己的hello hibernate.
开发环境:eclipse+jdk8
hibernate JAR包:hibernate-release-5.2.5.Final
- MySQL表脚本
CREATE TABLE `person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) CHARACTER SET gb2312 NOT NULL,
`password` varchar(20) NOT NULL,
`sex` varchar(2) CHARACTER SET gb2312 DEFAULT NULL,
`email` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;
- 数据模型 Person.java
Note:每个属性都要有getter, setter
package hiber;
public class Person {
private String id ;
private String name ;
private String password ;
private String sex ;
private String email ;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getId() {
return id;
}
public void setId(String 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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String toString(){
return getId() + ":"
+ getName() +":"
+ getEmail() +":"
+ getSex() +":";
}
}
- 数据模型与表字段mapping,person.hbm.xml
Note: generator要注意,这里踩了不少坑。
参考:http://ryxxlong.iteye.com/blog/612446
http://blog.csdn.net/huaweitman/article/details/39025553
<?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" >
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Thu Nov 09 16:06:12 CST 2006 -->
<hibernate-mapping package="hiber">
<class name="Person" table="PERSON">
<id name="id" column="ID" type="string">
<generator ***class="native"*** />
</id>
<property name="name" column="NAME" type="string" not-null="true" />
<property name="password" column="PASSWORD" type="string" not-null="true" />
<property name="sex" column="SEX" type="string" />
<property name="email" column="EMAIL" type="string" />
</class>
</hibernate-mapping>
- hibernate.cfg.xml配置
Note:记得把mysql驱动JAR加入编译路径;
<?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="myeclipse.connection.profile">mysql</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">test</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
**<mapping resource="person.hbm.xml" />**
</session-factory>
</hibernate-configuration>
- 数据操作类 PersonOperate.java
package hiber;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
public class PersonOperate {
private Session session = null;
public PersonOperate(){
//get hibernate config
Configuration configuration = new Configuration().configure();
// get sessionFactory
SessionFactory factory = configuration.buildSessionFactory();
// get a ssesion
this.session = factory.openSession();
}
public void insert(Person person){
Transaction transaction = this.session.beginTransaction();
this.session.save(person);
transaction.commit();
// this.session.close();
}
public List<Person> query(){
List<Person> persons = null;
@SuppressWarnings("unchecked")
Query<Person> query = this.session.createQuery("FROM Person");
persons = query.getResultList();
return persons;
}
}
- 测试类 Test.java
package hiber;
import java.util.List;
public class test {
public static void main(String[] args){
PersonOperate operate = new PersonOperate();
for(int i=1; i<11; i++){
Person person = new Person();
person.setEmail("danny.yao@13.com");
// person.setId(""); //db 自增主键
person.setName("danny"+i);
person.setPassword("pwd");
person.setSex("m");
operate.insert(person);
}
List<Person> persons;
persons = operate.query();
System.out.println(persons.size());
for(Person person : persons){
System.out.println(person.toString());
}
}
}