Spring example Jpetstore analysis of the relationship between the layers of the---------3jpetstore

Source: Internet
Author: User
Tags abstract zip

The following is a Jpetstore account Management section, to analyze the relationship between the layers of Jpetstore:

1. Persistence Layer Analysis

1.1 The XML Innuendo section of the Ibatis for account management is as follows:

Account management involves four sheets:

Signon Store user name, password

Account, which stores the user's basic information

Profile, store user-selected language, and favorite product categories

Bannerdata not clear at the moment

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE sqlmap Public "-//ibatis.com//dtd SQL Map 2.0//en" "Http://www.ibatis.com/dtd/sql-map-2.dtd" >

<sqlmap namespace= "Account" >

"Account information result set"

<resultmap id= "Result" class= "Org.springframework.samples.jpetstore.domain.Account" >
<result property= "username" column= "userid" columnindex= "1"/>
<result property= "Email" column= "email" columnindex= "2"/>
<result property= "FirstName" column= "FirstName" columnindex= "3"/>
<result property= "LastName" column= "LastName" columnindex= "4"/>
<result property= "status" column= "status" columnindex= "5"/>
<result property= "Address1" column= "Addr1" columnindex= "6"/>
<result property= "Address2" column= "ADDR2" columnindex= "7"/>
<result property= "City" column= "City" columnindex= "8"/>
<result property= "state" column= "state" columnindex= "9"/>
<result property= "zip" column= "zip" columnindex= "ten"/>
<result property= "Country" column= "country" columnindex= "one"/>
<result property= "Phone" column= "phone" columnindex= "/>"
<result property= "LanguagePreference" column= "langpref" columnindex= "/>"
<result property= "Favouritecategoryid" column= "favcategory" columnindex= "/>"
<result property= "listoption" column= "mylistopt" columnindex= "/>"
<result property= "banneroption" column= "banneropt" columnindex= "/>"
<result property= "Bannername" column= "bannername" columnindex= "/>"
</resultMap>

"Access to user information according to user name"

<select id= "Getaccountbyusername" resultmap= "Result" >
Select
Signon.username as UserID,
Account.email,
Account.firstname,
Account.lastname,
Account.status,
ACCOUNT.ADDR1,
ACCOUNT.ADDR2,
Account.city,
Account.state,
Account.zip,
Account.country,
Account.phone,
Profile.langpref,
Profile.favcategory,
Profile.mylistopt,
Profile.banneropt,
Bannerdata.bannername
From account, profile, Signon, Bannerdata
where Account.userid = #value #
and signon.username = Account.userid
and Profile.userid = Account.userid
and profile.favcategory = Bannerdata.favcategory
</select>

"Based on account, password, get account"

<select id= "Getaccountbyusernameandpassword" resultmap= "Result" >
Select
Signon.username as UserID,
Account.email,
Account.firstname,
Account.lastname,
Account.status,
ACCOUNT.ADDR1,
ACCOUNT.ADDR2,
Account.city,
Account.state,
Account.zip,
Account.country,
Account.phone,
Profile.langpref,
Profile.favcategory,
Profile.mylistopt,
Profile.banneropt,
Bannerdata.bannername
From account, profile, Signon, Bannerdata
where Account.userid = #username #
and Signon.password = #password #
and signon.username = Account.userid
and Profile.userid = Account.userid
and profile.favcategory = Bannerdata.favcategory
</select>

"Get system User Name list"

<select id= "getusernamelist" resultclass= "java.lang.String" >
Select Username as value from Signon
</select>

"Update account Information"

<update id= "Updateaccount" >
Update account Set email = #email #, FirstName = #firstName #, LastName = #lastName #, status = #status #, ADDR1 = #address1 #, ADDR2 = #address2: varchar#, city = #city #, state = #state #, zip = #zip #, country = #country #, phone = #phone # where Useri D = #username #
</update>

"Add new Account"

