Hibernate.cfg.xml示例:
<?xml version="1.0" encoding="UTF-8"?> <!-- DTD(Document Type Definition)文档类型定义;XML解析器,使用DTD文档来检查XML文件的合法性 --> <!-- Hibernate版本不同,其DTD文档略有差异;切记,要使用与Hibernate版本相对应的DTD文档 --> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <!-- Hibernate主配置文件的根元素 --> <hibernate-configuration> <!-- Hibernate初始化SessionFactory的参数 --> <session-factory> <span style="white-space:pre"> </span><!-- 指定Hibernate连接数据库时,所用的驱动 --> <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property> <!-- 设置数据库的连接地址(URL);localhost:本机,3306:端口号,push:数据库名 --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/push</property> <!-- 指定数据库的用户名 --> <property name="hibernate.connection.password">gjs@y1</property> <!-- 指定数据库的密码 --> <property name="hibernate.connection.username">root</property> <!-- 指定数据库的方言;每种数据库,都有其对应的方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 指定映射文件;映射文件可以有很多个 --> <mapping resource="com/back/entity/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.org/dtd/hibernate-mapping-3.0.dtd"> <!-- Hibernate映射文件的根元素 --> <hibernate-mapping> <!-- 配置PO对象和数据库中表的对应关系,使用class元素 --> <!-- name:PO对象;table:数据库表;catalog:数据库名 --> <class name="com.back.entity.User" table="user" catalog="push"> <!-- 配置PO对象和数据库中表的id字段,即定义主键,使用id元素 --> <!-- name:PO对象的属性;type:属性的类型 --> <id name="id" type="java.lang.Integer"> <!-- column:数据表中的列 --> <column name="id" /> <!-- generator:主键的生成方式 --> <generator class="native" /> </id> <!-- 配置PO对象和数据库中表的其他字段,使用property元素 --> <property name="name" type="java.lang.String"> <column name="name" /> </property> <property name="password" type="java.lang.String"> <column name="password" /> <span style="white-space:pre"> </span></property> </class> </hibernate-mapping>测试代码:
package test; import java.io.File; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.back.entity.User; public class abcTest { @SuppressWarnings("deprecation") public static void main(String[] args) throws Exception { File file = new File("src/hibernate.cfg.xml"); System.out.println("file path:" + file.getAbsolutePath()); Configuration cfg = new Configuration().configure(file); SessionFactory sf = cfg.buildSessionFactory(); Session session = sf.openSession(); Transaction tx = session.beginTransaction(); User user = new User(); user.setName("test"); user.setPassword("password"); Integer id = (Integer) session.save(user); System.out.println("save() id:" + id); user = (User) session.get(User.class, id); if (user != null) { System.out.println("id:" + user.getId() + ",name:" + user.getName() + ",password:" + user.getPassword()); } // session.delete(); String delStr = "delete from User where name=:name and password=?0"; Query query = session.createQuery(delStr); query.setString("name", "test"); query.setParameter("0", "password"); int count = query.executeUpdate(); System.out.println("executeUpdate() count:" + count); tx.commit(); session.close(); System.out.println("done."); } }