why Mybaits to integrate spring?
In fact, you want to use the services provided by spring, such as spring's transaction management, the spring IOC's management of beans, and so on.
How does MyBatis integrate spring?
Since spring does not yet have an integrated MyBatis feature, the framework "Mybatis-spring-1.0.2.jar" for consolidation is added to the spring framework and the MyBatis framework (when the framework MyBatis the official itself).
(1) Create a new Web project with the name Mybatisspring.
(2) put the jar packages of Spring3.0.3, Mybatis3.0.6, mybatis-spring1.0.2, log4j, Oracle driver and DBCP connection pool under the Lib of the Web project, the specific jar package is as follows: Java code Classes12.jar log4j-1.2.16.jar Mybatis-3.0.6.jar Mybatis-spring-1.0.2.jar Org.springframework.aop-3.0.3.release.jar Org.springframework.asm-3.0.3.release.jar Org.springframework.aspects-3.0.3.release.jar Org.springframework.beans-3.0.3.release.jar Org.springframework.context-3.0.3.release.jar Org.springframework.context.support-3.0.3.release.jar Org.springframework.core-3.0.3.release.jar Org.springframework.expression-3.0.3.release.jar Org.springframework.jdbc-3.0.3.release.jar Org.springframework.transaction-3.0.3.release.jar Org.springframework.web-3.0.3.release.jar Commons-logging-1.1.1.jar Commons-dbcp-1.2.jar Commons-pool-1.4.jar
(3) under SRC, create a new Log4j.properties file with the following contents: Java code Log4j.appender.stdout=org.apache.log4j.consoleappender Log4j.appender.stdout.layout=org.apache.log4j.patternlayout log4j.appender.stdout.layout.conversionpattern=%d%p [%c]-%m%n log4j.logger.com.ibatis=debug log4j.logger.com.ibatis.common.jdbc.simpledatasource=debug Log4j.logger.com.ibatis.common.jdbc.scriptrunner=debug Log4j.logger.com.ibatis.sqlmap.engine.impl.sqlmapclientdelegate=debug Log4j.logger.java.sql.connection=debug Log4j.logger.java.sql.statement=debug Log4j.logger.java.sql.preparedstatement=debug,stdout
(4) in the Oracle database, execute the following SQL to create a user_info table:
Java code--Create TABLE CREATE TABLE User_info (ID number (n) not NULL, NAME VARCHAR2 (50)); --insert data Insert into User_info (id,name) VALUES (1, ' Zhang San ');
(5) Create a new Java class Userinfo.java, the contents of which are as follows:
Java code Package com.user; public class UserInfo { private int id; private String name; public UserInfo () { } &N Bsp public UserInfo (String name) { This (0, name); } public UserInfo (int ID, String name) { &nbs p; this.id = id; this.name = name; } public int getId () { & nbsp return id; } public void setId (int id) {   ; this.id = id; } public String GetName () { return name; } public void SetName (String name) {& nbsp this.name = name; } @Override public String toString () { Return "ID:" + ID + ", Name:" + name; } }
(6) Create a new Userinfo.xml file under Com.user.sqlmap, the contents of which are as follows:
Java code <?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace= "User" > <select id= "selectuser" parametertype= "int" resulttype= "UserInfo" > <![ cdata[SELECT * from user_info where id = #{id}]]> </select> </mapper>
(7) under SRC, create a new Mybatis.cfg.xml file with the following contents: Java code <?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> <typeAliases> <typealias alias= "UserInfo" type= "Com.user.UserInfo"/& Gt </typeAliases> <mappers> <mapper resource= "Com/user/sqlmap/userinfo.xml"/> < ;/mappers> </configuration>
(8) Create a new Java class Userservice.java, the contents of which are as follows:
Java code Package com.user; Import org.mybatis.spring.sqlsessiontemplate; public class UserService { Private sqlsessiontemplate sqlsession; public sqlsessiontemplate getsqlsession () { return sqlsession; } public void setsqlsession ( Sqlsessiontemplate sqlsession) { this.sqlsession = sqlsession; } public UserInfo selectuser () { UserInfo user = null; try { user = (UserinFO) sqlsession.selectone ("User.selectuser", "1"); catch (Exception e) { e.printstacktrace (); } return user; } }
(9) Create a new Applicationcontext.xml file under SRC, the contents of which are as follows: Java code <?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "http://www.springframework.org/ Schema/beans " default-autowire=" byname " xmlns:xsi="/http/ Www.w3.org/2001/XMLSchema-instance " xsi:schemalocation=" http://www.springframework.org/ Schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> <bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" > <property name= "Driverclassname" value= "Oracle.jdbc.OracleDriver"/> <property name= "url" value= "Jdbc:oracle:thin: @localhost: 1521:sid"/> <property name= "username" value= "xxxx"/> <property name= "password" value= "xxxx"/> &NBSP;&NBSP;&NBSP;&NBSP;&NBsp; <property name= "maxactive" value= "></property> <property name= "Maxidle" value= "All" ></property> <property name= "maxwait" value= "$" ></property> <property name= "Defaultautocommit" value= "true" ></property> </bean> <bean id= "sqlsessionfactory" class= " Org.mybatis.spring.SqlSessionFactoryBean "> <property name= "Configlocation" value= "Classpath:mybatis.cfg.xml" ></property> <property name= "DataSource" ref= "DataSource"/> </bean> <bean id= "sqlsessiontemplate" class= "org.mybatis.spring.SqlSeSsiontemplate "> <constructor-arg index=" 0 "ref=" Sqlsessionfactory "/> </bean> <bean id= "UserService" class= "Com.user.UserService" > < Property Name= "Sqlsession" ref= "sqlsessiontemplate"/> </bean> </ beans>
(10) Create a new test Java class Userinfotest.java, the specific contents of this class are as follows: Java code Package com.user; Import java.io.ioexception; Import org.springframework.context.applicationcontext; Import org.springframework.context.support.classpathxmlapplicationcontext; public class Userinfotest { /** * @param args * @thr oWS IOException */ public static void Main (string[] args) throws Ioexce Ption { ApplicationContext context = new Classpathxmlapplicationcontext ("Applicationcontext.xml"); userservice userservice = (userservice) context.getbean ("UserService"); UserInfo UserInfo = Userservice.selectuser (); System.out.println (userInfo); } }
(11) Right-userinfotest class, choose Run as application, run the mybaits operation database.
Java code Log4j:warn No Appenders could is found for logger ( Org.springframework.context.support.ClassPathXmlApplicationContext). Log4j:warn Please initialize the log4j system properly. Log4j:warn See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 2012-02-11 21:13:42,156 DEBUG [java.sql.PreparedStatement]-==> executing:select * from user_info where id =? 2012-02-11 21:13:42,156 DEBUG [java.sql.PreparedStatement]-==> parameters:1 (String) id:1, Name: Zhang San