MyEclipse6Java开发中文教程 入最后一页,这一页选择是否生成 Session Factory类,去掉复选框 Create Session Factory cass?的选中状态,最后点击 Finish按钮完成整个添加过程。这最后一页的设置如下所示 Create Hibernate Session Factory for MyEclipse Define SessionFactory properties 去掉选中状态 JCreate Session Factory class? Jave source folder B CLass neme Hi bernstesessionFacto 图114添加 Hibernate功能向导的最后一页 稍等片刻之后,类库将会添加当前项目,所有的jar文件会出现在目录 WebrootwEB-NFm下,然后修改文件src/ hibernate. cfg. Xm的内容如下所示(已改动 部分已粗斜体显示): <? xm1 vers '1.0 encoding='UTF-8? < DOCTYPE hibernate-configuration PUBLIC DTD 3.0//El http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> erated by MyEclipse Hibernate Tools ection username">root</propert <propertya name="connection. url">jdbc: mysql: //localhost: 3306/test?useUnicode=tru e& characterEncoding=GBK</property> name="dialect">org.hibernate dialect. MySQLDialect</property> Property name="myeclipse connection profile">mysq15</property> <property name="connection driver class">com mysql jdbc Driver</property> </session-factory> </hibernate-configuration> 这么做的原因是XML文件中&是个特殊字符,必须用转义后的&来代替。 刘长炯著
MyEclipse 6 Java 开发中文教程 11 刘长炯著 入最后一页,这一页选择是否生成 Session Factory 类,去掉复选框 Create SessionFactory class?的选中状态,最后点击 Finish 按钮完成整个添加过程。这最后一页的设置如下所示: 图 11.4 添加 Hibernate 功能向导的最后一页 稍等片刻之后,类库将会添加当前项目,所有的 jar 文件会出现在目录 /WebRoot/WEB-INF/lib 下,然后修改文件 src/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"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="connection.username">root</property> <property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=tru e&characterEncoding=GBK</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="myeclipse.connection.profile">mysql5</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> </session-factory> </hibernate-configuration> 这么做的原因是 XML 文件中&是个特殊字符,必须用转义后的&来代替
MyEclipse6Java开发中文教程 114添加 Spring功能 最后这个项目需要加入的内容就是 Spring功能了,详细的操作过程可以参考10521 创建项目,添加必要的开发功能一节内容。 选择菜单 My Eclipse> Project Capabilities> Add Spring Capabilities….来启动 Add Spring Capabilities向导。在这个向导的第一页有两个地方需要设置:第一个地方是 选择类库的时候要选中 Spring ORM和 Spring Web的包,在 Select the libraries to add to the buildpath(选择添加到类路径的类库)一栏的下侧,选中 Spring2.0 Persistence Core Libraries和 Spring20 Web libraries;第二个地方是在 JAR Library Installation(JAR 类库安装)处点击选中单选钮 Copy checked Library contents to project folder(TLDs always copied),这个选项将选中的类库的JAR文件复制到项目目录,在此单选钮下方的 Library Folder会自动选中M的 ebrootweB-NF/Ib目录,点击Nex按钮进入第笋页。那 么第二页选择 Spring配置文件的位置保持原来的不变即可,再点击进入第三页设置 LocalSessionFactory,这一页也保持默认,然后点击 Finish按钮结束整个向导。稍等片刻 后,所有的jar文件都会复制到 Web rooteB-INFllib目录,这时候点击并展开 Package Explorer视图中项目的 Referenced libraries节点就可以看到所有的 Spring和 Hibernate 相关的jar文件都已经添加完毕了。 115 Spring整合 Hibernate 在10.52 Hibernate整合 Spring开发节我们已经详细讨论了 Spring整合 Hibernate的步骤和不同的方式。为了简化开发,我们将会使用10525用 Spring2.0的 @ Transactiona标注解决事务提交问题(最佳方案)一节的标注方式来生成DAO层。具 体步骤和代码请参考那一节的介绍即可。为了让大家有个对照,我这里把DAO类代码,配 置文件源码以及测试类的代码贴出来供参考用,当然为了节省版面,我已经去掉了绝大部分 无用的注释。 先贴的是两个配置文件的,包括 Spring的和 Hibernate的。 Hibernate配置文件 hibernate. cfg xml sion=1.0 encoding=UTE-82> 109 "-//Hibernate/Hibernate Configuration DTD 3.0//EN Whttp://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd <hibernate-configuration> <session-factory> <property name="connection. username">root</property> 刘长炯著
MyEclipse 6 Java 开发中文教程 12 刘长炯著 11.4 添加 Spring 功能 最后这个项目需要加入的内容就是Spring功能了,详细的操作过程可以参考 10.5.2.1 创建项目,添加必要的开发功能一节内容。 选择菜单 MyEclipse > Project Capabilities > Add Spring Capabilities ... 来启动 Add Spring Capabilities 向导。在这个向导的第一页有两个地方需要设置:第一个地方是 选择类库的时候要选中Spring ORM 和Spring Web 的包,在Select the libraries to add to the buildpath(选择添加到类路径的类库)一栏的下侧,选中 Spring 2.0 Persistence Core Libraries 和 Spring 2.0 Web Libraries;第二个地方是在 JAR Library Installation(JAR 类库安装)处点击选中单选钮 Copy checked Library contents to project folder (TLDs always copied),这个选项将选中的类库的 JAR 文件复制到项目目录,在此单选钮下方的 Library Folder 会自动选中/WebRoot/WEB-INF/lib 目录,点击 Next 按钮进入第二页。那 么第二页选择 Spring 配置文件的位置保持原来的不变即可,再点击进入第三页设置 LocalSessionFactory,这一页也保持默认,然后点击 Finish 按钮结束整个向导。稍等片刻 后,所有的 jar 文件都会复制到/WebRoot/WEB-INF/lib 目录,这时候点击并展开 Package Explorer 视图中项目的 Referenced Libraries 节点就可以看到所有的 Spring 和 Hibernate 相关的 jar 文件都已经添加完毕了。 11.5 Spring 整合 Hibernate 在 10.5.2 Hibernate 整合 Spring 开发 一节我们已经详细讨论了 Spring 整合 Hibernate 的步骤和不同的方式。为了简化开发,我们将会使用 10.5.2.5 用 Spring 2.0 的 @Transactional 标注解决事务提交问题(最佳方案)一节的标注方式来生成 DAO 层。具 体步骤和代码请参考那一节的介绍即可。为了让大家有个对照,我这里把 DAO 类代码,配 置文件源码以及测试类的代码贴出来供参考用,当然为了节省版面,我已经去掉了绝大部分 无用的注释。 先贴的是两个配置文件的,包括 Spring 的和 Hibernate 的。 Hibernate 配置文件 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.username">root</property>
MyEclipse6Java开发中文教程 jdbc: mysql: //localhost: 3306/test?useUnicode=true & amp; chara cterEncoding=GBK </ <property name="dialect"> org. hibernate. dialect. MySQLDialect <property name="myeclipse connection profile">mysql5</property> <property name="connection driver class com.mysql. jdbc Drive </property> <mapping resource="dao/student hbm xm </session-factory> </hibernate-configuration> Spring配置文件 applicationContext. xml ? xmI version=”1.0" encoding="UTE-8”2> beansxmlns="http:i/www.springframework.org/schema/beans xmIn i=http://www.w3.org/2001/xmlschema-instance xmlns:aop=thttp://www.springframeworkorg/schema/aop xmins:tx=http://www.springframeworkorg/schema/tx" xsi: schemalocation= http://www.springframeworkorg/schema/beans ttp://www.springframeworkorg/schema/beans/spring-beans-2.0.xsd http://www.springframeworkorg/schema/tx http://www.springframeworkorg/schema/tx/spring-tx-2.0.xsd http://www.springframeworkorg/schema/aop http://www.springframeworkorg/schema/aop/spring-aop-2.0.xsd"> <tx: annotation-driven transaction-manager="transactionManager proxy-target-class="true"/> 刘长炯著
MyEclipse 6 Java 开发中文教程 13 刘长炯著 <property name="connection.url"> jdbc:mysql://localhost:3306/test?useUnicode=true&amp;chara cterEncoding=GBK </property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="myeclipse.connection.profile">mysql5</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <mapping resource="dao/Student.hbm.xml" /> </session-factory> </hibernate-configuration> Spring 配置文件 applicationContext.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
MyEclipse6Java开发中文教程 <bean id="sessionFactory class=org. springframework orm hibernate3. LocalSessionFactoryB value="classpath: hibernate. cfg.xm"> <bean id="StudentdAo class="dao. student DAo> <property name="sessionFactory"> sessionFactory"/> </property> </bean> 1-声明一个B1x3理器供代理类自动管理事务用 <bean id="transactionmanage class="org. springframework orm hibernate3. HibernateTransactio Manager> sessionFactory> local="sessionFactory "/> operty> </beans> 实体类映射配置文件dao/ Studenthbm. xml <?xml version="1.0" encoding="utf-8"?> < DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 刘长炯著
MyEclipse 6 Java 开发中文教程 14 刘长炯著 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryB ean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> <bean id="StudentDAO" class="dao.StudentDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!-- 声明一个 Hibernate 3 的 事务管理器供代理类自动管理事务用 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransaction Manager"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> </beans> 实体类映射配置文件 dao/Student.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">
MyEclipse6Java开发中文教程 <hibernate-mapping> <class name="dao. Student" table="student"catalog="test"> <id name="id"type="java. lang Integer"> <column name="id"/> <generator class="increment"/> <property name=username" type="java. lang string"> <column name="username"length="200 not-null="true/ </property> <property name=password type="java. lang str <column name="password"length="20" not-null="true"/ </property> <property name=age" type=java. langInteger"> <column name="age"/> </class> </hibernate-mapping> DAO层实体类 dao. Student java package dao Student实体类 public class student implements java. io Serializable private eger private Us tring username private Integer age public Student()( public Student (Integer id, String username, String password) this. id this username username this password 15 刘长炯著
MyEclipse 6 Java 开发中文教程 15 刘长炯著 <hibernate-mapping> <class name="dao.Student" table="student" catalog="test"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="increment" /> </id> <property name="username" type="java.lang.String"> <column name="username" length="200" not-null="true" /> </property> <property name="password" type="java.lang.String"> <column name="password" length="20" not-null="true" /> </property> <property name="age" type="java.lang.Integer"> <column name="age" /> </property> </class> </hibernate-mapping> DAO 层实体类 dao.Student.java: package dao; /** * Student 实体类. */ public class Student implements java.io.Serializable { private Integer id; private String username; private String password; private Integer age; public Student() { } public Student(Integer id, String username, String password) { this.id = id; this.username = username; this.password = password; }