导航:首页 > 源码编译 > hbm源码

hbm源码

发布时间:2025-01-19 07:16:19

java项目中怎样看使用的是什么框架啊

1、首先使用开发工具打开以前练手的项目,如下图所示。

② 关于spring整合hibernate的时候自动创建表的问题

<prop key="hibernate.hbm2ddl.auto">update</prop>
漏了"hibernate",若没写全会被框架认为没配置,所以不会导出schema;

关于update和create:
(1)update 但schema发生改变时进行更新,比如添加字段,保留原有数据;
(2)create 每次运行重新创建schema,如果表存在,先删除再创建,原有数据丢失;
update也会创建表,以下是配置>update<执行的代码(hibernate的源码):
if ( table.isPhysicalTable() ) {

TableMetadata tableInfo = databaseMetadata.getTableMetadata(
table.getName(),
( table.getSchema() == null ) ? defaultSchema : table.getSchema(),
( table.getCatalog() == null ) ? defaultCatalog : table.getCatalog(), table.isQuoted()
);
if ( tableInfo == null ) {
script.add(
table.sqlCreateString(
dialect,
mapping,
defaultCatalog, defaultSchema
)
);
}
else {
Iterator<String> subiter = table.sqlAlterStrings(
dialect,
mapping,
tableInfo,
defaultCatalog,
defaultSchema
);
while ( subiter.hasNext() ) {
script.add( subiter.next() );
}
}

Iterator<String> comments = table.sqlCommentStrings( dialect, defaultCatalog, defaultSchema );
while ( comments.hasNext() ) {
script.add( comments.next() );
}

}
}

表若不存在,也会被创建.

③ struts2集成Spring,Hibernate的问题!!

部分搭建过程及源码:
1.先组合实现Hibernate3.2+Spring2.5支持,删除hibernate.cfg.xml文件,修改applicationContext.xml文件的内容,增加SessionFactory和dataSource的设置。
2.通过MyEclipse的向导方式,生成POJO类和对应的映射文件。
3.修改applicationContext.xml文件中<property name="mappingResources">元素的内容。
4.编写DAO接口和实现类。
5.修改applicationContext.xml文件,增加对Dao实现类的配置。
6.组合Struts2和Spring2.5,修改web.xml文件,增加struts2的所需要的过滤器配置。
7.增加struts2相应类库,增加struts2与spring的配置jar包。
8.拷贝struts.xml文件到src根目录下,再修改struts.xml文件,进行常量配置。
9.修改web.xml文件,配置Spring监听器,和上下文变量。并增加OpenSessionInViewFilter的设置。
10.写入action类。
11.配置struts.xml文件。
12.修改applicationContext.xml
13.编写Jsp文件。
14.加载运行项目。
下面是关键文件的源码:
struts.xml源码:
Code highlighting proced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- struts2委托spring管理 -->
<constant name="struts.objectFactory" value="spring"/>
<!-- /crm/emp/add.action -->
<package name="crm_employee" extends="struts-default" namespace="/emp">
<action name="add" class="addBean" method="add">
<result>add.action</result>
<result>/emp/add_suc.jsp</result>
</action>
<action name="list" class="listBean" method="list">
<result>/emp/list.jsp</result>
</action>
<action name="delete" class="deleteBean" method="delete">
<result>delete.action</result>
<result>/emp/delete_suc.jsp</result>
</action>
<action name="update" class="updateBean" method="update">
<result>update.action</result>
<result>/emp/edit_suc.jsp</result>
</action>
<action name="edit" class="editBean" method="edit">
<result>/emp/edit.jsp</result>
</action>
<!-- Add actions here -->
</package>
</struts>

web.xml源码:
Code highlighting proced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 配置spring的监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<!-- 开启监听 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 配置OpenSessionInViewFilter,必须在struts2监听之前 -->
<filter>
<filter-name>lazyLoadingFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<!-- 设置监听加载上下文 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>lazyLoadingFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

applicationContext.xml源码:
Code highlighting proced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?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.5.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">
<!-- 配置Hibernate支持 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/tables">
</property>
<property name="username" value="root"></property>
<property name="password" value="hicc"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/sy/crm/model/Employee.hbm.xml</value>
</list>
</property>
</bean>
<bean id="employeeDao"
class="com.sy.crm..hibernate.EmployeeDaoHibernate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="employeeManager"
class="com.sy.crm.service.impl.EmployeeManagerImpl">
<property name="employeeDao">
<ref bean="employeeDao" />
</property>
</bean>

