利用ant和maven-ant-tasks使用Hibernate链接数据库
来源:互联网 发布:淘宝店铺排名技巧 编辑:程序博客网 时间:2024/06/04 18:19
6月最后一天,赶紧水一篇博客(o.o)
利用hibernate链接mysql数据库,利用hibernatetool生成entity类,生成数据表。
参考资料
动物园系列的《精通Hibernate》
废话少说,一步一步来,建立我们的项目
当然前提是你要下载
ant和maven-ant-tasks,(这是可以理解为整合maven和ant的一个工具)
你也可以不用下载ant,ecplise自带有ant,
新建一个普通的java project,他的目录大概是这样的
然后在里面新建一个文件夹(folder)com.oreilly.hh.data
然后里面是我们的hibernate映射文件:
Track.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> <class name="com.oreilly.hh.data.Track" table="TRACK"> <meta attribute="class-description"> @author zhonglin_li </meta> <id name="id" type="int" column="TRACK_ID"> <meta attribute="scope-set">protected</meta> </id> <property name="title" type="string" not-null="true" /> <property name="filePath" type="string" not-null="true" /> <property name="playTime" type="time" /> <property name="added" type="date" /> <property name="volume" type="short" not-null="true" /> </class> </hibernate-mapping>
这就是将 java类与数据库中的标对应起来,
然后关键是我们的build.xml文件:
build.xml
<?xml version="1.0" encoding="UTF-8"?><project name="Harnessing Hibernate 3" default="" basedir="." xmlns:artifact="antlib:org.apache.maven.artifact.ant"> <!-- xmlns是为maven ant tasks提供的命名空间, default是build.xml默认运行的terget不写的话,就需要我们自己指定--> <property name="source.root" value="src" /> <property name="class.root" value="class" /> <property name="data.dir" value="data" /> <artifact:dependencies pathId="dependency.classpath"> <dependency groupId="org.hibernate" artifactId="hibernate" version="3.2.5.ga"> <exclusion groupId="javax.transaction" artifactId="jta" /> </dependency> <dependency groupId="org.hibernate" artifactId="hibernate-tools" version="3.2.0.beta9a" /> <dependency groupId="log4j" artifactId="log4j" version="1.2.14" /> </artifact:dependencies> <!-- 添加项目的依赖,这是告诉maven ant taasks 去 maven的中央仓库下载 hibernate,hibernate-tool 和log4j 。至于其中的奥妙,我现在也没怎么弄清楚,只知道这样写就是添加了依赖,格式什么的,看了代码就会明白的--> <path id="project.class.path"> <pathelement location="${class.root}" /> <path refid="dependency.classpath" /> </path> <!-- 写了这个 在构建build.xml时 项目就知道我们要依赖外部jar在哪去找, <path refid="dependency。classpath">这个名字要跟artifact:dependencies 不然会找不到你通过maven ant tasks 下载的依赖包 --> <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="project.class.path" /> <!-- 这是告诉ant使用我们依赖的 hibernatejar中的hibernatetool工具,classname classpathref 这个classpathref应该与破path中定义的id一样 --> <target name="codegen"> <hibernatetool destdir="${source.root}"> <configuration> <fileset dir="${source.root}"> <include name="**/*.hbm.xml" /> </fileset> </configuration> <hbm2java/> </hibernatetool> </target></project>
然后如果你的explise支持ant的话你就可以运行了,不过在这之前,我们先看看ant在ecplise中的设置。我用的是自己下载的ant jar包,
在window->preferences->ant->runtime中可以看见和设置ant_home的位置,
开始运行
然后还记得我们的maven ant tasks吧,现在是需要他的时候了
右击我们的build.xml,run as 选着第二个(一般有两个,第一个直接运行,第二个允许我们进行一些设置)
再classpath中我们将下载好的maven-ant-tasks.jar添加进去,这样我们才能使用它,(如果你下载了ant和maven ant tasks,你可以一开始就将maven ant tasks jar包放入ant保存路径的lib中,然后再ecplise中使用自己的ant,就是你保存了maven ant tasks的这个,那样你可能就不需要在ecplise中导maven ant tasks jar包的这个过程,不过还是推荐你运行build.xml时进来看看,到底有没有maven ant tasks 这个jar。)
点击run
第一次运行的时候可能会慢一些,maven ant tasks 需要从中央仓库中下载hibernate ,hibernatetool,log4j的jar 而且他还要下载自己的一些jar包,这个过程更你的网络有关,这个时候你需要一杯咖啡。当然如果你自己写这个过程也可能出错,你可以查询其他的资料,不过多检查自己的代码总是没错的,如果你是第一次写这些xml很容易出错的。
最总你肯定会完成这个项目,
刷新你的项目,你就会看见,Track.java
这就是第一阶段,利用hibernate tool生成 java 代码。
到了这一步 下面就简单了,因为困难去全在第一部分解决了,剩下的就水到渠成了。
在我们继续下面的编码之前,先来看看,maven ant tasks都做了什么 。
在window->preferences->maven中可以看见和设置maven_home
如图我设置了,使用自己的maven,
maven在本地(就是你使用的电脑)有个本地仓库,里面存放着你依赖过的所有地jar,这个本地库默认的路径是“当前用户.m2\repository”,当然你的电脑你做主,你可以修改这个路径,在maven安装路径下的
apache-maven-3.3.9\conf\settings.xml打开这个.xml,修改localRepository
之后如果你再使用这个maven那么就jar就会下载到你规定的路径下。
例如,这个例子在我填写的路径下就有了这些东西:
弄清楚这些再往下看,可能你会安心一些,
接下来就是使用hibernatetool在数据库中创建我们的Track表了。
首先你需要写一下hibernate.properties告诉hibernate怎么去链接数据库,什么数据库。
- 利用ant和maven-ant-tasks使用Hibernate链接数据库
- ant中使用maven-ant-tasks下载maven的依赖
- Ant: 使用外部的tasks
- Ant,Maven,Gradle使用。
- maven 和 ant 比较
- maven 和 ant 学习
- 比较Maven和Ant
- 比较Maven和Ant
- maven和ant 比较
- 比较Maven和Ant
- Maven和Ant
- maven和ant区别
- Ant和Maven特点
- Maven和Ant对比
- maven 和 ant 比较
- Maven和Ant
- 利用ant,hibernate的schemaexport自动生成数据库
- Tomcat Ant Tasks
- Bzoj2631 Tree
- php判断检测一个数组里有没有重复的值
- 浅谈 Linux 内核开发之网络设备驱动
- HttpClient网络请求数据
- HTTP请求方法对照表
- 利用ant和maven-ant-tasks使用Hibernate链接数据库
- 设置页面透明
- iOS-私有API与runtime
- 使用BCrypt算法加密存储登录密码用法及好处
- 剑指offer-丑数
- 栈与队列-顺序栈与链栈类模板的实现(数据结构基础 第3周)
- Ubuntu 12.04忘记登录密码及修改密码
- wifi定位
- iOS错误解决