The first integration of spring and mybatis today is a rough process. It took three hours to complete the integration. I am afraid there are dozens of errors, especially the package guide.
Now, the results posted today are worthy of these five hours. The following figure shows the overall structure.
This is the spring control file applicationContext. xml !)
[Html]
<Span style = "font-size: 14px;"> <? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<! -- Create a jdbc data source -->
<Bean id = "dataSource" class = "org. apache. commons. dbcp. BasicDataSource" destroy-method = "close">
<Property name = "driverClassName" value = "oracle. jdbc. driver. OracleDriver"/>
<Property name = "url" value = "jdbc: oracle :****:@***. ***. **. **: 1521: ***** "/>
<Property name = "username" value = "******"/>
<Property name = "password" value = "******"/>
</Bean>
<Bean id = "sqlSessionFactory" class = "org. mybatis. spring. SqlSessionFactoryBean">
<Property name = "dataSource" ref = "dataSource"/>
<Property name = "configLocation" value = "classpath: configuration. xml"/>
</Bean>
<Bean id = "sqlSession" class = "org. mybatis. spring. SqlSessionTemplate">
<Constructor-arg index = "0" ref = "sqlSessionFactory"/>
</Bean>
<Bean id = "customerTest" class = "com. test. Test">
<! -- Inject SqlSessionTemplate instance -->
<Property name = "sqlSessionTemplate" ref = "sqlSession"/>
</Bean>
</Beans>
</Span>
This is the configuration. xml configuration file of mybatis. There is no way. If the rice bowl is snatched by spring, only this is left.
[Html]
<Span style = "font-size: 14px;"> <? 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 = "Customer" type = "com. test. Customer"/>
</TypeAliases>
<Mappers>
<Mapper resource = "com/test/Customer. xml"/>
</Mappers>
</Configuration>
</Span>
This is the data ing file customer. xml.
[Html] view plaincopy
<Span style = "font-size: 14px;"> <? 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 = "Customer">
<Select id = "selectCustomer" parameterType = "int" resultType = "Customer">
Select * from tcor_crm_customer where CUSTID =#{ id}
</Select>
</Mapper> </span>
The customer's POJO class is saved. The setter and getter methods are super simple. Remember to rewrite the toString method.
This is the test method
[Java]
<Span style = "font-size: 14px;"> package com. test;
Import org. mybatis. spring. support. SqlSessionDaoSupport;
Import org. springframework. beans. factory. BeanFactory;
Import org. springframework. context. support. ClassPathXmlApplicationContext;
Public class Test extends SqlSessionDaoSupport {
Public static void main (String [] args ){
// TODO Auto-generated method stub
New Test (). firstTest ();
}
Public void firstTest (){
BeanFactory factory = new ClassPathXmlApplicationContext ("applicationContext. xml ");
Test test = (Test) factory. getBean ("customerTest ");
Customer cus = (Customer) test. getSqlSession (). selectOne ("selectCustomer", 10696 );
System. out. println (cus );
}
}
</Span>