<insert id= "Insertaccount" >
Insert into account (e-mail, FirstName, LastName, Status, Addr1, ADDR2, city, state, zip, country, phone, UserID) VALUES (# email#, #firstName #, #lastName #, #status #, #address1 #, #address2: varchar#, #city #, #state #, #zip #, #country #, #phone #, #u sername#)
</insert>

Update user profile table, which includes user language choices, and favorite animal categories

<update id= "Updateprofile" >
Update profile Set langpref = #languagePreference #, favcategory = #favouriteCategoryId #, mylistopt = #listOptionAsInt #, BA nneropt = #bannerOptionAsInt # where UserID = #username #
</update>

"Adding user profile Information"

<insert id= "Insertprofile" >
Insert into profile (Langpref, Favcategory, mylistopt, banneropt, UserID) VALUES (#languagePreference #, # favouritecategoryid#, #listOptionAsInt #, #bannerOptionAsInt #, #username #)
</insert>

Update user Password

<update id= "Updatesignon" >
Update signon Set password = #password # where username = #username #
</update>

Add user name, password to user login form signon〉

<insert id= "Insertsignon" >
Insert into Signon (password,username) VALUES (#password #, #username #)
</insert>

</sqlMap>
1.2 Account Management Pojo class, Accountdao for account management interface, defined a series of methods:

Public interface Accountdao {

Account Getaccount (String username) throws DataAccessException;

Account Getaccount (string Username, string password) throws DataAccessException;

void Insertaccount (Account account) throws DataAccessException;

void Updateaccount (Account account) throws DataAccessException;

List getusernamelist () throws DataAccessException;

}

1.3Account Interface Implementation:

public class Sqlmapaccountdao extends Sqlmapclientdaosupport implements Accountdao {

Public account Getaccount (String username) throws DataAccessException {
Return (account) Getsqlmapclienttemplate (). queryForObject ("Getaccountbyusername", username);
}

based on user name, password, get account number

Public account Getaccount (string Username, string password) throws DataAccessException {
Account Account = new account ();
Account.setusername (username);
Account.setpassword (password);
Return (account) Getsqlmapclienttemplate (). queryForObject ("Getaccountbyusernameandpassword", account);
}

create account, need to update account,signon,profile three sheets

public void Insertaccount (account account) throws DataAccessException {
Getsqlmapclienttemplate (). Insert ("Insertaccount", account);
Getsqlmapclienttemplate (). Insert ("Insertprofile", account);
Getsqlmapclienttemplate (). Insert ("Insertsignon", account);
}

update account, need to update account,signon,profile three sheets

public void Updateaccount (account account) throws DataAccessException {
Getsqlmapclienttemplate (). Update ("Updateaccount", account, 1);
Getsqlmapclienttemplate (). Update ("Updateprofile", account, 1);
if (Account.getpassword () = null && Account.getpassword (). Length () > 0) {
Getsqlmapclienttemplate (). Update ("Updatesignon", account, 1);
}
}

"Get System User Name list"

Public List getusernamelist () throws DataAccessException {
Return Getsqlmapclienttemplate (). queryForList ("getusernamelist", null);
}

}

Summary:

ibatis persistence layer uses Sql-map-config.xml to configure all Ibatis. xml Files

<sqlMapConfig>

<sqlmap resource= "Org/springframework/samples/jpetstore/dao/ibatis/maps/account.xml"/>
<sqlmap resource= "Org/springframework/samples/jpetstore/dao/ibatis/maps/category.xml"/>
<sqlmap resource= "Org/springframework/samples/jpetstore/dao/ibatis/maps/product.xml"/>
<sqlmap resource= "Org/springframework/samples/jpetstore/dao/ibatis/maps/item.xml"/>
<sqlmap resource= "Org/springframework/samples/jpetstore/dao/ibatis/maps/order.xml"/>
<sqlmap resource= "Org/springframework/samples/jpetstore/dao/ibatis/maps/lineitem.xml"/>
<sqlmap resource= "Org/springframework/samples/jpetstore/dao/ibatis/maps/sequence.xml"/>

</sqlMapConfig>

Configuring all DAO implementations with Dataaccesscontext-config.xml

<bean id= "sqlmapclient" class= "Org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
<property name= "configlocation" value= "Web-inf/sql-map-config.xml"/>
<property name= "DataSource" ref= "DataSource"/>
</bean>

<bean id= "Accountdao" class= "Org.springframework.samples.jpetstore.dao.ibatis.SqlMapAccountDao" >
<property name= "sqlmapclient" ref= "Sqlmapclient"/>
</bean>

2, the coupling of the persistence layer and the business logic layer, the business logic interface contains the Accountdao interface operation

Public interface Petstorefacade {

Account Getaccount (String username);

Account Getaccount (string Username, string password);

void Insertaccount (account account);

void Updateaccount (account account);

List getusernamelist ();

。。。。。。。

}

The business logic object Petstoreimpl implements the re-encapsulation of the Accountdao

public class Petstoreimpl implements Petstorefacade, OrderService {

Private Accountdao Accountdao;

Public account Getaccount (String username) {
Return This.accountDao.getAccount (username);
}

Public account Getaccount (string Username, string password) {
Return This.accountDao.getAccount (username, password);
}

public void Insertaccount (account account) {
This.accountDao.insertAccount (account);
}

public void Updateaccount (account account) {
This.accountDao.updateAccount (account);
}

Public List getusernamelist () {
return This.accountDao.getUsernameList ();
}

。。。。

}

Summarize:

The coupling of the persistence layer to the business logic layer is in APPLICATIONCONTEXT.CML:

providing transaction management for the business logic layer


<bean id= "Basetransactionproxy" class= " Org.springframework.transaction.interceptor.TransactionProxyFactoryBean "
    abstract=" true "
  <property name= "TransactionManager" ><ref bean= "TransactionManager"/></ Property>
  <property name= "transactionattributes";
   <props>
    <prop key= "insert*" >PROPAGATION_REQUIRED</PROP>
     <prop key= "update*" >PROPAGATION_REQUIRED</PROP>
    <prop key= "*" >propagation_required,readonly</prop>
   </props>
  </ Property>
 </bean>


<bean id= "Petstore" parent= "Basetransactionproxy" >
<property name= "Target" >
<bean class= "Org.springframework.samples.jpetstore.domain.logic.PetStoreImpl" >
<property name= "Accountdao" ref= "Accountdao"/>
<property name= "CategoryDao" ref= "CategoryDao"/>
<property name= "Productdao" ref= "Productdao"/>
<property name= "Itemdao" ref= "Itemdao"/>
<property name= "Orderdao" ref= "Orderdao"/>
</bean>
</property>

4 The coupling of the presentation layer and the Business Logic layer:

The coupling of the presentation layer and the business logic layer is implemented through baseaction and accesses the business logic object through the interface:

Public abstract class Baseaction extends Action {

private Petstorefacade Petstore;

public void Setservlet (Actionservlet actionservlet) {
Super.setservlet (Actionservlet);
if (Actionservlet! = null) {
ServletContext ServletContext = Actionservlet.getservletcontext ();
Webapplicationcontext WAC = Webapplicationcontextutils.getrequiredwebapplicationcontext (ServletContext);
This.petstore = (Petstorefacade) wac.getbean ("Petstore");
}
}

protected Petstorefacade Getpetstore () {
return petstore;
}

}

All the actions in the system are derived from baseaction, while the derived class of Baseaction provides the business logic object
Petstorefacade's access.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.