"MDD"
Model Driven Development model drive development. Take a funny title, in fact, I have not studied how MDD is, but the following process I feel that there is such a little such meaning, write it out. It used to develop database applications, inseparable from the database table and its relationship, which in the case of complex applications, I think it is still necessary to exactly the role of optimization. But for some simple applications, I really don't want to deal with the database table. Fortunately, Hibernate provides a good method and tool to simplify this process. As long as you pay attention to our model and model, you can easily generate database tables and necessary Code, Yeah, very beautiful. OK, Let's Diy Our OWN "MDD", Just For Fun! I want to write an online football game, the following simple model is necessary: Player: such as Zidane, Well, I am in the intention. Team (Team): Match team events (Match): a set of events, such as Premier League 04/05 USE (User): a person playing this game (Squad): A user settings for a certain event setting One lineup will be first. There is a relatively simple relationship between them: Player N: 1 Team Team N: n Match User 1: N Squad Squad N: N Player
These are the relationships between the models and models I want, I want to get the POJO class and the DDL file corresponding to the database, so I only need to write a Hibernate mapping file to describe them.
Models.hbm.xml
XML Version = "1.0" encoding = "UTF-8"?>
It may be a little bit a little longer, but it is actually similar, and it can be seen for Hibernate. The yellow part is to expressed relationship, if you want the relationship to be two-way, you must describe them in both classes. Now there is a file that describes our model, holding this file, add the hibernate tool, I have something I want. These require two tools, hbm2java.bat comes from the Hibernate-Extensions toolkit, schemaexport.bat from the bin directory in the Hibernate package. (1) hbm2java.bat generates the Pojo class through the mapping file. Java file I am more lazy, copy model.hbm.xml to the hibernate-extensions-2.0.2 / tools / bin directory I use, switch to CMD, carried out
> hbm2java.bat model.hbm.xml
The current directory will generate a generated directory, the generated class .java file is inside.
(2) The following needs to be compiled, no Ant, write a batch processing, below is mine, it is easy to change to your: compile.bat
@echo off rem ---------------------------------------------------------------------------------------------------------------------- -------------------- Rem Compile Java Sourse Rem ------------------------ -------------------------------------------
SET JDBC_DRIVER = D: /J2SDK1.4.2_01/lib/mysql-connector-java-3.0.8-STable-bin.jar set hibernate_Home = E: /ftp/pub/softWares/Hibernate/Hibernate-2.1
SET CORELIB =% hibernate_home% / lib set lib = .. / lib set props =% hibernate_Home% / src set cp =% jdbc_driver%;% prOPs%;% hibernate_home% / hibernate2.jar;% CoreLib% / commons-logging- 1.0.3.jar;% corelib% / commons-collections-2.1.jar;% Corelib% / commons-lang-1.0.1.jar;% CORELIB% / CGLIB-2.0-rc2.jar;% Corelib% / DOM4J- 1.4.jar;% corelib% / odmg-3.0.jar;% corelib% / xml-apis.jar;% corelib% / xerces-2.4.0.jar;% Corelib% / xalan-2.4.0.jar;% lib % / jdom.jar;% lib% / .. / hibernate-tools.jar javac -classpath% CP% org / smth / j2ee / WebSoccer / domain / *. Java (3) SchemaExport.bat tool generates DDL files for databases Lazy, copy SchemaExport.bat to the generated directory, and match a hibernate.properties file. SchemaExport.bat is generally changed, set ClassPath, very simple, open and see if you want to change. My hibernate.properties
Hibernate.Connection.driver_class = org.gjt.mm.mysql.driver
Hibernate.Connection.url = jdbc: mysql: // localhost: 3306 / WebSoccerHibernate.Connection.userName = root
Hibernate.connection.password = ********
Hibernate.diaforct = net.sf.hibernate.dialect.mysqldiaLect
Then execute in the generated directory:> schemaexport.bat --text --output = models.ddl models.hbm.xml
In the current directory, there is a models.ddl I want. What database is required, set the corresponding hibernate.properties file.
It feels a little MDD. Well, just these, there are still many things to do ...
Author: oosky @ smth email: wzt@mails.tsinghua.edu.cn