Mybatis (vi) Spring integration Mybatis

Source: Internet
Author: User

Heart Mo Impetuous ~ Walk, step by step, even if do not study, play, can do? Life is like that, to find something interesting, to pass the time of things to do, and study technology, brain-to-hand process, or more interesting than other work ~ So, hard what is to force themselves to do what they think the right thing, want to do what to do!

--wh

First, spring integration MyBatis ideas

Very simple, here First review the foundation of MyBatis,

MyBatis, there are two configuration files

Global profile Sqlmapconfig.xml (Configure data sources, global variables, load map files, etc.)

Map file Xxxmapper.xml, used to configure input parameter output parameters, database statements.

Steps to use after the MyBatis is configured

1. Get the location of the sqlmapconfig.xml and load it

2. Create Sqlsessionfactory object from content in Sqlmapconfig.xml

3. Then create the Sqlsession object from the Sqlsessionfactory object

4, with the Sqlsession object can be carried out the corresponding operation.

Integration Ideas

With some of the above knowledge reviews, then there is the idea that spring will inherit the Mabatis.

1, let spring to manage the data source information, sqlmapconfig.xml do not need to load the data source. Give Spring management

2, let spring through a single case management sqlsessionfactory, only need a sqlsessionfactory help us generate sqlsession can. That is, you need sqlsession objects to let sqlsessionfactory spawn. So it's a single-case approach.

3. Let spring create the sqlsession bean. That is, creating sqlsession through Sqlsessionfactory,

4, if the use of Mapper agent development of the way, then the mapper of the persistence layer will need to be managed by spring, spring and MyBatis integration to generate Mapper proxy objects.

Second, the project construction

2.1. Create a Java project

                  

2.2. Add Jar Package

Core and dependency packages of MyBatis

Database driver Package

Spring's Package

JUnit Package

Spring and MyBatis Integration package

DBCP Connection Pool

Jar packages required for mybatis and spring integration

  

2.3.

2.4. Add Sqlmapconfig.xml

            

Sqlmapconfig.xml

2.5. Mapping configuration file Student.xml

              

Student.xml

2.6, integration of spring configuration file Applicationcontext.xml

            

Applicationcontext.xml

  

2.7, Db.properties

                        

Db.properties

2.8, the general view, wrote four configuration files, and a JavaBean

            

Iii. development of the original DAO

The above is a common configuration, when we use MyBatis, there are two modes, one is the original DAO way, one is to use the Mapper proxy, here is how the original DAO is integrated with spring

Studengdao: interface

            

Studentdaoimpl: Implementation Class

            

The disadvantage of using the original DAO development is that it can only be done by selectone or selectlist, rather than directly invoking the method in the mapping configuration file.

Configuring Studentdao in Spring

            

      

A lot of people here will have doubts, think in spring to configure the Studengdao this bean, but we simply do not use the Set method in the Studengdaoimpl to let it automatically inject it? The reason is to inherit the Sqlsessiondaosupport class in Studengdaoimpl, this class helps us, so we can get the object directly through This.getsqlsession ().

Test:

            

        

Iv. Development of Mapper mode

Write Mapper interface, Studentmapper.java

            

Write the mapper mapping file Studengmapper.xml, and note that two are to be put together. Names are the same

            

Generate Mapper proxy object in spring

              

View Code

Test:

            

              

V. Summary

So it's over, Spring integrates MyBatis, it's simple, and what objects are created by spring. Note the differences between the original DAO and the Mapper, which are developed in two ways. It turned out to be the same in the city and in different ways. This is explained in the first section mybatis, this is not in the statement, the next section on the reverse engineering of MyBatis

Mybatis (vi) Spring integration Mybatis

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.