all gone

all gone

2006年6月16日 #

Ubuntu UTF8环境下搭建中文LaTeX环境

  1. 基本包的安装
sudo apt-get install tetex-base tetex-bin tetex-extra
  1. CJK包安装
sudo apt-get install latex-cjk-chinese
  1. CJK UTF8字体生成
将下面的code复制到文件cvtfont.sh中:

#!/bin/sh
#
# Origin By Edward G.J. Lee
2001.11.25
# http:
//www.study-area.org/tips/latex/pdftex.html
# This code is Public Domain.
#
# Enhanced by intron@intron.ac,
2006.1.9
# Modified
for NetBSD by jungle@soforge.com, 2006.1.14
# Italic support added by robert.zhangle@gmail.com,
2006.5.19
#

if [ $# -ne 3 ]
then

cat
<< EOF
Usage: `basename $
0` font.ttf <TeX_Internal_Font_Name> <Tex_Font_Name>
<TeX_Internal_Font_Name>: Used to name *.enc, *.afm, *.pfb, *.tfm & *.map
<Tex_Font_Name>: Used in TeX source: \\CJKencfamily{UTF8}{Tex_Font_Name}

An example: `basename $
0` gbsn00lp.ttf gbsn00lp songpt1

EOF

exit
1
fi

if ! which ttf2pt1; then
echo
"Please install pkgsrc fonts/ttf2pt1 before you can run me."
exit
1
fi

if ! which t1asm; then
echo
"Please install pkgsrc fonts/t1utils before you can run me."
exit
1
fi

if ! which extconv; then
echo
"Please install pkgsrc chinese/CJK before you can run me."
exit
1
fi


cat
<<EOF
***************************************************************************
* Please confirm that current working directory is EMPTY except scripts *
* and HAS ENOUGH SPACE to contain so many intermediate files. *
* If it is not empty, this script may DAMAGE INNOCENT FILES!!! *
***************************************************************************
EOF

read
-p "Go ahead?[n]" a

if [ "$a" != "y" ] && [ "$a" != "Y" ]; then
exit
0
fi

FONTFILENAME
="$1"
FONTNAME
="$2"
FONTTEXNAME
="$3"

#TEXMFHOME
=`kpsexpand '$TEXMFHOME'`
TEXMFMAIN
=`kpsexpand '$TEXMFMAIN'`
TEXMFHOME
='/usr/share/texmf-tetex/'
TTFONTFILEDIR
=${TEXMFHOME}/fonts/truetype/chinese/
ENCDIR
=${TEXMFHOME}/fonts/enc/dvips/chinese/${FONTNAME}
AFMDIR
=${TEXMFHOME}/fonts/afm/chinese/${FONTNAME}
TFMDIR
=${TEXMFHOME}/fonts/tfm/chinese/${FONTNAME}
PFBDIR
=${TEXMFHOME}/fonts/type1/chinese/${FONTNAME}
MAPDIR
=${TEXMFHOME}/fonts/map/dvips/chinese

UPDMAPCFG
=${TEXMFHOME}/web2c/updmap.cfg

if [ ! -f $UPDMAPCFG ]
then
SYSUPDMAPCFG
=`kpsewhich -format 'web2c files' updmap.cfg`
cp $SYSUPDMAPCFG $UPDMAPCFG
fi

CIDXMAP
=${TEXMFHOME}/dvipdfm/config/cid-x.map

if [ ! -f $CIDXMAP ]
then
touch $CIDXMAP
fi

CJKDIR
=${TEXMFMAIN}/tex/latex/CJK/UTF8

INSTALLDIRMODE
="-m 0755"
INSTALLMODE
="-m 0644"

FONTFILEBASENAME
=`basename "$FONTFILENAME"`
FDNAME
=c70${FONTTEXNAME}.fd

#MAPFILE
=/usr/local/share/ttf2pt1/maps/cugb.map
PLANENUM
=255

# Initialize ${FONTNAME}.map
echo
-n "" > ${FONTNAME}.map


n
=0

echo
"Generating subfonts from plane $n to plane $PLANENUM ..."

while [ $n -le $PLANENUM ]
do
m
=`printf %02x $n`

echo
-n "[$n]"

SUBFONTNAME
=${FONTNAME}${m}

# Generate
*.t1a *.afm *.enc
ttf2pt1
-GAE -pft -OHUBs -W0 -l plane+pid=3,eid=1,0x$m \\
"$FONTFILENAME" ${SUBFONTNAME}

# avoid dvips(k)(before v5.
86) t1part module bug.
perl
-pi -e 's/_/Z/g' ${SUBFONTNAME}.t1a ${SUBFONTNAME}.afm

# Generate
*.pfb
t1asm
-b ${SUBFONTNAME}.t1a > ${SUBFONTNAME}.pfb 2>/dev/null

# Generate
*.tfm
afm2tfm ${FONTNAME}$m.afm ${FONTNAME}$m.tfm
>/dev/null
afm2tfm ${FONTNAME}$m.afm
-s .167 ${FONTNAME}sl$m.tfm >/dev/null

