Multi-layer structure BLOG platform based on J2EE system

xiaoxiao2021-03-06  46

BLOG (WebLog) is increasingly popular on the Internet. Many netizens have their own blog, show yourself through Blog, and meet more netizens. The more famous Blog platform is based on ASP.NET. TEXT. However, its logic is all in the form of a stored procedure in the database. Although the stored procedure can greatly improve the efficiency of data operations, the stored procedure itself is a structured program, and it is not possible to play object-oriented power or not to implement code multiplexing.

Therefore, I decided to achieve a BLOG platform, function and interface, and .text, based on multi-layer structure based on J2EE systems, and temporarily named Crystal Blog. The implementation features are: publishing and editing articles; multi-user support; full-text search; RSS support; picture management; SMTP mail sending and other common functions. The interface is as follows:

First, choose platform and framework

Due to the J2EE platform, we are ready to use WebLogic Server 8.1 as a run platform, using WebLogic Workshop8.1 as a development tool. The database selects MS SQL Server 2000 SP3 to create a data repository stored in BLOG store all user data.

Since we don't have a specific database encoding, we will use other database tests later. Before system design, choose an excellent framework to greatly improve development efficiency. Spring is a lightweight J2EE framework. It covers almost all aspects of the JDBC from the background database to the front desk web framework. Also, the various modules of Spring are very loose, and we can use it as a framework of the entire application, or only one of its modules. In addition, Spring is very powerful to make us easily integrate the web end written by Struts, or use Hibernate as the backend O / R mapping solution.

Spring's core thinking is IOC and AOP. Spring itself is a lightweight container, and the EJB container is different. Spring's components are ordinary Java Beans, which makes unit testing can no longer rely on the container, more easily. Spring is responsible for managing all Java Beans components that also support declarative transaction management. We only need to write a Java Beans component, then "assemble" it can be, and the initialization and management of components are completed by Spring, just declaration in the configuration file. The biggest advantage of this approach is that the coupling of each component is extremely loose, and there is no need to implement the Singleton mode itself.

Since the background is used to use the relational data storage data, it is essential to use O / R mapping. Ibatis is another O / R mapping program similar to Hibernate, which is characterized by small, simple configuration, flexible query, and fully complies with our requirements.

In addition to Spring and Ibatis, the third-party components used in Spring and Ibatis include: the Lucene engine for full-text search, used for CommON-file-upload1.0 uploaded for files, used to output RSSLIBJ1.0 RC2 for RSS.

Due to the use of Spring this lightweight frame, there is no need for an EJB server, just the web server is required. Therefore, the system can run on a WebLogic Server, Tomcat, and RESIN, etc. On a web server that supports Servlet and JSP.

Second, the system design

Obviously, the J2EE architecture of the multilayer structure ensures the flexibility and scalability of the system. We still use representation of layer / logic layer / persistent layer three-layer design.

The entire system is based on spring, and the persistence layer adopts DAO mode and IBATIS O / R mapping, encapsulates all database operations; the intermediate layer is a normal JavaBean managed by Spring, using FA? ADE mode; represents the MVC framework provided by Spring. Since Spring is integrated with other frameworks, we use Velocity as View. Since Velocity cannot call Java code, forcibly use the MVC mode instead of embed logic code in View. Third, configure the server

Built a configuration in WebLogic, name Blog, add a data source, named jdbc / blog:

The directory structure of the entire application is as follows:

Crystalblog /

DOC / (store API document)

Report / (store JUnit test results)

SRC / (store Java source program)

Web / (web directory)

| Manage / (Store BLOG Management Page)

| SKIN / (store BLOG interface page)

| UPLOAD / (Picture to store users upload)

| Web-INF /

| Classes / (Store compiled class file)

| LIB / (all JAR files used)

