One of the spring IBatis Struts2 integrations: Spring and IBatis integration

Source: Internet
Author: User
Tags config log4j
The whole process of integrating spring IBatis is as follows:
All required *.jar files are introduced. Configure Web. Xml.
Configure Log4j.
Configure the C3P0 database connection pool. Configure spring and integrate with Ibatis. Build the database table structure and its domain class and configuration to write the DAO class. Write the Unit test class.
first, the required class library | Required jar ListC3p0-0.9.1.2.jar//Connection pool implementation class Library
Commons-logging-1.0.4.jar
Ibatis-2.3.4.726.jar
Log4j-1.2.15.jar
Ojdbc14-10g.jar//Oracle JDBC Driver class Library
Spring-2.5.6.jar
Spring-test.jar//unit tests need to use the class library
Junit-4.4.jar//unit tests need to use the class library

ii. Configuring Web. Xml |
Add the following in Web. xml: <!--load Spring configuration file--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
Classpath:ApplicationContext.xml
</param-value>
</context-param>

<!--Spring Listener configuration-
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
three, Configuration log4j | How to configure log4j
Create Log4j.xml in the Src/main/resouces directory with the following contents: <?xml version= "1.0"?>
<! DOCTYPE log4j:configuration SYSTEM "LOG4J.DTD" >

<log4j:configuration xmlns:log4j= "http://jakarta.apache.org/log4j/" >

<appender name= "Sis_log_file" class= "Org.apache.log4j.DailyRollingFileAppender" >
<param name= "File" value= "Log/sis.log"/>
<param name= "Datepattern" value= ". Yyyymmddhh"/>
<layout class= "Org.apache.log4j.PatternLayout" >
<param name= "Conversionpattern"
Value= "%d%p%t%c{3}%m%n"/>
</layout>
</appender>

<root>
<level value= "Info"/>
<appender-ref ref= "Sis_log_file"/>
</root>
</log4j:configuration>
Iv. Configuring the C3P0 database Connection pool | How to configure C3P0 JDBC Connection pool
The jdbc.properties is established under the Src/main/resouces directory, which reads as follows:
#c3p0 data source configuration;
Jdbc.driverclassname=oracle.jdbc.oracledriver
Jdbc.url=jdbc:oracle:thin:@192.168.0.8:1521:orcl10
Jdbc.username=aofeng
Jdbc.password=aofeng

# Number of connections established after the connection pool has completed initialization
Initialpoolsize=0

# The minimum number of connections to the connection pool
minpoolsize=2

# Connection Pool Maximum number of connections
maxpoolsize=10

# Maximum idle time (in seconds) for the connection to be reclaimed after the connection idle timeout
maxidletime=1000

idleconnectiontestperiod=1000

2. Then add the following configuration in spring's configuration applicationcontext.xml:
<!--load the resource file, the following datasource references the configuration item in the resource file--
<context:property-placeholder location= "Classpath:jdbc.properties"/>

<!--data Source configuration--
<bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method= "Close" >
<property name= "Driverclass" value= "${jdbc.driverclassname}"/>
<property name= "Jdbcurl" value= "${jdbc.url}"/>
<property name= "user" value= "${jdbc.username}"/>
<property name= "Password" value= "${jdbc.password}"/>
<property name= "MaxIdleTime" value= "${maxidletime}"/>
<property name= "maxpoolsize" value= "${maxpoolsize}"/>
<property name= "minpoolsize" value= "${minpoolsize}"/>
<property name= "initialpoolsize" value= "${initialpoolsize}"/>
<property name= "Idleconnectiontestperiod" value= "${idleconnectiontestperiod}"/>
</bean>
v. Configuring spring and Ibatis Integration | How to configure spring integrates with Ibatis
In the Src/main/resouces directory, establish the Ibatis SQL mapping configuration file Sqlmapconfig.xml, which reads as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Sqlmapconfig
Public "-//ibatis.apache.org//dtd SQL Map Config 2.0//en"
"Http://ibatis.apache.org/dtd/sql-map-config-2.dtd" >

<sqlMapConfig>
<settings cachemodelsenabled= "true" usestatementnamespaces= "true"/>

<!--here to begin adding Sqlmap configuration Files--
</sqlMapConfig>
2. Add the following configuration in spring's configuration applicationcontext.xml:
<!--IBatis ORM operation Class--
<bean id= "sqlmapclient" class= "Org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
<property name= "DataSource" ref= "DataSource"/>
<property name= "configlocation" value= "Classpath:SqlMapConfig.xml"/>
</bean>

<!--spring Ibatis templates--
<bean id= "Sqlmapclienttemplate" class= "Org.springframework.orm.ibatis.SqlMapClientTemplate" >
<property name= "sqlmapclient" ref= "Sqlmapclient" ></property>
</bean>

<!--transaction Management configuration--
<bean id= "TransactionManager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" ref= "DataSource"/>
</bean>

<tx:annotation-driven transaction-manager= "TransactionManager"/>
vi. Building data table structures and Domain classes | Create table and its corresponding domain class
The table structure that tests the integration of spring and Ibatis is as follows:
CREATE TABLE USER
(
USER_ID Number (TEN) is not NULL,
User_type Number (2),
User_status Number (1) is not NULL,
User_name VARCHAR2 (one) not NULL,
USER_PASSWD VARCHAR2 () NOT NULL,
Create_time DATE NOT NULL,
Update_time DATE NOT NULL,
Last_login_time DATE
);
ALTER TABLE USER
Add constraint Pk_user primary key (USER_ID)
Using Index;

2, establish the User table corresponding domain class: User.java, its contents are as follows:
/**
* Setup Time: 2011-3-19
*/
Package cn.aofeng.sis.domain;

Import Java.util.Date;

/**
* Table user corresponding to the persistent layer Pojo.
*
* @author proud wind <a href= "mailto:aofengblog@163.com" >aofengblog@163.com</a>
*/
public class User {

/**
* This field corresponds to the database column USER. user_id
*/
Private Long userId;

/**
* This field corresponds to the database column USER. User_type
*/
Private short usertype;

/**
* This field corresponds to the database column USER. User_status
*/
Private short userstatus;

/**
* This field corresponds to the database column USER. User_name
*/
Private String UserName;

/**
* This field corresponds to the database column USER. user_passwd
*/

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.