# Generate ${FONTNAME}.map
AFMNAME
=`grep -m 1 -i fontname ${SUBFONTNAME}.afm | cut -d ' ' -f 2`
echo
"${FONTNAME}$m ${AFMNAME} <${FONTNAME}$m.pfb" >> ${FONTNAME}.map
echo
"${FONTNAME}sl$m ${AFMNAME} \\" .167 SlantFont \\" <${FONTNAME}$m.pfb" \\
>> ${FONTNAME}.map

n
=`expr $n + 1`
done

echo

cat
> ${FDNAME} << EOF
% This file is automatically generated by `basename $0`
\\\\def\\\\fileversion{
0.0.1}
\\\\def\\\\filedate{`date
+%Y/%m/%d`}
\\\\ProvidesFile{${FDNAME}}[\\\\filedate\\\\space\\\\fileversion]

\\\\DeclareFontFamily{C70}{${FONTTEXNAME}}{\\\\hyphenchar \\\\font\\\\m@ne}

\\\\DeclareFontShape{C70}{${FONTTEXNAME}}{m}{n}{
<-> CJK * ${FONTNAME}}{}
\\\\DeclareFontShape{C70}{${FONTTEXNAME}}{bx}{n}{
<-> CJKb * ${FONTNAME}}{\\\\CJKbold}
\\\\DeclareFontShape{C70}{${FONTTEXNAME}}{m}{it}{
<-> CJK * ${FONTNAME}sl}{}
\\\\DeclareFontShape{C70}{${FONTTEXNAME}}{bx}{it}{
<-> CJKb * ${FONTNAME}sl}{\\\\CJKbold}
\\\\DeclareFontShape{C70}{${FONTTEXNAME}}{m}{sl}{
<-> CJK * ${FONTNAME}sl}{}
\\\\DeclareFontShape{C70}{${FONTTEXNAME}}{bx}{sl}{
<-> CJKb * ${FONTNAME}sl}{\\\\CJKbold}

\\\\endinput
EOF


#exit
0;

echo
echo
"Installing ..."

install
-d $INSTALLDIRMODE $TTFONTFILEDIR $ENCDIR $AFMDIR $TFMDIR $PFBDIR $MAPDIR $CJKDIR

install $INSTALLMODE $FONTFILENAME $TTFONTFILEDIR

n
=0
while [ $n -le $PLANENUM ]
do
m
=`printf %02x $n`
echo
-n "[$n]"

install $INSTALLMODE ${FONTNAME}${m}.enc $ENCDIR
install $INSTALLMODE ${FONTNAME}${m}.afm $AFMDIR
install $INSTALLMODE ${FONTNAME}${m}.tfm $TFMDIR
install $INSTALLMODE ${FONTNAME}sl${m}.tfm $TFMDIR
install $INSTALLMODE ${FONTNAME}${m}.pfb $PFBDIR

n
=`expr $n + 1`
done

echo

install $INSTALLMODE ${FONTNAME}.map $MAPDIR
install $INSTALLMODE $FDNAME $CJKDIR

echo
>> $UPDMAPCFG
echo
"Map ${FONTNAME}.map" >> $UPDMAPCFG

# update cid
-x.map

echo
"${FONTNAME}@Unicode@ unicode :0:${FONTFILENAME}" >> $CIDXMAP
echo
"${FONTNAME}sl@Unicode@ unicode :0:${FONTFILENAME}" >> $CIDXMAP

mktexlsr

#updmap
-sys
updmap

read
-p "Do you want remove all intermediate files?[n]" a
if [ "$a" = "y" ] || [ "$a" = "Y" ]; then

n
=0
while [ $n -le $PLANENUM ]
do
m
=`printf %02x $n`
echo
-n "[$n]"

rm
-f ${FONTNAME}${m}.enc ${FONTNAME}${m}.afm ${FONTNAME}${m}.t1a \\
${FONTNAME}${m}.pfb ${FONTNAME}sl${m}.tfm ${FONTNAME}${m}.tfm

n
=`expr $n + 1`
done

echo

rm
-fv $FDNAME ${FONTNAME}.map
fi

echo
echo
"OK, all done. :-)"
echo


根据自己的需要可以将TEXMFHOME目录设置成不同的地方,只要Latex能找到即可
我将该目录改为了/usr/share/texmf-tetex,如果相应的目录不存在,则需要自己新建。


将windows系统中的字体文件拷到脚本所在目录执行脚本即可生成Latex所需字体文件,以simhei.ttf为例:

sudo ./cvtfont.sh simhei.ttf simhei simhei







posted @ 2007-05-16 13:34 all gone 阅读(1114) | 评论 (0)编辑 收藏

Struts2 入门

     摘要: 很久没有看Java相关的东西了,这几天才发现Struts2 已经发布了,以前就听说Struts2就是以前的Webwork2,只是作了少许的改动而已,我以前也没看过Webwork,所以赶紧下来试了一把。Struts2的地址:http://struts.apache.org/2.x在Eclipse中新建了一个Tomcat工程(如果用WTP插件的话Dynamic Web工程)struts2Test,以下...  阅读全文

