"Web Development Learning Note" Ibatis Learning Summary

Source: Internet
Author: User

Ibatis Study SummaryIbatis Database configuration file
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE sqlmapconfig public          "-//ibatis.apache.org//dtd SQL Map Config 2.0//en"          "Http://ibatis.apache.org/dtd /sql-map-config-2.dtd "><sqlMapConfig>    <datasource type=" simple ">      <property name=" JDBC. Driver "value=" Org.hsqldb.jdbcDriver "/>      <property name=" JDBC. Connectionurl "Value=" JDBC:HSQLDB:. " />      <property name= "JDBC. Username "value=" carp "/>      <property name=" JDBC. Password "value=" 123 "/>    </dataSource>  </transactionManager>  <sqlmap resource=" com/ Mydomain/data/account.xml "/></sqlmapconfig>
database and model mapping configuration file
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE sqlmap Public "-//ibatis.apache.org//dtd SQL Map 2.0//en" "http://ibatis.apache.org/dtd/sql-map- 2.dtd "><sqlmap namespace=" account "> <resultmap id=" accountresult "class=" account "> <result property= "id" column= "acc_id"/> <result property= "firstName" column= "Acc_first_name"/> <result property= "LastName" column= "Acc_last_name"/> <result property= "EmailAddress" column= "Acc_email"/> </resultMap > <!--Select with no parameters using the result map to account class.  --<select id= "selectallaccounts" resultmap= "Accountresult" > select * from Account </select> <!--  A Simpler Select example without the result map. Note the aliases to match the properties of the target result class.      --<select id= "Selectaccountbyid" parameterclass= "int" resultclass= "account" > select acc_id as ID, Acc_first_name as FirstnaMe, acc_last_name as LastName, Acc_email as EmailAddress from account where acc_id = #id # </select> <!--Insert example, using the account parameter class--and <insert id= "Insertaccount" parameterclass= "Accou NT "> INSERT into account (acc_id, Acc_first_name, Acc_last_name, acc_email values (#id #, #firstName #, #lastName #, #emailAddress #) </insert> <!--Update example, using the account parameter Clas S-to <update id= "Updateaccount" parameterclass= "account" > Update account Set acc_first_name = #firstNam  e#, acc_last_name = #lastName #, acc_email = #emailAddress # where acc_id = #id # </update> <!-- Delete example, using an integer as the parameter class--and <delete id= "Deleteaccountbyid" parameterclass= "int"    ; Delete from account where acc_id = #id # </delete></sqlMap>
Model Object
Package Com.mydomain.domain;public class Account {  private int id;  Private String firstName;  Private String lastName;  Private String EmailAddress; /*model layer, the main data access part *  /public int getId () {    return ID;  }  public void setId (int id) {    this.id = ID;  }  Public String Getfirstname () {    return firstName;  }  public void Setfirstname (String firstName) {    this.firstname = firstName;  }  Public String Getlastname () {    return lastName;  }  public void Setlastname (String lastName) {    this.lastname = lastName;  }  Public String getemailaddress () {    return emailaddress;  }  public void setemailaddress (String emailaddress) {    this.emailaddress = EmailAddress;  }}
DAO data Access Object
Package Com.mydomain.data;import Com.ibatis.sqlmap.client.sqlmapclient;import Com.ibatis.sqlmap.client.sqlmapclientbuilder;import Com.ibatis.common.resources.resources;import Com.mydomain.domain.account;import Java.io.reader;import Java.io.ioexception;import Java.util.List;import Java.sql.sqlexception;public class Simpleexample {/** * sqlmapclient instances is thread safe, so your only need one.i    n this case, we'll use a static singleton.  */private static sqlmapclient sqlmapper; /** * It's not a good idea to put code that can fail in a class initializer, * and for sake of the argument, here's how Yo   U Configure an SQL Map.      */Static {try {reader reader = Resources.getresourceasreader ("Com/mydomain/data/sqlmapconfig.xml");      Sqlmapper = sqlmapclientbuilder.buildsqlmapclient (reader);     Reader.close ();      } catch (IOException e) {//Fail fast.    throw new RuntimeException ("Something bad happened while building the Sqlmapclient instance." + E, E); } } public static List selectallaccounts () throws SQLException {return sqlmapper.queryforlist ("selectallaccounts"); The public static account Selectaccountbyid (int id) is throws SQLException {return (account) Sqlmapper.queryforobject ("s  Electaccountbyid ", id);   } public static void Insertaccount (account account) throws SQLException {Sqlmapper.insert ("Insertaccount", account); } public static void Updateaccount (account account) throws SQLException {sqlmapper.update (' Updateaccount ', account  );  public static void DeleteAccount (int id) throws SQLException {sqlmapper.delete ("DeleteAccount", id); }}
Conclusion:
1, through the database and model object mapping configuration file, the database table and Java objects are associated,2, through the database configuration file, which contains the database and model object mapping configuration file, to ensure the database connection;   3, through the DAO for the database to delete and change the operation;




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.