MyBatis a little bit of drip

Source: Internet
Author: User

2012.1.30

Download the MyBatis package, version 3.06

(1) Download MyBatis to use the jar Mybatis-3.0.6.jar to the project folder

Download DBCP jar to project at the same time, 3 jar

Commons-collections-3.2.1.jar, Commons-dbcp-1.4.jar, Commons-pool-1.6.jar


(2) Configuring the primary XML for MyBatis

Create a new XML file, save as Dbconfig.xml, here is the sample content

Here are a few points to note

1. Referencing the properties file to configure DB parameters

Introducing Files

<properties resource= "Application.properties" >
</properties>

Access value "${username}" This actually takes the value of username = xxx inside the application.properties

The benefit of this is that the definitions that are often modified are placed in a separate, simple configuration file without requiring the user to modify the configuration of the primary XML


2. Use a custom connection pool, such as DBCP

MyBatis cannot directly use an external connection pool and requires

1. Write a class that implements the interface class: Org.apache.ibatis.datasource.DataSourceFactory; public class Dbcpdatasourcefactory implements Datasourcefactory {
Private Basicdatasource datasource = null;

Public Dbcpdatasourcefactory () {
This.datasource = new Basicdatasource ();
}

Public DataSource Getdatasource () {
TODO auto-generated Method Stub
return datasource;
}

public void SetProperties (Properties PS) {
Datasource.setdriverclassname (Ps.getproperty ("Driverclassname"));
Datasource.setusername (Ps.getproperty ("username"));
Datasource.seturl (ps.getproperty ("url"));
Datasource.setpassword (Ps.getproperty ("password"));
Datasource.setdefaultautocommit (Ps.getproperty ("Defaultautocommit", "0"). Equals ("1"));
Datasource.setinitialsize (Integer.parseint (Ps.getproperty ("InitialSize", "2"));
Datasource.setmaxactive (Integer.parseint (Ps.getproperty ("Maxactive", "20"));
Datasource.setmaxidle (Integer.parseint (Ps.getproperty ("Maxidle", "0"));
Datasource.setmaxwait (Long.parselong (Ps.getproperty ("maxwait", "0"));
}
}

First, I used the alias to define the class dbcpdatasourcefactory I inherited, and then DataSource pointed to this

Then, in particular, this XML configuration is strictly ordered, such as the properties cannot be configured behind typealiases, otherwise it will be wrong to load the XML


1. Write a class that implements the interface class: Org.apache.ibatis.datasource.DataSourceFactory;

The inside defines a member DataSource, returns him inside the Getdatasource, assigns a property to him inside the setproperties
public class Dbcpdatasourcefactory implements Datasourcefactory {
Private Basicdatasource datasource = null;

Public Dbcpdatasourcefactory () {
This.datasource = new Basicdatasource ();
}

Public DataSource Getdatasource () {
TODO auto-generated Method Stub
return datasource;
}

public void SetProperties (Properties PS) {
Datasource.setdriverclassname (Ps.getproperty ("Driverclassname"));
Datasource.setusername (Ps.getproperty ("username"));
Datasource.seturl (ps.getproperty ("url"));
Datasource.setpassword (Ps.getproperty ("password"));
Datasource.setdefaultautocommit (Ps.getproperty ("Defaultautocommit", "0"). Equals ("1"));
Datasource.setinitialsize (Integer.parseint (Ps.getproperty ("InitialSize", "2"));
Datasource.setmaxactive (Integer.parseint (Ps.getproperty ("Maxactive", "20"));
Datasource.setmaxidle (Integer.parseint (Ps.getproperty ("Maxidle", "0"));
Datasource.setmaxwait (Long.parselong (Ps.getproperty ("maxwait", "0"));
}
}


3. Introduction of configuration files for each module

<mappers>
<mapper resource= "Com/talkyun/iaas/util/db/sqlmap/rhea_kvm.xml"/>
</mappers>


4. Configuration examples are as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE configuration Public "-//mybatis.org//dtd Config 3.0//en"
"Http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
<properties resource= "Application.properties" >
</properties>
<typeAliases>
<typealias type= "Com.talkyun.iaas.util.db.DBCPDataSourceFactory" alias= "rhea_dbcp"/>
</typeAliases>

<environments default= "Development" >
<environment id= "Development" >
<transactionmanager type= "JDBC"/>
<datasource type= "RHEA_DBCP" >
<property name= "Driverclassname" value= "${driverclassname}"/>
<property name= "url" value= "${url}"/>
<property name= "username" value= "${username}"/>
<property name= "Password" value= "${password}"/>
<property name= "InitialSize" value= "${initialsize}"/>
<property name= "maxactive" value= "${maxactive}"/>
</dataSource>
</environment>
</environments>

<mappers>
<mapper resource= "Com/talkyun/iaas/util/db/sqlmap/rhea_kvm.xml"/>
<mapper resource= "Com/talkyun/iaas/util/db/sqlmap/rhea_server.xml"/>
<mapper resource= "Com/talkyun/iaas/util/db/sqlmap/rhea_group.xml"/>
<mapper resource= "Com/talkyun/iaas/util/db/sqlmap/rhea_user.xml"/>
<mapper resource= "Com/talkyun/iaas/util/db/sqlmap/rhea_kvm_template.xml"/>
</mappers>
</configuration>


(3) when the above is configured, test load

Import Java.io.Reader;
Import java.sql.Connection;

Import org.apache.ibatis.io.Resources;
Import org.apache.ibatis.session.SqlSession;
Import Org.apache.ibatis.session.SqlSessionFactory;
Import Org.apache.ibatis.session.SqlSessionFactoryBuilder;

private static sqlsessionfactory sqlmapper = null;

String resource = "Com/db/config.xml";
Reader reader = Resources.getresourceasreader (Resource);
Sqlmapper = new Sqlsessionfactorybuilder (). build (reader);
sqlsession ss = Sqlmapper.opensession ();
If there is no report exception, the program is connected properly, you can start working



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.