posted @ 2007-01-08 21:59 all gone 阅读(8459) | 评论 (8)编辑 收藏

Ubuntu下NVIDIA驱动的问题

前几天按照Ubuntu How-To上的NVIDIA驱动安装说明安装了8672版的驱动,结果发现只要退出X-Window,系统就会没有反应,网上搜了搜,发现有人也有一样的情况,按照说明重新安装、重新配置Xorg,问题依然没有解决,X-Window也起不来了。

到NVIDIA网站上下了最新的8774版驱动后,赶紧找找看怎么自己装,有人已经写好了
在恢复模式下:
需要gcc支持,如果没有安装则:
apt-get install build-essential gcc
删除已经安装的NVIDIA驱动:
apt-get --purge remove linux-restricted-modules-`uname -r` linux-restricted-modules-common nvidia-glx nvidia-settings nvidia-kernel-common
安装内核headers
apt-get install linux-headers-`uname -r`
将下载下来的NVIDIA-Linux-x86-1.0-8774-pkg1.run解压缩:
sh ./NVIDIA-Linux-x86-1.0-8774-pkg1.run --extract-only
进入解压后的NVIDIA-Linux-x86-1.0-8774-pkg1文件夹,执行安装:
./nvidia-installer -n --x-prefix=/usr/lib/xorg/
最后由于默认安装到了/usr/lib/xorg/lig/xorg,需要将文件拷到对应的目录下:
cp --no-dereference /usr/lib/xorg/lib/xorg/libX* /usr/lib/xorg/modules/

