Ibatis latest and most complete Development Guide-easy to understand ibat tutorial (2)

Source: Internet
Author: User
Tags event listener
Oscache comes from a third-party organization, opensymphony. You can use
Get the latest version of Oscache (http://www.opensymphony.com/oscache ).
We recommend that you use Oscache for production deployment. Oscache is a widely used open source cache implementation.
(Hibernate also provides support for Oscache). It is based on a more reliable and efficient design. More importantly,
The latest version of Oscache supports cache clusters. If the system needs to be deployed in the cluster or
Oscache is the best choice for multi-server Load balancer environments to achieve performance advantages.
The Oscache configuration in ibatis is quite simple:
<Cachemodel id = "usercache" type = "Oscache">
<Flushinterval hours = "24"/>
<Flushonexecute Statement = "updateuser"/>
<Property name = "size" value = "1000"/>
</Cachemodel>
The reason for simple configuration is that Oscache has its own configuration file (Oscache. properties) J.
The following is a typical Oscache configuration file:
# Whether to use memory as the cache space
Cache. Memory = true
# Cache Management event listener. You can use this listener to learn the running status of the current cache.
Cache. event. Listeners = com. opensymphony. Oscache. plugins. clustersupport. jmsbroa
Dcastinglistener
# If you use disk cache (Cache. Memory = false), you must specify the disk storage interface to implement
# Cache. Persistence. Class = com. opensymphony. Oscache. plugins. diskpersistence. Disk
Persistencelistener
# File storage path used by the disk cache
# Cache. Path = C: myappcache
# Cache scheduling algorithm. Available options include LRU, FIFO, and unlimited cache)

# Cache. algorithm = com. opensymphony. Oscache. Base. algorithm. datagocache
# Cache. algorithm = com. opensymphony. Oscache. Base. algorithm. unlimitedcache
Cache. algorithm = com. opensymphony. Oscache. Base. algorithm. lrucache
# Maximum memory cache capacity
Cache. capacity = 1000
# Whether to limit the disk cache capacity
# Cache. Unlimited. Disk = false
# JMS-Based Cluster cache synchronization Configuration
# Cache. Cluster. JMS. Topic. Factory = Java: COMP/ENV/JMS/topicconnectionfactory
# Cache. Cluster. JMS. Topic. Name = Java: COMP/ENV/JMS/oscachetopic
# Cache. Cluster. JMS. node. Name = node1
# Cluster cache synchronization configuration based on javagroup
# Cache. Cluster. properties = UDP (mcast_addr = 231.12.21.132; mcast_port = 45566; IP _
TTL = 32; mcast_send_buf_size = 150000; mcast_recv_buf_size = 80000): Ping (timeout
= 2000; num_initial_members = 3): merge2 (min_interval = 5000; max_interval = 10000
): Fd_sock: verify_suspect (timeout = 1500): pbcast. nakack (gc_lag = 50; retransm
It_timeout = 300,600,120 4800 20000,): pbcast. Stable (desired_avg_gossip = ):
Unicast (timeout = 5000): Frag (frag_size = 8096; down_thread = false; up_thread = Fal
Se): pbcast. GMS (join_timeout = 5000; join_retry_timeout = 2000; shun = false; print_loc
Al_addr = true)
# Cache. Cluster. multicast. IP = 231.12.21.132
After configuration, put this file in classpath. Oscache will automatically find this file during initialization
File and create a cache instance according to the configuration.

Ibatis in spring
Here we will focus on ibatis applications under the Spring framework, especially ibatis in the container transaction management mode.
Application development.
For more information about Spring framework, see the following document:
Spring Development Guide http://www.xiaxin.net/Spring_Dev_Guide.rar
For ibatis, the spring configuration file is as follows:
Ibatis-Context.xml:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en"
">
<Beans>
<Bean id = "datasource"
Class = "org. Apache. commons. DBCP. basicdatasource"
Destroy-method = "close">
<Property name = "driverclassname">
<Value> net. SourceForge. jtds. JDBC. Driver </value>
</Property>
<Property name = "url">
<Value> JDBC: jtds: sqlserver: // 127.0.0.1: 1433/sample </value>
</Property>
<Property name = "username">
<Value> test </value>
</Property>
<Property name = "password">
<Value> changeit </value>
</Property>
</Bean>
<Bean id = "sqlmapclient"
Class = "org. springframework. Orm. ibatis. sqlmapclientfactorybean">
<Property name = "configlocation">
<Value> sqlmapconfig. xml </value>
</Property>
</Bean>
<Bean id = "transactionmanager"
Class = "org. springframework. JDBC. datasource. datasourcetransactio
Nmanager ">
Http://www.springframework.org/dtd/spring-beans.dtd

 

 