| Search / (Store Lucene's INDEX)

| C.TLD (files required to use JSTL)

| Dispatcher-servlet.xml (Spring Profile)

| Web.xml (standard web configuration file)

blog.war (packaged deployment)

Build.xml (Ant script)

Fourth, write ANT scripts

Ant is a very great tool for executing batch tasks. Use Ant to fully automate compilation, test, packaging, deployment, and generating documents, etc., thereby greatly saving development time. First we put all the .jar files used in the / web / web-inf / lib, then write the Compile task, and the generated class file is placed directly into the web / web-inf / class directory. If the compilation is successful, the unit test is performed, and the result of the unit test is stored in the Report directory in text files. If the test passes, the next step is to pack BLOG.WAR files. Then use the application to the server, copy the contents of the web directory to the% bea_home / user_projects / domains / blogdomain / application / blog / directory. If you want to deploy on Tomcat, copy the entire web directory directly to% Tomcat% / WebApps / Blog /. Finally, if needed, you can generate an API document with Javadoc.

V. System design

Crystal Blog is divided into three-layer structure: the background data persistence layer, using DAO mode; the intermediate logic layer, adopts the FACADE mode; the front-end Web layer, the MVC structure, using JSP as a view.

Design Domain object

The Domain layer is an abstract entity. According to the features we want to achieve, the following entities are designed, they are all ordinary Java Beans: Account: packaged a user, including user ID, user name, password, user settings, etc. Category: Encapsulates a classification, a total of 3 category, which are used to manage Article, Image, and Link, an Account corresponding to multiple category. Article: Package an article, including title, summary, content, etc., a category corresponds to multiple Article.

Feedback: Package a reply, including title, username, url, and content, an Article corresponds to multiple Feedback.

Image: Encapsulate a picture, Image only contains image information (ImageID, Type), and the specific picture is stored in the form of a file uploaded to the server. A category corresponds to multiple images.

LINK: Package a link, and Category is a multi-to-one relationship. There are attributes such as Title, URL, RSS.

Message: Encapsulate a message so that other users can send mail to a user through the system without knowing the Email address.

Finally, in order to uniquely identify each database record, we need a primary key. Automatically incremented primary key generation methods can be used in MS SQL Server and Oracle. However, many databases do not support automatically incremented primary keys, take into account portability, we define a sequence table yourself to generate an incremented primary key. SEQUENCE is available and only 7 records, respectively record the current maximum main key value of Account to the Message object, respectively. When the system is started, SqlConfig is responsible for initializing the Sequence table. Sequencedao is responsible for providing the next primary key, in order to improve efficiency, cache 10 primary keys.

Sixth, configure ibatis

Next, use iBATI to implement O / R mapping. First from

Http://www.ibatis.com Download iBatis 2.0, copy the required JAR file to the web / web-inf / lib / directory. iBATIS uses XML configuration database tables to the mapping of the Java object, first write a SQL-MAP-Config.xml:

PUBLIC "- // ibatis.com//dtd SQL Map Config 2.0 // En"

"

http://www.ibatis.com/dtd/sql-map-config-2.dtd ">>

LazyLoadInabled = "true" maxrequests = "32"

Maxsessions = "10" maxTransactions = "5"

USSTATEMENTNAMESPACES = "false"

/>

Put SQL-MAP-Config.xml in a web / web-inf / class / directory, iBATIS can search this configuration file and write a initialization class:

Public class sqlconfig {

Private sqlconfig () {}

Private static final sqlmapclient SQLMAP;

STATIC {

Try {

Java.io.Reader Reader = Resources.getResourceceasReader ("SQL-MAP-Config.xml);

SQLMAP = SQLMAPCLIENTBUILDER.BUILDSQLMAPCLIENT (Reader);

} catch (exception e) {

E.PrintStackTrace ();

Throw new runtimeException ("Error Initializing Sqlconfig. Cause:" E);

}

}

Public static sqlmapclient getsqlmaPinstance () {

Return SQLMAP;

}

}

SQLMapClient encapsulates most of the operations of accessing the database, you can use SQLConfig.getsqlmapInstance () to get this unique instance.

Seven, use DAO model 1

To separate logic layers and database persistence layers, define a series of DAO interfaces: Accountdao, Categorydao, ArticleDao ... Its acting class corresponds to SQLMapAccountDao, SqlmapcategoryDao, SQLMapArticleDao ... This makes the logical layer completely detached the database access code. If you need to use other O / R mapping solutions in the future, you can directly implement the new DAO interface to replace existing SQLMapxxxxdao. Take SQLMapAccountDao as an example to achieve a login () method is very simple:

Public int login (string username, string password) throws authorizationException {

Try {

Map map = new hashmap ();

Map.put ("UserName", UserName;

Map.Put ("Password", Password);

Integer i = (Integer) SQLMap.QueryForObject ("Login", Map);

IF (i == null)

Throw New RuntimeException ("Failed: Invalid UserName or Password.");

Return I.intValue ();

}

Catch (SQLException SQLE) {

Throw new runtimeException ("SQL Exception:" SQLE);

}

Define login queries in an Account.xml configuration file:

CopyRight © 2020 All Rights Reserved
Processed: 0.037, SQL: 9