002 medical project-Layer 3 construction of the main project module yycgproject (integration of the three frameworks)

Source: Internet
Author: User

 

First, provide the project structure:

Log4j. properties-log configuration file

DB. properties ---- connect to the database

Configure database connection Parameters

Mybatis/sqlmapconfig. xml --- mybatis core configuration file

Mybatis configuration item

Core configuration file of spring/applicationcontext. XML-à spring

Configure public content: data source and transaction management

Configure Dao in spring/applicationcontext-base-dao.xml à

Configure sqlsessionfactory and Dao (Mapper)

 

Spring/applicationContext-base-service.xml-à configure Service

Configure business Interfaces

Spring/springmvc. xml --- configure springmvc

Processor er

Processor Adapter

View parser

Interceptor

 

 

I. Integrate Dao and spring frameworks first:

Achieve the goal: integrate Spring and mybatis to manage the DaO interface of spring.

Let spring manage sqlsessionfactory, and then sqlsession is automatically generated by sqlsessionfactory. sqlsession is used in Dao interface.

 

 

 

Integration Method 1 (traditional Dao)

Programmers create Dao interface and Dao interface implementation classes,

 

The Dao interface implementation class inherits sqlsessiondaosupport

Configure the DaO interface in the spring container and inject sqlsessionfactory into the DaO interface implementation,

Call this. getsqlsession () in the DaO interface method to obtain the sqlsession.

Example:

The interface implementation class inherits sqlsessiondaosupport

To use this method, you need to write the ER interface, the Mapper interface implementation class, And the Mapper. xml file.

1. Configure the Mapper. xml location in sqlmapconfig. xml

<Mappers>

<Mapper resource ="Mapper. xmlFile address"/>

<Mapper resource ="Mapper. xmlFile address"/>

</Mappers>

 

2. Define the Mapper Interface

 

3. Implement class Integration sqlsessiondaosupport

 

In the Mapper method, you can use this. getsqlsession () to add, delete, modify, and query data.

4. Spring Configuration

<! -- Sessionfactory -->
<Bean id = "sqlsessionfactory" class = "org. mybatis. Spring. sqlsessionfactorybean">
<Property name = "datasource" ref = "datasource"/>
<! -- Integration of mybatis and package scanning of mapper files -->
<Property name = "configlocation" value = "classpath:/mybatis/sqlmapconfig. xml"/>

</Bean>

<Bean id =""Class ="MapperInterface implementation">

<Property name ="Sqlsessionfactory"Ref ="Sqlsessionfactory"> </Property>

</Bean>

 

 

 

 

Integration Method 2 using mapper dynamic Proxy:

 

Programmers only need to write mapper interfaces (equivalent to Dao interfaces), and do not need to write mapper interface implementation classes. mybatis provides er interfaces and mapper interfaces. XML (ing file) generates the Mapper interface dynamic proxy object (mapper interface implementation ).

 

What rules can be used to generate mapper?Proxy object:

The namespace in Mapper. XML is equal to the address of the Mapper interface.

The ID (ID of mapped statement) of the SQL defined in Mapper. XML is equal to the Chinese name of Mapper. java.

The parametertype of the Statement defined in Mapper. XML is equal to the parameter type of the method in Mapper. java.

The resulttype of the Statement defined in Mapper. XML is equal to the return value type of the method in Mapper. java.

 

Develop two files: Mapper. Java and Mapper. xml.

 

LMethod 1: Use mapperfactorybean

Step 1:

Write Mapper. xml:

<! -- Sysusermappercustom is the Mapper interface -->

<Mapper namespace ="Yycg. Base. Dao. Mapper. sysusermappercustom">

<! -- Query user information by id -->

<Select id ="Findsysuserbyid"Parametertype ="String"

Resulttype ="Yycg. Base. pojo. Po. sysuser">

Select * From sysuser where ID =#{ ID}

</SELECT>

</Mapper>

 

 

Step 2:

Write Mapper. Java,

 

Then configure mapper in the spring container:

 

UseMapperfactorybean, According to mapperInterface to generate a proxy object.

 

 

If Mapper. xml and Mapper. Java have different names or are not in the same directory, We need to configure the Mapper. xml address in sqlmapconfig. xml.

Note: Mapper. xml and Mapper. Java have the same name and are in the same directory. Therefore, you do not need to load the Mapper file in sqlmapconfig. xml. This feature is based onMapperfactorybeanProvided.

 

 

 

UseMapperfactorybeanIn springContainer for each mapperConfiguration is troublesome. Therefore, the second method of mapper dynamic proxy is generated.

 

L Method 2: Use mapper Automatic Scanner

 

Use the ER er scanner provided in the mybaits and spring integration packages to automatically scan Mapper, generate dynamic proxy objects, and register them in the spring container.

 

 

 

Configure mapper Automatic Scanner

 

 

 

Develop two files: Mapper. JavaAnd Mapper. xmlNote: Mapper. xmlAnd Mapper. JavaIn a directory with the same name

 

Test:

Obtain the spring container and obtain sysusercustommapper from the container.

 

The system uses the scanner method to create a Mapper object.

 

002 medical project-Layer 3 construction of the main project module yycgproject (integration of the three frameworks)

Related Article

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.