Learn Mybatis step by step-implement CRUD operations for a single table (3)

Source: Internet
Author: User
Tags couchdb

This chapter is next to the content in the previous section. This chapter creates CRUD and GetList operations based on single table operations. In this example, the Visitor table is used as an example, to create some test data, let's get an Add method first.

Add the add, delete, update, query, and getList interfaces in the last IVisitorOperation interface class as follows:

         Visitor basicQuery(           delete(          Visitor query(     List<Visitor> getList();

For the corresponding CRUD operation, under the <mapper> node of VisitorMapper. xml respectively corresponding to insert, update, delete, select node, specific configuration details parameter description course reference official website http://mybatis.github.io/mybatis-3/sqlmap-xml.html
The configuration in this example is as follows. The passed parameter # {parameter_name} can also be used directly $ {parameter_name },

In the former way, Mybatis converts it into a parameterized form, such as insert into table (name) values (# {name}) => insert into table (name) values (? ) (In the case of Mysql)

In the latter way, Mybatis will pass the parameters intact without any operation, for example, insert into table (name) values ($ {name}) => insert into table (name) values ([the value you passed]), pass aa, here is aa, and pass 'A' here is 'A '.

VisitorMapper

Note that:For example, if the interface is add (Visitor visitor), id = "add", parameterType = "Visitor" When configuring the insert Node"

Otherwise, an exception is reported. For example, if the id node does not correspond to the interface name, the following exception occurs:

 

You can notice in VisitorMapper. in the xml configuration file, when listing is obtained, the resultMap is used. When using resultMap, you can specify which fields your SQL statement maps, because sometimes you do not need to configure so many ing columns, or your column has an alias, therefore, you cannot directly use resultType = "Visitor" for ing, because the default Mybatis ing method is to match the attribute names of the Javabean with the table fields, you can also configure the <settings> attribute value under the <configuration> node to control whether the ing is performed or not.

     

Additional modifications to other Mapper configurations can be viewed in the http://mybatis.github.io/mybatis-3/configuration.html#settings.
The VISER class of Visitor is as follows:

                                                                              

You will also notice that there is a <SQL> node, which is used to extract common SQL statements or fields for reuse elsewhere. For more information, see http://mybatis.github.io/mybatis-3/sqlmap-xml.html.
The rest is just the same operation. You can create a DemoRun class in the demo program to store various test methods, as shown below:

TestBasicQuery (= (Visitor) session. selectOne ("david. mybatis. demo. IVisitorOperation. basicQuery "testBasicQueryByInterfaceWay (= session. getMapper (IVisitorOperation. ==< Visitor> visitors = Arrays. asList (Visitor [] {Visitor ("mongodb", "mongodb@gmail.com" Visitor ("redis", "redis@gmail.com"), Visitor ("memcached ", "memcached@gmail.com" Visitor ("CouchDB", "CouchDB@gmail.com"), Visitor ("HBase", "HBase@gmail.com" Visitor ("Bigtable", "Bigtable@gmail.com"), Visitor ("Hive ", "Hive@gmail.com" Visitor ("MapReduce", "MapReduce@gmail.com" "unused" (session === session. getMapper (IVisitorOperation. recordCount = (session = deleteVisitor (= session. getMapper (IVisitorOperation. recordCount = updateVisitor (= session. getMapper (IVisitorOperation. = "original object:" + = (name. contains ("updated" 0, name. indexOf ("updated" + "updated" recordCount = "updated object:" + queryVisitor (= session. getMapper (IVisitorOperation. = 1 = session. getMapper (IVisitorOperation. <Visitor> visitors =DemoRun class

Run the next simple single-table-based CRUD, And the DEMO is complete.

The next chapter describes how to use Mybatis for multi-table join queries ~ ^ 0 ^

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.