hibernate 最入门简单配置
得到的是hibernate-release-4.3.0.Beta2
这程序实现向数据库中插入记录
输入hibernate之后
之后导入工程。
接下来配置 结构如下图
User.java代码如下
View Code
1 package javamxj.hibernate; 2 import java.util.Date; 3 public class User { 4 5 /** 6 * @param args 7 */ 8 private int id; 9 private String username; 10 private String password; 11 12 public int getId() { 13 return id; 14 } 15 public void setId(int id) { 16 this.id = id; 17 } 18 public String getUsername() { 19 return username; 20 } 21 public void setUsername(String username) { 22 this.username = username; 23 } 24 public String getPassword() { 25 return password; 26 } 27 public void setPassword(String password) { 28 this.password = password; 29 } 30 31 32 }
hibernate.cfg.xml代码如下
View Code
1 <?xml version='1.0' encoding='GBK'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 7 <hibernate-configuration> 8 9 <session-factory> 10 11 <!-- 是否将运行期生成的SQL输出到日志以供调试 --> 12 <property name="show_sql">true</property> 13 14 <!-- SQL方言,这里设定的是MySQL --> 15 16 <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property> 17 18 <!-- JDBC驱动程序 --> 19 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 20 21 <!-- JDBC URL, "?useUnicode=true&characterEncoding=GBK" 表示使用GBK进行编码 --> 22 <property name="connection.url"> 23 jdbc:mysql://localhost:3306/HibernateTest?useUnicode=true&characterEncoding=GBK 24 </property> 25 26 <!-- 数据库用户名 --> 27 <property name="connection.username">root</property> 28 29 <!-- 数据库密码 --> 30 <property name="connection.password"> your password</property> 31 32 <!-- 指定User的映射文件 --> 33 <mapping resource="javamxj/hibernate/User.hbm.xml"/> 34 35 </session-factory> 36 37 </hibernate-configuration>
User.hbm.xml代码如下
View Code
1 <?xml version="1.0" encoding="GBK"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping> 7 8 <class name="javamxj.hibernate.User" table="UserTable"> 9 <id name="id" > 10 <generator class="assigned" /> 11 </id> 12 <property name="username" /> 13 <property name="password" /> 14 </class> 15 16 </hibernate-mapping>
Test.java代码如下
View Code
1 package javamxj.hibernate; 2 3 import org.hibernate.HibernateException; 4 import org.hibernate.Session; 5 import org.hibernate.SessionFactory; 6 import org.hibernate.Transaction; 7 import org.hibernate.cfg.Configuration; 8 9 public class Test { 10 11 /** 12 * @param args 13 */ 14 public static void main(String[] args) { 15 16 try { 17 SessionFactory sf = new Configuration().configure() 18 .buildSessionFactory(); 19 Session session = sf.openSession(); 20 Transaction tx = session.beginTransaction(); 21 22 User user = new User(); 23 user.setUsername("wf"); 24 user.setPassword("xxxx"); 25 26 session.save(user); 27 tx.commit(); 28 session.close(); 29 30 } catch (HibernateException e) { 31 e.printStackTrace(); 32 } 33 } 34 35 }
接下来配置数据库
create HibernateTest;
use HibernateTest;
create table usertable(ID int(6) not null auto_increment,username varchar(24) not null default '',password varchar(24) not null default '', primary key (ID));
用select* from usertable查询
程序执行下数据表中就多一条记录。