标签:
maven
<!-- 导入org.hibernate jar包 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.1.4.Final</version> </dependency> <!-- 导入Mysql数据库链接jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency>
hibernate.cfg.xml配置文件
<?xml version=‘1.0‘ encoding=‘UTF-8‘?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="dialect">org.hibernate.dialect.MySQLDialect </property> <property name="connection.url">jdbc:mysql://localhost:3306/test2</property> <property name="connection.username">root</property> <property name="connection.password">Sa1234$</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="myeclipse.connection.profile">mysql</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <property name="format_sql">true</property> <mapping resource="user.hbm.xml" /> </session-factory> </hibernate-configuration>
实体类
package my; public class User implements java.io.Serializable { // Fields private Long userId; private String userName; private String password; private String comment; // Constructors public User() { } // Property accessors public Long getUserId() { return this.userId; } public void setUserId(Long userId) { this.userId = userId; } public String getUserName() { return this.userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } public String getComment() { return this.comment; } public void setComment(String comment) { this.comment = comment; } }
映射文件
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="my.User" table="user" catalog="test2"> <id name="userId" type="java.lang.Long"> <column name="userId" /> <generator class="identity" /> </id> <property name="userName" type="java.lang.String"> <column name="userName" length="30" not-null="true" /> </property> <property name="password" type="java.lang.String"> <column name="password" length="80" not-null="true" /> </property> <property name="comment" type="java.lang.String"> <column name="comment" length="250" /> </property> </class> </hibernate-mapping>
测试
package my.hibernate2; import my.User; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class App { public static void main(String[] args) { Configuration config = new Configuration(); SessionFactory sessionFactory = config.configure() .buildSessionFactory(); // 默认读取hibernate.cfg.xml Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User user = new User(); user.setUserName("hibernate nuam"); user.setPassword("h111"); session.save(user); transaction.commit(); session.close(); } }
标签:
原文地址:http://www.cnblogs.com/dehai/p/5033855.html