Ibatis Connect Oracle Example! __oracle

Source: Internet
Author: User
Tags create database

An example of using Ibatis to change database additions and deletions:
The development environment used here is: Eclipse3.2+mysql5.0.20,ibatis package is Ibatis-common-2.jar,
Ibatis-dao-2.jar , the Ibatis-sqlmap-2.jar,mysql bag is Mysql-connector-java-5.0.3-bin.jar.
Step:
1. Create DATABASE:
 create db itcast
 use Itcast;
CREATE TABLE:
 create table Student
  (
  ID int primary key auto_increment,
  FirstName varchar NOT NULL,
  LastName varchar (a) NOT null
 )

2. Create Pojo class, Student.java, all classes used in this program are placed under the Cn.itcast package,
The other configuration files are placed under the Txd.configfile package.
Package cn.itcast;

public class Student {
Private Integer ID;

Private String FirstName;

Private String LastName;

Public String Getfirstname () {
return FirstName;
}

public void Setfirstname (String firstname) {
This.firstname = FirstName;
}

Public Integer getId () {
return ID;
}

public void SetId (Integer id) {
This.id = ID;
}

Public String Getlastname () {
return lastname;
}

public void Setlastname (String lastname) {
This.lastname = LastName;
}
}

3. The XML configuration file corresponding to the student class Student.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= "Student" >
<!--insert element, the id attribute value is used to identify this element, and the Parameterclass property is the type of the parameter.
The value of the property is the fully qualified name of the Java class (that is, the package name that includes the class). It is optional, but strongly recommended.
Its purpose is to limit the type of input parameters to the specified Java class, and to optimize the performance of the framework. # #符号中
The names from the properties of this class .-->
<insert id= "insert_student" parameterclass= "Cn.itcast.Student" >
INSERT into student (Firstname,lastname) values
(#firstname #, #lastname #)
</insert>

<select id= "getstudent" resultclass= "Cn.itcast.Student" >
Select ID, firstname, LastName from Student
</select>

<delete id= "delstudent" parameterclass= "int" >
Delete from student where id= #value #
</delete>

<update id= "updatestudent" parameterclass= "Cn.itcast.Student" >
Update student set firstname= #firstname #,lastname= #lastname #
where Id= #id #
</update>
</sqlMap>

4.jdbc.properties files, storing information such as the Driver,url,username,password of a database connection,
Driver=com.mysql.jdbc.driver
Url=jdbc:mysql:///itcast
Username=root
password=

5. Sqlmap configuration file Sqlmapconfigexample.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> element, used to use standard Java property files (name=value) in the configuration file-->
<properties resource= "Txd/configfile/jdbc.properties"/>
<!--
Cachemodelsenabled: Enables or disables all cache model for sqlmapclient globally.
Enhancementenabled: Enable or disable Run-time bytecode enhancements globally to optimize access
The performance of Java bean properties, while optimizing the performance of deferred loading.
Lazyloadingenabled: Enables or disables all deferred loading of sqlmapclient globally.
Maxrequests: The maximum number of threads executing the SQL statement at the same time.
MaxSessions: The maximum number of sessions that are active at the same time.
Maxtransactions: The maximum number of threads entering Sqlmapclient.starttransaction () at the same time.
Usestatementnamespaces: If you enable this property, you must use the fully qualified name to refer to mapped statement.
The fully qualified name of the mapped statement is synthesized by the name of the Sql-map and the name of the mapped-statement.
-->
<settings cachemodelsenabled= "true" enhancementenabled= "true"
Lazyloadingenabled= "true" maxrequests= "maxsessions=" "10"
maxtransactions= "5" usestatementnamespaces= "false"/>

<!--
The <transationManager> element lets you configure the transaction Management service for the SQL map. Property type Specifies the
The transaction manager type used. This property value can be either a class name or an alias.
The three transaction managers included in the framework were: JDBC,JTA and external.
-->
<transactionmanager type= "JDBC" >
<!--the DataSource element sets a series of parameters for the SQL map data source. -->
<datasource type= "Simple" >
<property name= "JDBC. Driver "value=" ${driver} "/>
<property name= "JDBC. Connectionurl "value=" ${url} "/>
<property name= "JDBC. Username "value=" ${username} "/>
<property name= "JDBC. Password "value=" ${password} "/>
</dataSource>
</transactionManager>
The <!--<sqlMap> element is used to include the SQL map mapping file and other SQL map configuration files. -->
<sqlmap resource= "Txd/configfile/student.xml"/>
</sqlMapConfig>

6.mysqlmapclient.java class, used to produce a sqlmapclient
Package cn.itcast;

Import java.io.IOException;
Import Java.io.Reader;

Import com.ibatis.common.resources.Resources;
Import com.ibatis.sqlmap.client.SqlMapClient;
Import Com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class Mysqlmapclient {

private static sqlmapclient sqlmapclient;

static {
String resource = "Txd/configfile/sqlmapconfigexample.xml";
Reader reader = null;
try {
Reader = Resources.getresourceasreader (Resource);
Sqlmapclient = sqlmapclientbuilder.buildsqlmapclient (reader);
catch (IOException e) {
E.printstacktrace ();
}
}

public static Sqlmapclient Getsqlmapinstance () {
return sqlmapclient;
}

}

7.StudentManager class test the above code,
 package cn.itcast
&NBSP
 import java.sql.SQLException;
 import java.util.List;
 
 import com.ibatis.sqlmap.client.SqlMapClient;
&NBSP
 public class Studentmanager {
 
  public static void main (string[] args) {
 & nbsp Sqlmapclient Sqlmap = Mysqlmapclient.getsqlmapinstance ();
 
  //Insert a record
 
  //Student Student = new Student ();
  //Student.setfirstname ("Zhang");
  //Student.setlastname ("San");
  //try {
  //Sqlmap.insert ("Insert_student", student);
  //System.out.println ("Insert success!");
  //\ catch (SQLException e) {
  //E.printstackt

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.