Atitit. Dynamically loading database configuration in ORM Hibernate MyBatis

Source: Internet
Author: User

Atitit. dynamic load Database configuration In Orm

1. Advantages of dynamic load Database configuration ::: 1

1.1. Combine multiple profiles ... 1

1.2. Connect multiple Databases 1

2. Basic process::: getcfg Memory Object ,,,, Generating factory class , opoensession 1

2.1. Hibernate Adding configuration flow dynamically 1

2.2. MyBatis Dynamic Add configuration Flow 1

2.3. #===hb Code 2

3. Reference 3

1. Advantages of dynamic load Database configuration:::1.1. Combination of multiple configuration files...1.2. Connect multiple Databases 2. The basic process::: GetcfgMemory Objects,,,,Generate Factory class,in theopoensession

Author :: old Wow's paw attilax Ayron, email:[email protected]

Reprint please indicate source: Http://blog.csdn.net/attilax

2.1. HibernateDynamic Add configuration Flow

Configure configuration objects ... is the configuration file Java Object Mappings .... will never Configuration . buildsessionfactory ();

=============

Configuration . Configure (propertyfile) ;

Configuration . SetProperty ("Connection.url", connection_url) ;

sessionfactory = configuration. buildsessionfactory ();

2.2. MyBatisDynamic Add configuration Flow

·  Properties.setproperty ("Jdbc.password", " Dynamic settings ");

·

· loading mybatis configuration files and mapping files

· String resource = "Mybatis/mybatis-config.xml";

· Reader reader = Resources.getresourceasreader (Resource);

· Sqlsessionfactorybuilder builder = new Sqlsessionfactorybuilder ();

· Sqlsessionfactory factory = Builder.build (reader, properties);

· sqlsession session = Factory.opensession ();

2.3. #===hb Code

Static   {

try {

Configuration . Configure (propertyfile) ;

O8j

Properties Properties = New properties () ;

Properties . Load ( new inputstreamreader(new fileinputstream (Pathx

. ClassPath () + "/jdbc.properties"), "Utf-8" ));

string   username   =   properties getproperty ( "jdbc.username" ) trim () ;

string   password   =   properties getproperty ( "Jdbc.password" ;

String driverclass = properties. GetProperty ("Jdbc.driverclassname")

. Trim ();

String connection_url = properties. GetProperty ("Jdbc.url") . Trim ();

// Configuration.setproperties (properties);

Configuration . SetProperty ("Connection.driver_class", driverclass) ;

Configuration . SetProperty ("Connection.url", connection_url) ;

Configuration . SetProperty ("Connection.username", username) ;

Configuration . SetProperty ("Connection.password", password) ;

Configuration . SetProperty ("Hibernate.connection.url",

Connection_url );

Configuration . SetProperty ("Hibernate.connection.driver_class",

Driverclass );

Configuration

. SetProperty ("Hibernate.connection.username", username) ;

Configuration

. SetProperty ("Hibernate.connection.password", password) ;

End O8j

Of course, in addition to the specified XML file, you can also specify the class being mapped, and hibernate will help you find the mapping definition file:

// Configuration.addclass (gvmaterial.class);

// Configuration.addannotatedclass (gvmaterial.class);

// Configuration.set

// Configuration.addresource (resourcename)

sessionfactory = configuration. buildsessionfactory ();

} catch ( Exception e ) {

System . Err

. println ("%%%% Error Creating sessionfactory%%%%") ;

e . Printstacktrace ();

Throw New runtimeexception (e);

//ati o8j

}

3. Reference

MyBatis Dynamic Connection database, dynamic incoming database parameters - Haitao's CSDN blog - Blog channel -CSDN.NET.htm

Atitit. Dynamically loading database configuration in ORM Hibernate MyBatis

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.