<bean id="addBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
<property name="employeeManager">
<ref bean="employeeManager" />
</property>
</bean>
<bean id="listBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
<property name="employeeManager">
<ref bean="employeeManager" />
</property>
</bean>
<bean id="deleteBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
<property name="employeeManager">
<ref bean="employeeManager" />
</property>
</bean>
<bean id="updateBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
<property name="employeeManager">
<ref bean="employeeManager" />
</property>
</bean>
<bean id="editBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
<property name="employeeManager">
<ref bean="employeeManager" />
</property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- 配置事务特性,配置add,delete,update开始的方法,事务传播特性为required -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置那些类的方法进行事务管理,当前com.sy.crm.service包中的子包,
类中所有方法需要,还需要参考tx:advice的设置 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(*
com.sy.crm.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>
</beans>

add.jsp源码:
Code highlighting proced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>add page</title>
<script language="JavaScript" src="validation-framework.js"></script>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

</head>
<body>
<center>
<h3>雇员注册:</h3><br>
<h4><a href="../emp/list.action">查看所有雇员</a></h4>
<div id="error" style="color:blue; font-weight:bold;"></div>
<s:form action="add" method="post" onsubmit="return doValidate('form')" name="form" id="form">
<s:textfield name="employee.name" label="姓名" id="name"/>
<s:textfield name="employee.address" label="地址"/>
<s:textfield name="employee.phone" label="电话"/>
<s:submit value="员工注册"/>
</s:form>
</center>
</body>
</html>
list.jsp源码:

Code highlighting proced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>list employee page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<style type="text/css">
table {
border: 1px solid black;
border-collapse: collapse;
}

table thead tr th {
border: 1px solid black;
padding: 3px;
background-color: #cccccc;
}

table tbody tr td {
border: 1px solid black;
padding: 3px;
}
</style>
</head>

<body>
<center>
<h3>
雇员管理:
</h3>
<br>
<h4>
<a href="../emp/add.jsp">员工注册</a>
</h4>
<s:form action="delete" theme="simple">
<table>
<thead>
<tr>
<th>
选择
</th>
<th>
编号
</th>
<th>
姓名
</th>
<th>
电话
</th>
<th>
地址
</th>
<th>
操作
</th>
</tr>
</thead>
<tbody>
<s:iterator value="employees">
<tr>
<td>
<input type="checkbox" name="id"
value='<s:property value="id" />' />
</td>
<td>
<s:property value="id" />
</td>
<td>
<s:property value="name" />
</td>
<td>
<s:property value="phone" />
</td>
<td>
<s:property value="address" />
</td>
<td>
<a
href='<s:url action="edit"><s:param name="id" value="id" /></s:url>'>
修改 </a>
<a
href='<s:url action="delete"><s:param name="id" value="id" /></s:url>'>
删除 </a>
</td>
</tr>
</s:iterator>
</tbody>
</table>
<s:submit value="delete" />
</s:form>
</center>
</body>
</html>

④ 请问下<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> 是什么意思啊什么作用呢

<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>

可以避免启动容器时报的一个错误:
Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException


亲身体验,如下图:


看到没有正确答案就回了一下,不客气~

阅读全文

与hbm源码相关的资料

热点内容
局域网如何用ftp服务器配置 浏览:70
程序员惯性思考模式 浏览:439
如何在个税app上查身份证号 浏览:6
电视家app安装在电视上怎么安 浏览:889
怎么将pdf格式转化为图片格式 浏览:637
服务器拔掉raid卡怎么装系统 浏览:232
区域对称加密算法 浏览:245
数字转汉字php 浏览:733
安卓源码硬件驱动 浏览:208
痰证pdf 浏览:814
电脑怎么把word文档转pdf 浏览:867
程序员那么可爱有孩子了吗 浏览:480
安卓文字折叠怎么使用 浏览:885
创造一个app如何挣钱 浏览:801
php55vc11 浏览:642
抖音如何关闭苹果app充值 浏览:332
python多个文件调用 浏览:792
java算法和数据结构 浏览:465
糖豆视频的文件夹 浏览:654
php的头部文件一般在哪个文件里 浏览:560