IBatis Code Auto-generation tool Ibator and Example use

Source: Internet
Author: User

ibator Download and install official: http://people.apache.org/builds/ibatis/ibator/installation: See "File" after installation of the Eclipse plug-in installation is complete> "New" and "Other ..."iBatis Code Auto-generation tool Ibator-Understated Splendour-Glow Color Space Select project Name-> "Next", "New" and "Other ..."iBatis Code Auto-generation tool Ibator-Understated Splendour-Click "Finish" on the glow-color space. It's going to be ibatortest ./ibatorconfig/The Ibatorconfig.xml file is generated in the directory. IBatis Code Auto-generation tool Ibator-Understated Splendour-color space and then modify the Ibatorconfig.xml file, the modified file is as follows<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE ibatorconfiguration Public "-//apache software foundation//dtd Apache iBATIS ibator Configuration 1.0//en" "http: Ibatis.apache.org/dtd/ibator-config_1_0.dtd "><ibatorconfiguration > <classpathentry location=" F:\ Javaee\ibatortest\lib\sqlserver.jar "/>/*SQL Server Database drive path*/<ibatorcontext id= "Context1" > <jdbcconnection driverclass= "Com.microsoft.jdbc.sqlserver.SQLServerD RiverConnectionurl= "Jdbc:microsoft:sqlserver://localhost:1433;databasename=demo" userid= "sa" password= "Joe"/> <javaMode Lgenerator targetpackage= "Com.demo.ibatis.beans" targetproject= "ibatortest"/> <sqlmapgenerator target Package= "Com.demo.ibatis.beans.mapFiles" targetproject= "Ibatortest"/> <daogenerator targetpackage= "Co M.demo.ibatis.dao "targetproject=" Ibatortest "type=" Generic-ci "/> <table schema=" dbo "tableName=" user  "Catalog=" Demo "domainobjectname=" User "> <generatedkey column=" ID "sqlstatement=" SQL Server " Identity= "true" type= "post"/> </table> </ibatorContext></ibatorConfiguration>right mouse button Ibatorconfig.xml,ibatis code auto-generation tool Ibator-Understated Splendour-The glow-color space will automatically generate the mapping file and the domain layer, as well as the DAO layer, where the user can simply write the service layer. Ibator database operations The files exported through Ibator include mapping files, domain classes, DAO classes. The domain class and DAO classes that it exports are generated from a framework designed by Ibator, which includes various functions. We are going to develop service layer code based on these classes. The interface of the newly generated DAO layer provides the following operational functions:intCountbyexample (userexample example) Thorws SQLException: Count by condition. intDeletebyprimarykey (Integer ID) thorws SQLException: Delete by primary key. intDeletebyexample (userexample example) Thorws SQLException: Delete by condition. String/integer insert (user record) Thorws SQLException: Insert (return value is ID value) User selectbyprimarykey (integer ID) thorws SQLException: Press primary key to query. List<?>Selectbyexample (userexample example) THORWS SQLException: Query list by criteria<?>Selectbyexamplewithblogs (userexample example) THORWS SQLException: Query by criteria (including BLOB fields). Only if the field type in the data table is binary is it generated. intUpdatebyprimarykey (User record) Thorws SQLException: Update by primary keyintupdatebyprimarykeyselective (User record) Thorws SQLException: Update A field with a value that is not NULL by the primary keyintupdatebyexample (User record, userexample example) THORWS SQLException: Update by conditionintupdatebyexampleselective (User record, userexample example) THORWS SQLException: Updating a field with a value that is not null by condition in detail: Userdaoimpl Userdao=NewUserdaoimpl (Sqlmapclientfactory.getsqlmapclient ()); Note: Sqlmapclientfactory.getsqlmapclient (): is a custom class and method, The goal is to get Sqlmapclient.①selectbyprimarykey () User User= Userdao.selectbyprimarykey (100); Equivalent to select * from user where id = 100②selectbyexample () and Selectbyexamplewithblogs () userexample Example=Newuserexample (); Criteria Criteria=Example.createcriteria (); Criteria.andusernameequalto ("Joe"); Criteria.andusernameisnull (); Example.setorderbyclause ("Username Asc,email desc"); List<?>list =Userdao.selectbyexample (example); equivalent: Select* FROM user where username = ' Joe ' and username areNULLORDER BY username Asc,email desc Note: In the ibator generated file Userexample.java contains a static internal class criteria, there are many ways in the criteria, mainly defining SQL The query condition after the statement where. ③insert () User User=NewUser (); User.setid (101); User.setusername ("Test"); User.setpassword ("123") User.setemail ("[Email protected]"); Userdao.insert (user); equivalent: INSERT INTO user (Id,username,password,email) VALUES (101, ' Test ', ' 123 ', ' [email protected] '); ④updatebyprimarykey () and updatebyprimarykeyselective () user User=NewUser (); User.setid (101); User.setusername ("Joe"); User.setpassword ("Joe"); User.setemail ("[Email protected]"); Userdao.updatebyprimarykey (user); equivalent: Update user set username= ' Joe ', password= ' Joe ', email= ' [email protected] ' where id=101User User=NewUser (); User.setid (101); User.setpassword ("Joe"); userdao.updatebyprimarykeyselective (user); equivalent: Update user set Password= ' Joe ' where id=101⑤updatebyexample () and updatebyexampleselective () userexample Example=Newuserexample (); Criteria Criteria=Example.createcriteria (); Criteria.andusernameequalto ("Joe"); User User=NewUser (); User.setpassword ("123"); userdao.updatebyprimarykeyselective (user,example); equivalent: Update user set Password= ' 123 ' where username= ' Joe '⑥deletebyprimarykey () Userdao.deletebyprimarykey (101); Equivalent to: Delete from user where id=101⑦deletebyexample () userexample Example=Newuserexample (); Criteria Criteria=Example.createcriteria (); Criteria.andusernameequalto ("Joe"); Userdao.deletebyexample (example); equivalent: Delete from user where username= ' Joe '⑧countbyexample () userexample Example=Newuserexample (); Criteria Criteria=Example.createcriteria (); Criteria.andusernameequalto ("Joe");intCount =Userdao.countbyexample (example); equivalent to: SELECT COUNT (*) from user where username= ' Joe 'extending the DAO class to implement a more complex sqlibator plug-in simply gives us a code framework that satisfies the basic functionality, such as: Add, delete, change, check, condition, and sort, all of which are implemented by the functions exported by the Ibator tool. But Ibator cannot provide us with all the functions, but since the Ibatis framework is based on native SQL. Therefore, we only need to extend it based on the code generated by the Ibator code plugin. The method of expansion is, of course, based on the Ibatis mapping file, only need to add more<statement>, <select>such as SQL declaration elements. Example:<select id= "Getmaxuserid" resultclass= "Java.lang.Integer" > <! [Cdata[select Max (ID) from User]]></select><select id= "getusernamelist" resultclass= "java.lang.String" > <! [Cdata[select distinct username from user]]></select>The following two functions are then implemented in Userdaoimpl.java: PublicInteger Getmaxuserid ()throwssqlexception{return(Integer) Sqlmapclient.queryforobject ("Users.getmaxuserid");} PublicList<?>getusernamelist ()throwssqlexception{List<?>list =sqlmapclient.queryforlist ("users.getusernamelist"); returnlist;}

IBatis Code Auto-generation tool Ibator and Example use

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.