cp /usr/lib/xorg/lib/xorg/modules/drivers/* /usr/lib/xorg/modules/drivers/

cp --no-dereference /usr/lib/xorg/lib/xorg/modules/extensions/* /usr/lib/xorg/modules/extensions/

posted @ 2006-09-22 15:42 all gone 阅读(1043) | 评论 (0)编辑 收藏

Qt 4下连接MySQL数据库

前天刚装了个Qt4for windows,发现安装的时候MySQL等数据库插件都没有安装,像Qt X11版一样,都需要自己安装这些插件。

在Qt的安装目录C:\Qt4.1.2\下,有个src目录,进入plugins\sqldrivers\mysql,可以看到两个文件,main.cpp和mysql.pro,很显然要用qmake来完成编译、生成目标。

编译前,首先保证依赖的MySQL头文件和库文件正确。首先MySQL安装目录下的include中的所有文件拷到qmake时能找到的目录下,接着从lib/opt目录下拷出libmysql.a,其实lib/opt下只有libmysql.lib和libmysql.dll,从网上搜了一下找到一个方法从libmysql.dll生成libmysql.a:

1 、安装好MySQL (如果不愿意安装,找个libmySQL.dll文件也可以)

2、下载Pexports工具

3、转换操作: pexports libmysql.dll > libmysql.def

4、使用MinGW的 dlltool转换成为libmysql.a文件。dlltool --input-def libmysql.def --dllname libmysql.dll --output-lib libmysql.a -k

5、尝试是否成功:
将生成的libmysql.a 拷到qmake的lib搜索路径下后在plugins\sqldrivers\mysql目录下做:
qmake -project
qmake
make

如果再出现象mysql_connect@xx (xx是数字)的错误提示。就执行第6步。

6、修改libmysql.def文件,给mysql_connect加上@xx,即mysql_connect@xx

7、重做第4步

8、然后尝试第5步,如果还是出现错误提示。就作第6步。一直到没有错误为止。

最后C:\Qt4.1.2\plugins\sqldrivers下将会生成需要的libqsqlmysql.a和qsqlmysql.dll。

找了一个测试文件,通过。
#include <QtSql> 
#include <QtGui> 


bool createConnection(){ 
QSqlDatabase db 
= QSqlDatabase::addDatabase("QMYSQL"); 
db.setHostName("localhost"); 
db.setDatabaseName("test"); 
db.setUserName("test"); 
db.setPassword(""); 

if(!db.open()){ 
QMessageBox::critical(
0, QObject::tr("Database Error"), 
                              db.lastError().text())
; 

return false
; 


QSqlQuery query
; 
//query.exec("insert into book values( 3, 'title', 'author') "); 
query.exec("select title , author from book "); 

while (query.next()) { 


    QString title 
= query.value(0).toString(); 
    QString author = query.value(1).toString(); 

QMessageBox::critical(
0, title,author); 
    



return true
; 




int main(int argc
,char** argv){ 

QApplication app(argc
, argv); 
    if (!createConnection()){ 

return 
1; 

        

    return app.exec()
; 




posted @ 2006-08-28 20:57 all gone 阅读(2726) | 评论 (0)编辑 收藏

EJB3.0 + Struts小试

     摘要: 一直想做一个 EJB3.0 结合 Struts 的例子,前天刚好从网上找到了一个相关的 Tutorial ,于是小试了一把,虽然只是完成了 Tutorial 上的一小点功能,但是感觉还是有必要写下来,免得以后还要再去看别人的Tutorial 。 开发环境:       Eclipse3.2 + WTP1.5 , JBoss 4.0.3SP1 , jboss-EJB-3.0_RC...  阅读全文

posted @ 2006-08-11 13:52 all gone 阅读(3060) | 评论 (0)编辑 收藏

SSH(Spring + Struts + Hibernate)小试 之三

SSH(Spring + Struts + Hibernate)小试 之三


前面通过PlugIn我们实现了Spring Context的加载,不过仅仅加载Context并没有什么实际
意义,我们还需要修改配置,将Struts Action交给Spring容器进行管理。下面将通过一个Regsit
实例加以说明。

首先准备好regist.jsp和login.jsp,在regist.jsp中 的form有name,password,password2,email域。

在struts-config.xml中添加配置:

<form-bean name="registForm" type="com.lzy.forum.form.RegistForm" />

<action path="/regist" name="registForm"
type="org.springframework.web.struts.DelegatingActionProxy"

validate="true" input="/regist.jsp" scope="request">
<forward name="failure" path="/regist.jsp" />
<forward name="success" path="/login.jsp" />
</action>


RegistForm,RegistAction按照原来Struts的方法去写,我在RegistAction中加入了一个UserDAO对象
userDAO,这个对象由Spring注入。

<bean name="/regist" class="com.lzy.forum.action.RegistAction"
singleton="false">
<property name="userDAO">
<ref local="userDAO" />
</property>
</bean>

基本上SSH的架构已经完成,经确认后可以自己测试一下了。(完)

posted @ 2006-07-18 16:41 all gone 阅读(641) | 评论 (1)编辑 收藏

SSH(Spring + Struts + Hibernate)小试 之二

Spring和Struts结合有几种方法,我选了最常用的PlugIn方式,在struts-config.xml中加入

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
      <set-property property="contextConfigLocation"
            value="/WEB-INF/action-servlet.xml" />
 </plug-in>

WEB-INF目录下的action-servlet.xml就是Spring的配置文件,下面给出的是整个的action-servlet.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

//Data Source

 <bean id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName">
   <value>com.mysql.jdbc.Driver</value>
  </property>
  <property name="url">
   <value>jdbc:mysql://localhost:3306/forum</value>
  </property>
  <property name="username">
   <value>test</value>
  </property>
  <property name="password">
   <null />
  </property>
 </bean>

// For Hibernate
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref local="dataSource" />
  </property>
  <property name="mappingResources">
   <list>
    <value>com/lzy/forum/domain/User.hbm.xml</value>
    <value>com/lzy/forum/domain/Article.hbm.xml</value>
    <value>com/lzy/forum/domain/Board.hbm.xml</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.MySQLDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
 </bean>

 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean>

 

// For DAO
 <bean id="userDAO"
  class="com.lzy.forum.dao.impl.UserDAOHibernate">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean>

 

 <bean id="boardDAO"
  class="com.lzy.forum.dao.impl.BoardDAOHibernate">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean>

 <bean id="articleDAO"
  class="com.lzy.forum.dao.impl.ArticleDAOHibernate">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean>

//For Struts Action

 <bean name="/regist" class="com.lzy.forum.action.RegistAction"
  singleton="false">
  <property name="userDAO">
   <ref local="userDAO" />
  </property>

 </bean>


 <bean name="/login" class="com.lzy.forum.action.LoginAction"
  singleton="false">
  <property name="userDAO">
   <ref local="userDAO" />
  </property>

 </bean>

 <bean name="/boardManage"
  class="com.lzy.forum.action.BoardManageAction" singleton="false">
  <property name="userDAO">
   <ref local="userDAO" />
  </property>
  <property name="boardDAO">
   <ref local="boardDAO" />
  </property>

 </bean>

 <bean name="/boardNavigate"
  class="com.lzy.forum.action.BoardNavigateAction" singleton="false">
  <property name="boardDAO">
   <ref local="boardDAO" />
  </property>

 </bean>

 <bean name="/boardDisplay"
  class="com.lzy.forum.action.BoardDisplayAction" singleton="false">
  <property name="boardDAO">
   <ref local="boardDAO" />
  </property>

 </bean>

 <bean name="/articleManage"
  class="com.lzy.forum.action.ArticleManageAction" singleton="false">
  <property name="userDAO">
   <ref local="userDAO" />
  </property>
  <property name="boardDAO">
   <ref local="boardDAO" />
  </property>
  <property name="articleDAO">
   <ref local="articleDAO" />
  </property>


 </bean>


</beans>

经过这样的配置之后,Hibernate事务交由Spring管理,sessionFactory从Spring容器获得。下面通过其中的一个DAO实例来看看这样做带来的方便。


//BoardDAO.java

package com.lzy.forum.dao;

import java.util.ArrayList;

import com.lzy.forum.domain.Board;

public interface BoardDAO {
 
 public boolean isBoardExist(String name);
 
 public void addBoard(Board board);
 
 public void deleteBoard(Board board);
 
 public Board loadBoard(int id);
 
 public Board loadBoard(String name);
 
 public Board loadBoardWithArticles(int id);
 
 public ArrayList getRootBoardsList();
 
 
 
}


//BoardDAOHibernate.java

package com.lzy.forum.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Set;

import org.hibernate.Hibernate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.lzy.forum.dao.BoardDAO;
import com.lzy.forum.domain.Board;

public class BoardDAOHibernate extends HibernateDaoSupport implements BoardDAO {

 public void deleteBoard(Board board) {
  // TODO Auto-generated method stub
  this.getHibernateTemplate().delete(board);
 }

 public void addBoard(Board board) {
  // TODO Auto-generated method stub
  this.getHibernateTemplate().saveOrUpdate(board);

 }

 public boolean isBoardExist(String name) {
  // TODO Auto-generated method stub
  ArrayList list = (ArrayList) this.getHibernateTemplate().find(
    "from Board where name = ? ", new Object[] { name });
  if (list.size() > 0) {
   // System.out.println("find it");
   return true;
  }
  return false;
 }

 public Board loadBoard(int id) {
  // TODO Auto-generated method stub
  ArrayList list = (ArrayList) this.getHibernateTemplate().find(
    "from Board where id = ? ", new Object[] { id });

  Board b = null;
  if (list.size() > 0) {
   b = (Board) list.get(0);
  }

  return b;
 }

 public Board loadBoard(String name) {
  // TODO Auto-generated method stub
  ArrayList list = (ArrayList) this.getHibernateTemplate().find(
    "from Board where name = ? ", new Object[] { name });

  Board b = null;
  if (list.size() > 0) {
   b = (Board) list.get(0);
  }

  return b;
 }

 public ArrayList getRootBoardsList() {
  // TODO Auto-generated method stub
  ArrayList list = (ArrayList) this.getHibernateTemplate().find(
    "from Board b left join fetch b.ChildBoards where b.Parent = null order by b.id");
  System.out.println(list.size() + " root boards found ");
  

  ListIterator index = list.listIterator();
  while (index.hasNext()) {

   Board s = (Board) index.next();
   //this.getSession(true);
   //Hibernate.initialize(s.getChildBoards());
   ///*
    Set children  = (Set) s.getChildBoards();
    Iterator it = children.iterator();
   
    while(it.hasNext()){
    Board b = (Board)it.next();
    //Hibernate.initialize(b);
    System.out.println(b.getName());
    }
    //*/

   //System.out.println(children.size() + "child(ren) found ");
   //s.setChildBoards(s.getChildBoards());
  }

  return list;
 }

 public Board loadBoardWithArticles(int id) {
  // TODO Auto-generated method stub
  ArrayList list = (ArrayList) this.getHibernateTemplate().find(
    "from Board b left join fetch b.Articles where b.Id = ? ", new Object[] { id });

  Board b = null;
  if (list.size() > 0) {
   b = (Board) list.get(0);
  }

  return b;

 }


}