<Property name = "datasource">
<Ref local = "datasource"/>
</Property>
</Bean>
<Bean id = "userdao" class = "net. xiaxin. Dao. userdao">
<Property name = "datasource">
<Ref local = "datasource"/>
</Property>
<Property name = "sqlmapclient">
<Ref local = "sqlmapclient"/>
</Property>
</Bean>
<Bean id = "userdaoproxy"
Class = "org. springframework. transaction. Interceptor. transactionpro
Xyfactorybean ">
<Property name = "transactionmanager">
<Ref bean = "transactionmanager"/>
</Property>
<Property name = "target">
<Ref local = "userdao"/>
</Property>
<Property name = "transactionattributes">
<Props>
<Prop key = "insert *"> propagation_required </prop>

<Prop key = "get *"> propagation_required, readonly </prop>
</Props>
</Property>
</Bean>
</Beans>
You can see:
1. sqlmapclient Node
The sqlmapclient node is actually configured with a sqlmapclient creation factory class.
The name of the ibatis ing file is configured in the configlocation attribute.
2. transactionmanager Node
Transactionmanager uses datasourcetransactionmanager in spring.
3. userdao Node

 

Corresponding, userdao requires two attributes: sqlmapclient and datasource,
Sqlmapclient obtains the database connection from the specified datasource.
In this example, the ibatis ing file is very simple:
Sqlmapconfig. xml:
<Sqlmapconfig>
<Sqlmap resource = "net/xiaxin/Dao/entity/user. xml"/>
</Sqlmapconfig>
Net/xiaxin/Dao/entity/user. xml
<Sqlmap namespace = "user">
<Typealias alias = "user" type = "net. xiaxin. Dao. entity. User"/>
<Insert id = "insertuser" parameterclass = "user">
Insert into users (username, password) values (# username #,
# Password #)
</Insert>
</Sqlmap>
Userdao. Java:
Public class userdao extends sqlmapclientdaosupport implements
Iuserdao {
Public void insertuser (User user ){
Getsqlmapclienttemplate (). Update ("insertuser", user );
}
}
Sqlmapclientdaosupport (if ibatis 1.x is used, the corresponding support class is
Sqlmapdaosupport) is a helper class for ibatis in spring. It schedules datasource,

 

Sqlmapclienttemplate (corresponding to the version of ibatis 1.x is sqlmaptemplate) to complete the ibatis operation,
Dao obtains the above functions through extension of this class. Attribute settings for userdao in the configuration file above
Property is also inherited from this base class.
Sqlmapclienttemplate encapsulates the traditional sqlmapclient call mode to simplify upper-layer access
Code.
User. Java:
Public class user {
Public integer ID;
Public String username;

 

 

Public String password;
Public integer GETID (){
Return ID;
}
Public void setid (integer ID ){
This. ID = ID;
}
Public String GetPassword (){
Return password;
}
Public void setpassword (string password ){
This. Password = password;
}
Public String GetUserName (){
Return username;
}
Public void setusername (string username ){
This. Username = username;
}
}
Test code:
Inputstream is = new fileinputstream ("Ibatis-Context.xml ");
Xmlbeanfactory factory = new xmlbeanfactory (is );
Iuserdao userdao = (iuserdao) Factory. getbean ("userdaoproxy ");
User user = new user ();
User. setusername ("Sofia ");
User. setpassword ("mypass ");
Userdao. insertuser (User );
Compared with the previous ibatis program code, we can find that userdao. Java becomes abnormal and concise, Which is exactly
Spring framework brings us great help.
Spring and other IOC frameworks are revolutionizing traditional application frameworks.

 

This is the first time I ran spring helloworld. Interested
Readers can study Spring framework and enjoy it!

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.