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 ^