BoardDAOHibernate继承 HibernateDaoSupport后,通过getHibernateTemplate()得到一个HibernateTemplate实例,然后执行CRUD操作,非常简单。需要注意的是,由于执行一次CRUD操作后,Hibernate session关闭,如果有使用延迟加载策略的对象没有加载,在后面的Web层很容易出现如下错误:
failed to lazily initialize a collection of role
从网上搜到的解决方法是Open Session In View,
<filter>
     <filter-name>hibernateFilter</filter-name>
     <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
     </filter-class>

     <!-- singleSession默认为true,若设为false则等于没用OpenSessionInView -->
    <init-param>
       <param-name>singleSession</param-name>
       <param-value>true</param-value>
    </init-param>
  </filter>

   <filter-mapping>
     <filter-name>hibernateFilter</filter-name>
     <url-pattern>*.do</url-pattern>
   </filter-mapping>
我试了试,还是有一些问题,所以后来还是放弃了这种方法,所有在Web层中需要的对象都必须在Business层中加载完成。如下面的代码所示:

ArrayList list = (ArrayList) this.getHibernateTemplate().find(
    "from Board b left join fetch b.Articles where b.Id = ? ", new Object[] { id });


这样Hibernate和Spring的结合也算完成了。其他的一些DAO类和实现代码我没有给出,但是大同小异,和Board的实现类似。


最后将是Struts 和Spring的结合,将在下一篇给出。(未完待续)





posted @ 2006-07-18 16:09 all gone 阅读(944) | 评论 (0)编辑 收藏

SSH(Spring + Struts + Hibernate)小试 之一

之前对Spring Struts Hibernate都稍有了解,Spring的IOC给我留下了很深的印象,容器管理的对象生成
方法极大地简化了程序逻辑,Spring也提供了用于Web的Spring MVC 框架,由于我感觉其易用性还是不及
Struts,所以我还是选择Struts作为前端WEB框架,Hibernate我一直没有在Web应用中试过,这次当然不会
错过机会了。


开发环境的塔建我就不多说了,具体可以参考我以前的几篇日志。

在Eclipse中新建一个动态Web工程,将Struts 、Spring、Hibernate需要的jar都添加到WEB-INF/lib目录下,
将Struts标签所需的tld文件、struts-config.xml文件(可以由StrutsIDE生成)添加到WEB-INF下,另外在
此目录下添加一个Spring的配置文件applicationContext.xml。


