"MDD" - model driver development

xiaoxiao2021-03-06  45

"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 simple model below is necessary:

Player: such as Zida, Well, I am in the meaning of him.

Team (Team): such as Manchester City

Event (Match): a set of events, such as Premier League 04/05 season

User (User): a person who played this game

Layup: A lineup of a user settings for a certain event

That's one.

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

< Property name = "username" type = "string" column = "username" />

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

转载请注明原文地址:https://www.9cbs.com/read-118502.html

New Post(0)