mysql 5.1 变量专题
2015-06-03 18:48
363 查看
jb下开发struts hibernate心得- -
jbuilder下开发struts+hibernate心得
一、用jbuilder建立一个struts项目(略)
二、使用Middlegen生成需要的*.hbm.xml文件,并拷贝到src目录下,同时在jb中将xml文件定义为copy
1、确保需要的数据库驱动存在于lib目录下
2、在config\database目录下配置相应的数据库配置文件
3、修改build.xml文件
<!DOCTYPE project [<!ENTITY database SYSTEM "file:./config/database/mysql.xml(数据库配置)">]>
<property name="name" value="youtian(项目名称)"/>
4、使用ant编译运行
5、手动修改生成的文件
三、将需要的jar包导入项目(hibernate、dbdriver)
四、编写hibernate.cfg.xml,置于src目录下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">false</property>
<property name="use_outer_join">true</property>
<property name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool.size">20</property>
<property name="session_factory_name">hibernate/session_factory(jndi名)</property>
<!-- Mapping files -->
<mapping resource="User.hbm.xml"/>
(所有的*.hbm.xml文件都添加到这里)
</session-factory>
</hibernate-configuration>
五、编写HibernatePlugIn.java文件
import org.apache.struts.action.PlugIn;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.config.ModuleConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
public class HibernatePlugIn
implements PlugIn {
public void destroy() {
}
public void init(ActionServlet servlet, ModuleConfig config) throws
ServletException {
try {
ServletContext context = servlet.getServletContext();
SessionFactory sf = new Configuration().configure().buildSessionFactory();
context.setAttribute("net.sf.hibernate.SessionFactory",sf);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
六、修改struts-config.xml文件,加入plugin
<plug-in className="HibernatePlugIn">
<set-property property="configFilePath" value="/hibernate.cfg.xml" />
<set-property property="storeInServletContext" value="true" />
</plug-in>
</struts-config>
七、使用hibernate
Context ctx = new InitialContext();
SessionFactory sf = (SessionFactory) ctx.lookup("hibernate/session_factory");
Session s = sf.openSession();
……
注:这部分可以抽象出来
jbuilder下开发struts+hibernate心得
一、用jbuilder建立一个struts项目(略)
二、使用Middlegen生成需要的*.hbm.xml文件,并拷贝到src目录下,同时在jb中将xml文件定义为copy
1、确保需要的数据库驱动存在于lib目录下
2、在config\database目录下配置相应的数据库配置文件
3、修改build.xml文件
<!DOCTYPE project [<!ENTITY database SYSTEM "file:./config/database/mysql.xml(数据库配置)">]>
<property name="name" value="youtian(项目名称)"/>
4、使用ant编译运行
5、手动修改生成的文件
三、将需要的jar包导入项目(hibernate、dbdriver)
四、编写hibernate.cfg.xml,置于src目录下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">false</property>
<property name="use_outer_join">true</property>
<property name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool.size">20</property>
<property name="session_factory_name">hibernate/session_factory(jndi名)</property>
<!-- Mapping files -->
<mapping resource="User.hbm.xml"/>
(所有的*.hbm.xml文件都添加到这里)
</session-factory>
</hibernate-configuration>
五、编写HibernatePlugIn.java文件
import org.apache.struts.action.PlugIn;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.config.ModuleConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
public class HibernatePlugIn
implements PlugIn {
public void destroy() {
}
public void init(ActionServlet servlet, ModuleConfig config) throws
ServletException {
try {
ServletContext context = servlet.getServletContext();
SessionFactory sf = new Configuration().configure().buildSessionFactory();
context.setAttribute("net.sf.hibernate.SessionFactory",sf);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
六、修改struts-config.xml文件,加入plugin
<plug-in className="HibernatePlugIn">
<set-property property="configFilePath" value="/hibernate.cfg.xml" />
<set-property property="storeInServletContext" value="true" />
</plug-in>
</struts-config>
七、使用hibernate
Context ctx = new InitialContext();
SessionFactory sf = (SessionFactory) ctx.lookup("hibernate/session_factory");
Session s = sf.openSession();
……
注:这部分可以抽象出来
相关文章推荐
- Mysql 数据库文件存储在哪个目录
- VC使用mysql API 连接mysql数据库
- MySQL字符集问题
- Mysql PXC 问题从节点不能启动问题
- MySql之char与varchar
- Navicat for Mysql 只导出数据表结构(.sql文件)的方法
- (5)mysql优化之MyISAM表锁
- 利mysql-binlog日志恢复数据库
- MFC连接mysql出错
- MYSQL字符集设置
- MySQL运行状态show status中文详解
- ubuntu 安装MySQL
- 在 Mac 下用 Homebrew 安装 MySQL
- mysql同步复制
- How to completely uninstall mysql in ubuntu
- MySQL开发规范
- linix下的mysql源码安装
- mysql 按日期查询
- MySQL多字符集备份恢复测试
- 基于Mysql C Api编写mysql客户端程序 多线程