我参考的例子是夏昕的《深入浅出Hibernate》上的第6章,很好的一个论坛示例。

首先按照书上的ER模型,在数据库(MySQL)中建立相应的数据库。


create database forum;

use forum;

create table user(
 id int not null auto_increment primary key,
 name varchar(50),
 pwd varchar(50),
 email varchar(50)

)type=innodb;


create table board(
 id int not null auto_increment primary key, 
 parent_id int,
 create_by int not null,
 name varchar(50) not null,
 remark varchar(255),
 create_time datetime,

 index(parent_id),
 index(create_by),

 foreign key(parent_id) references board(id) on delete cascade,
 foreign key(create_by) references user(id) on delete no action
 
)type=innodb;


create table article(
 id int not null auto_increment primary key, 
 parent_id int ,
 board_id int not null,
 article_type int not null,
 title varchar(255),
 body text,
 create_by int not null,

 create_time datetime,
 hits int unsigned,
 bytes int unsigned,
 last_update_by int not null,
 last_update_time datetime,

 index(parent_id),
 index(board_id),
 index(create_by),
 index(last_update_by),

 foreign key(parent_id) references article(id) on delete no action,
 foreign key(board_id) references board(id) on delete cascade,
 foreign key(create_by) references user(id) on delete no action,
 foreign key(last_update_by) references user(id) on delete no action
 
)type=innodb;

之后,我选择了用Hibernate Sychonizer来生成映射文件和实体类,然后对生成的类进行修改(删掉了生成的Base类),
实际上Hibernate Sychonizer生成的映射文件也有错误,需要修改,我也是后来在使用中发现的。

//User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " >

<hibernate-mapping package="com.lzy.forum.domain">
 <class
  name="User"
  table="user"
 >

  <id
   name="Id"
   type="integer"
   column="id"
  >
   <generator class="native"/>
  </id>

  <property
   name="Name"
   column="name"
   type="string"
   not-null="false"
   length="50"
  />
  <property
   name="Pwd"
   column="pwd"
   type="string"
   not-null="false"
   length="50"
  />
  <property
   name="Email"
   column="email"
   type="string"
   not-null="false"
   length="50"
  />
 

  <set name="ArticlesByLastUpdateBy" inverse="true" lazy="true">
   <key column="last_update_by"/>
   <one-to-many class="Article"/>
  </set>

  <set name="ArticlesByCreateBy" inverse="true" lazy="true">
   <key column="create_by"/>这里的create_by是后来修改的,Hibernate Sychonizer生成的有错,后面有几处同样的错误】
   <one-to-many class="Article"/>
  </set>

  <set name="Boards" inverse="true" lazy="true">
   <key column="create_by"/>【这里的create_by是后来修改的】

   <one-to-many class="Board"/>
  </set>


 </class> 
</hibernate-mapping>

//User.java
package com.lzy.forum.domain;


public class User{
 private static final long serialVersionUID = 1L;

//  primary key
 private java.lang.Integer id;

 // fields
 private java.lang.String name;
 private java.lang.String pwd;
 private java.lang.String email;

 // collections
 private java.util.Set<Article> articlesByLastUpdateBy;
 private java.util.Set<Article> articlesByCreateBy;
 private java.util.Set<Board> boards;
 
 
 public java.util.Set<Article> getArticlesByCreateBy() {
  return articlesByCreateBy;
 }
 public void setArticlesByCreateBy(java.util.Set<Article> articlesByCreateBy) {
  this.articlesByCreateBy = articlesByCreateBy;
 }
 public java.util.Set<Article> getArticlesByLastUpdateBy() {
  return articlesByLastUpdateBy;
 }
 public void setArticlesByLastUpdateBy(
   java.util.Set<Article> articlesByLastUpdateBy) {
  this.articlesByLastUpdateBy = articlesByLastUpdateBy;
 }
 public java.util.Set<Board> getBoards() {
  return boards;
 }
 public void setBoards(java.util.Set<Board> boards) {
  this.boards = boards;
 }
 public java.lang.String getEmail() {
  return email;
 }
 public void setEmail(java.lang.String email) {
  this.email = email;
 }
 public java.lang.Integer getId() {
  return id;
 }
 public void setId(java.lang.Integer id) {
  this.id = id;
 }
 public java.lang.String getName() {
  return name;
 }
 public void setName(java.lang.String name) {
  this.name = name;
 }
 public java.lang.String getPwd() {
  return pwd;
 }
 public void setPwd(java.lang.String pwd) {
  this.pwd = pwd;
 }


}

//Board.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " >

