Hibernate连接SQL Server2008时候hibernate.cfg.xml和table.hbm.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">

<hibernate-configuration>

<session-factory>
	<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
	<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
	<property name="connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=Test</property>
	<property name="connection.username">sa</property>
	<property name="connection.password">password</property>
	<mapping resource="com/ljg/domain/Employee.hbm.xml"/>
</session-factory>

</hibernate-configuration>

然后是 table.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 package="com.ljg.domain">
	<class name="Employee" table="employee">
		<id name="id" column="id" type="java.lang.Integer">
			<generator class="native"/>
		</id>
		<property name="name" type="java.lang.String">
			<column name="name" not-null="false"/>
		</property>
		<property name="email" type="java.lang.String">
			<column name="email" not-null="false"/>
		</property>
		<property name="hireDate" type="java.util.Date">
			<column name="hireDate" not-null="false"/>
		</property>
	</class>
</hibernate-mapping>        



以下是Hibernate5的配置文件hibernate.cfg.xml: ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/testdb</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> </session-factory> </hibernate-configuration> ``` 接下来是加载配置文件的类HibernateSessionFactory: ```java import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateSessionFactory { private static SessionFactory sessionFactory; private HibernateSessionFactory() { } public static SessionFactory getSessionFactory() { if (sessionFactory == null) { Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); sessionFactory = configuration.buildSessionFactory(); } return sessionFactory; } } ``` 然后是持久化类Teachinfo和映射文件Teachinfo.hbm.xml: Teachinfo.java ```java public class Teachinfo { private int id; private String name; private int age; private String sex; public Teachinfo() { } public Teachinfo(int id, String name, int age, String sex) { this.id = id; this.name = name; this.age = age; this.sex = sex; } // 省略getter和setter方法 } ``` Teachinfo.hbm.xml ```xml <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="Teachinfo" table="teachinfo"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name" column="name"/> <property name="age" column="age"/> <property name="sex" column="sex"/> </class> </hibernate-mapping> ``` 最后是TeacherDao类,封装了对教师信息的增删改查操作: ```java import org.hibernate.Session; import org.hibernate.Transaction; public class TeacherDao { public void add(Teachinfo teachinfo) { Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession(); Transaction tx = session.beginTransaction(); session.save(teachinfo); tx.commit(); } public void delete(int id) { Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession(); Transaction tx = session.beginTransaction(); Teachinfo teachinfo = (Teachinfo) session.load(Teachinfo.class, id); session.delete(teachinfo); tx.commit(); } public void update(Teachinfo teachinfo) { Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession(); Transaction tx = session.beginTransaction(); session.update(teachinfo); tx.commit(); } public Teachinfo getById(int id) { Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession(); Transaction tx = session.beginTransaction(); Teachinfo teachinfo = (Teachinfo) session.get(Teachinfo.class, id); tx.commit(); return teachinfo; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值