Ibatis Introductory example, using Oracle and Java test ____java

Source: Internet
Author: User
Tags deprecated

A simple Ibatis introductory example with Oracle and Java tests

Directory structure:

1. Import Ibatis and Oracle drivers.

2. Create Class Person.java

Package com.ibeats;
Import Java.util.Date;

public class Person {
private int id;
Private String FirstName;
Private String LastName;
Private double weightinkilograms;
Private double heightinmeters;
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 double Getweightinkilograms () {
return weightinkilograms;
}
public void Setweightinkilograms (double weightinkilograms) {
This.weightinkilograms = Weightinkilograms;
}
Public double getheightinmeters () {
return heightinmeters;
}
public void Setheightinmeters (double heightinmeters) {
This.heightinmeters = heightinmeters;
}
}

3. configuration file Sqlmapconfig.xml

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Sqlmapconfig
Public "-//ibatis.com//dtd SQL Map Config 2.0//en"
"Http://www.ibatis.com/dtd/sql-map-config-2.dtd" >

<sqlMapConfig>
<properties resource= "Com/ibeats/db.properties"/>
<settings
Cachemodelsenabled= "true"
Enhancementenabled= "true"
Lazyloadingenabled= "true"
Maxrequests= "32"
Maxsessions= "10"
Maxtransactions= "5"
Usestatementnamespaces= "false"
/>

<transactionmanager type= "JDBC" >
<datasource type= "Simple" >
<property name= "JDBC. Driver "value=" ${driver} "/>
<property name= "JDBC. Connectionurl "value=" ${url} "/>
<property name= "JDBC. Username "value=" ${user} "/>
<property name= "JDBC. Password "value=" ${password} "/>
</dataSource>
</transactionManager>

<sqlmap resource= "Com/ibeats/person.xml"/>
</sqlMapConfig>

(file Element Description:
Resource: Property list configuration file for database connection parameter settings.
Settings
Cachemodelsenabled: Data caching, improved program performance, and the use of LRU (the most recent unused) method to save used data in memory. Default True
Enhancementenabled: Specifies whether to use the optimized classes in Cglib to improve the performance of deferred loading. Default Ture
Lazyloadingenabled: Deferred loading, unless absolutely necessary, delay the loading of the technology. Default True
Maxrequests (deprecated): Maximum number of requests at a time, defaults to 512
MaxSessions (deprecated): Session only allowed at any time, default 128
Maxtransactions (deprecated): Maximum number of transactions, default 32
Usestatementnamespaces: If you need to use a qualified name when referencing a mapped statement. Default False
TransactionManager: Handles all database transactions.
DATASOUTCE: Data Source Factory
Property: Configuration Item
Sqlmap: Configuring Sqlmap Files
Typealias: Defining aliases)

4. configuration file Person.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Sqlmap
Public "-//ibatis.com//dtd SQL Map 2.0//en"
"Http://www.ibatis.com/dtd/sql-map-2.dtd" >

<sqlmap namespace= "Person" >
<!--use primitive wrapper type (e.g. Integer) as parameter and allow results to
is auto-mapped results to Person object (Java Bean) Properties-->
<select id= "Getperson" parameterclass= "int" resultclass= "Com.ibeats.Person" >
SELECT per_id as ID,
Per_first_name as FirstName,
Per_last_name as LastName,
per_weight_kg as Weightinkilograms,
Per_height_m as Heightinmeters
From person
WHERE per_id = #id #
</select>

</sqlMap>


(file Element Description:
Parameterclass: Incoming parameters
ResultClass: Outgoing parameters
)

5. Database configuration file Db.properties
Driver=oracle.jdbc.driver.oracledriver
Url=jdbc:oracle:thin:@127.0.0.1:1521:oraclexc
User=scott
Password=tiger

6. Test class Sqlmapclient.java
Package com.ibeats;
Import com.ibatis.sqlmap.client.*;
Import com.ibatis.common.resources.*;
Import java.io.IOException;
Import Java.io.Reader;
Import java.sql.SQLException;
Import java.util.List;

public class Sqlmapclient {
public static void Main (String args[]) {

try {
Com.ibatis.sqlmap.client.SqlMapClient sqlmap = null;
String resource = "Com/ibeats/sqlmapconfig.xml";
Reader reader = Resources.getresourceasreader (Resource);
Sqlmap = sqlmapclientbuilder.buildsqlmapclient (reader);
person who = (person) sqlmap.queryforobject ("Getperson", New Integer (1));
System.out.println (Person.getfirstname ()); Print
catch (IOException e) {
E.printstacktrace ();
catch (SQLException E1) {
E1.printstacktrace ();
}
}
}


Description
queryForObject (): Gets a record.
queryForList (): Returns one or more rows, with parameters available to return a fixed number of rows, which can be paged.
)

All you need to do is change the database code to run it on your own machine.

Related Article

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.