<hibernate-mapping package="com.lzy.forum.domain">
 <class
  name="Board"
  table="board"
 >
   <meta attribute="sync-DAO">false</meta>
  <id
   name="Id"
   type="integer"
   column="id"
  >
   <generator class="native"/>
  </id>

  <property
   name="Name"
   column="name"
   type="string"
   not-null="true"
   length="50"
  />
  <property
   name="Remark"
   column="remark"
   type="string"
   not-null="false"
   length="255"
  />
  <property
   name="CreateTime"
   column="create_time"
   type="timestamp"
   not-null="false"
   length="19"
  />
  <many-to-one
   name="CreateBy"
   column="create_by"
   class="User"
   not-null="true"
  >
  </many-to-one>
  <many-to-one
   name="Parent"
   column="parent_id"
   class="Board"   
   not-null="false"
   lazy="false">
  </many-to-one>
 

  <set name="Articles" inverse="true" lazy="true" order-by="id">
   <key column="board_id"/>【这里的board_id是后来修改的】
   <one-to-many class="Article"/>
  </set>

  <set name="ChildBoards" inverse="true"  lazy="true" order-by="id">
   <key column="parent_id"/>这里的parent_id是后来修改的】
   <one-to-many class="Board"/>
  </set>


 </class> 
</hibernate-mapping>

//Board.java
package com.lzy.forum.domain;

 


public class Board  {
 private static final long serialVersionUID = 1L;

//  primary key
 private java.lang.Integer id;

 // fields
 private java.lang.String name;
 private java.lang.String remark;
 private java.util.Date createTime;

 // many to one
 private com.lzy.forum.domain.User createBy;
 private com.lzy.forum.domain.Board parent;

 // collections
 private java.util.Set<com.lzy.forum.domain.Article> articles;
 private java.util.Set<com.lzy.forum.domain.Board> childBoards;
 
 
 public java.util.Set<com.lzy.forum.domain.Article> getArticles() {
  return articles;
 }
 public void setArticles(java.util.Set<com.lzy.forum.domain.Article> articles) {
  this.articles = articles;
 }
 public java.util.Set<com.lzy.forum.domain.Board> getChildBoards() {
  return childBoards;
 }
 public void setChildBoards(java.util.Set<com.lzy.forum.domain.Board> childBoards) {
  this.childBoards = childBoards;
 }
 public com.lzy.forum.domain.User getCreateBy() {
  return createBy;
 }
 public void setCreateBy(com.lzy.forum.domain.User createBy) {
  this.createBy = createBy;
 }
 public java.util.Date getCreateTime() {
  return createTime;
 }
 public void setCreateTime(java.util.Date createTime) {
  this.createTime = createTime;
 }
 public java.lang.Integer getId() {
  return id;
 }
 public void setId(java.lang.Integer id) {
  this.id = id;
 }
 public java.lang.String getName() {
  return name;
 }
 public void setName(java.lang.String name) {
  this.name = name;
 }
 public com.lzy.forum.domain.Board getParent() {
  return parent;
 }
 public void setParent(com.lzy.forum.domain.Board parent) {
  this.parent = parent;
 }
 public java.lang.String getRemark() {
  return remark;
 }
 public void setRemark(java.lang.String remark) {
  this.remark = remark;
 }


}

//Article.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " >

<hibernate-mapping package="com.lzy.forum.domain">
 <class
  name="Article"
  table="article"
 >
  <meta attribute="sync-DAO">false</meta>
  <id
   name="Id"
   type="integer"
   column="id"
  >
   <generator class="native"/>
  </id>

  <property
   name="ArticleType"
   column="article_type"
   type="integer"
   not-null="true"
   length="11"
  />
  <property
   name="Title"
   column="title"
   type="string"
   not-null="false"
   length="255"
  />
  <property
   name="Body"
   column="body"
   type="string"
   not-null="false"
  />
  <property
   name="Hits"
   column="hits"
   type="integer"
   not-null="false"
   length="10"
  />
  <property
   name="Bytes"
   column="bytes"
   type="integer"
   not-null="false"
   length="10"
  />
  <property
   name="CreateTime"
   column="create_time"
   type="timestamp"
   not-null="false"
   length="19"
  />
  <property
   name="LastUpdateTime"
   column="last_update_time"
   type="timestamp"
   not-null="false"
   length="19"
  />
  <many-to-one
   name="LastUpdateBy"
   column="last_update_by"
   class="User"
   not-null="true"
  >
  </many-to-one>
  <many-to-one
   name="CreateBy"
   column="create_by"
   class="User"
   not-null="true"
  >
  </many-to-one>
  <many-to-one
   name="Parent"
   column="parent_id"
   class="Article"
   not-null="false"
  >
  </many-to-one>
  <many-to-one
   name="Board"
   column="board_id"
   class="Board"
   not-null="true"
  >
  </many-to-one>
 

  <set name="Articles" inverse="true" lazy="false" order-by="id">
   <key column="parent_id"/>【这里的parent_id是后来修改的】
   <one-to-many class="Article"/>
  </set>


 </class> 
</hibernate-mapping>



//Article.java
package com.lzy.forum.domain;

import java.util.Date;

 


public class Article  {
 private static final long serialVersionUID = 1L;
 // primary key
 private java.lang.Integer id;

 // fields
 private java.lang.Integer articleType;
 private java.lang.String title;
 private java.lang.String body;
 private java.lang.Integer hits;
 private java.lang.Integer bytes;
 private java.util.Date lastUpdateTime;
 private java.util.Date createTime;
 

 // many to one
 private com.lzy.forum.domain.User lastUpdateBy;
 private com.lzy.forum.domain.User createBy;
 private com.lzy.forum.domain.Article parent;
 private com.lzy.forum.domain.Board board;

 // collections
 private java.util.Set<com.lzy.forum.domain.Article> articles;

 public java.util.Set<com.lzy.forum.domain.Article> getArticles() {
  return articles;
 }

 public void setArticles(java.util.Set<com.lzy.forum.domain.Article> articles) {
  this.articles = articles;
 }

 public java.lang.Integer getArticleType() {
  return articleType;
 }

 public void setArticleType(java.lang.Integer articleType) {
  this.articleType = articleType;
 }

 public com.lzy.forum.domain.Board getBoard() {
  return board;
 }

 public void setBoard(com.lzy.forum.domain.Board board) {
  this.board = board;
 }

 public java.lang.String getBody() {
  return body;
 }

 public void setBody(java.lang.String body) {
  this.body = body;
 }

 public java.lang.Integer getBytes() {
  return bytes;
 }

 public void setBytes(java.lang.Integer bytes) {
  this.bytes = bytes;
 }

 public com.lzy.forum.domain.User getCreateBy() {
  return createBy;
 }

 public void setCreateBy(com.lzy.forum.domain.User createBy) {
  this.createBy = createBy;
 }

 public java.lang.Integer getHits() {
  return hits;
 }

 public void setHits(java.lang.Integer hits) {
  this.hits = hits;
 }

 public java.lang.Integer getId() {
  return id;
 }

 public void setId(java.lang.Integer id) {
  this.id = id;
 }

 public com.lzy.forum.domain.User getLastUpdateBy() {
  return lastUpdateBy;
 }

 public void setLastUpdateBy(com.lzy.forum.domain.User lastUpdateBy) {
  this.lastUpdateBy = lastUpdateBy;
 }

 public java.util.Date getLastUpdateTime() {
  return lastUpdateTime;
 }

 public void setLastUpdateTime(java.util.Date lastUpdateTime) {
  this.lastUpdateTime = lastUpdateTime;
 }

 public com.lzy.forum.domain.Article getParent() {
  return parent;
 }

 public void setParent(com.lzy.forum.domain.Article parent) {
  this.parent = parent;
 }

 public java.lang.String getTitle() {
  return title;
 }

 public void setTitle(java.lang.String title) {
  this.title = title;
 }
 public java.util.Date getCreateTime(){
  return createTime;
 }

 public void setCreateTime(java.util.Date createTime) {
  // TODO Auto-generated method stub
  this.createTime = createTime;
 }
 

}

接下来将会在web中加入Spring支持和为实体类提供DAO支持。(未完待续)

posted @ 2006-07-18 09:54 all gone 阅读(1781) | 评论 (2)编辑 收藏

Eclipse + CDT + MinGW 下编写QT程序


环境:
(1)安装Eclipse + CDT + MinGW
(2)安装QT for Windows
(3)安装QT plugin for Eclipse :org.eclipse.qt-1.0.2.zip,在Preference里设好相关路径。

新建一个 QT project,将hello。cpp复制到工程中:
#include <QApplication>
#include <QPushButton>

int main(int argc, char *argv[]){
 
 QApplication app(argc, argv);
 
 QPushButton hello("Hello world!");
 
 hello.resize(100, 30);
 hello.show();
 
 return app.exec();
}

build后生成Makefile,新建两个Make Target:all,clean
Make all 后发现找不到libQtCored4.a等文件,在QT的lib目录下找到相应的libQtCore4.a,复制后重命名为libQtCored4.a,
其他库文件类似,最后编译通过,运行后出现带hello按钮的窗口。

 

posted @ 2006-06-16 19:45 all gone 阅读(1177) | 评论 (0)编辑 收藏

Eclipse + CDT + MinGW 下编写OpenGL程序


Eclipse + CDT + MinGW环境的搭建比较简单,由于机器内存不够大,我在preference里将C++的indexer都置为NO INDEXER。
安装MinGW后可以看到,在Include和Lib目录下有了与OpenGL相关的头文件和静态库。为了使用GLUT,我将glut.h放到了相应的
GL目录下,看到Lib目录下有libglut.a库文件,我没有将glut。lib放进去(后来发现libglut。a好像在windows不可用)。

新建一个Standtart Make C++ project,将原来可运行的OpenGL程序复制过来,写好Makefile,结果编译的时候还是出现错误。
还好找到了这个非常有用的网页:http://www.mingw.org/MinGWiki/index.php/Glut
按照上面列出的错误,我在每个#include<GL/glut.h>前都加上#define _STDCALL_SUPPORTED,然后又将glut32。lib、
opengl32.lib、glu32.lib都放到当前的工程目录下,用”glut32。lib opengl32.lib glu32.lib“ 代替原来的 ”-lglut -lopengl32 -lglu32",
最后终于编译通过。

 

posted @ 2006-06-16 19:33 all gone 阅读(1178) | 评论 (0)编辑 收藏