http://my.oschina.net/zimingforever/blog/111907
这个博客早就想写了。最近在搞mybatis的mapper解析器,所以特意学习并研究了下mybatis,其实mybatis的使用和ibatis大同小异,之前搞了些ibatis的博客,也可以参考一下。这里一个mybatis官方参考文档,是我学习的主要参考:
http://mybatis.github.com/mybatis-3/zh/index.html
http://mybatis.github.com/spring/zh/index.html
本次博客主要介绍mybatis的环境搭建及如何和搭配spring使用,关于动态sql的部分可能会放在后面找一个专题来写。建议要有一定的ibatis的基础
1maven组织结构所需要的jar包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<dependency>
<groupId>org.mybatis<
/groupId
>
<artifactId>mybatis<
/artifactId
>
<version>3.2.0<
/version
>
<classifier>sources<
/classifier
>
<
/dependency
>
<dependency>
<groupId>org.mybatis<
/groupId
>
<artifactId>mybatis<
/artifactId
>
<version>3.2.0<
/version
>
<
/dependency
>
<dependency>
<groupId>log4j<
/groupId
>
<artifactId>log4j<
/artifactId
>
<version>1.2.15<
/version
>
<
/dependency
>
<dependency>
<groupId>mysql<
/groupId
>
<artifactId>mysql-connector-java<
/artifactId
>
<version>5.1.16<
/version
>
<
/dependency
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<
configuration
>
<
properties
resource
=
"mysql.properties"
></
properties
>
<
environments
default
=
"development"
>
<
environment
id
=
"development"
>
<
transactionManager
type
=
"JDBC"
/>
<
dataSource
type
=
"POOLED"
>
<
property
name
=
"driver"
value
=
"${driver}"
/>
<
property
name
=
"url"
value
=
"${url}"
/>
<
property
name
=
"username"
value
=
"${username}"
/>
<
property
name
=
"password"
value
=
"${password}"
/>
</
dataSource
>
</
environment
>
</
environments
>
<
mappers
>
<
mapper
resource
=
"org/mybatis/example/BlogMapper.xml"
/>
</
mappers
>
</
configuration
>
|
enviroments可以配置事务管理器,主要有两种类型,一种是JDBC,另一种是managed
datasource用来配置数据源,其有三种类型,unpooled(每次查询都打开关闭连接),pooled(连接池),JNDI。
接下来是mybatis的mapper文件
1
2
3
4
5
|
<
mapper
namespace
=
"org.mybatis.example.BlogMapper"
>
<
select
id
=
"selectBlog"
parameterType
=
"int"
resultType
=
"org.mybatis.example.Blog"
>
select * from Blog where id = #{id}
</
select
>
</
mapper
>
|
1
2
3
4
5
6
7
8
9
10
11
12
|
public
static
void
main(String[] args)
throws
IOException {
String resource =
"org/mybatis/example/mybatis-config.xml"
;
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new
SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
try
{
Blog blog = (Blog) session.selectOne(
"org.mybatis.example.BlogMapper.selectBlog"
,
1
);
System.out.println(
"blog.name="
+blog.getName());
}
finally
{
session.close();
}
}
|
另外mybatis和spring也有很好的结合,如果在mybatis中使用spring的话,需要在maven中额外的引入jar包
1
2
3
4
5
6
7
8
9
10
11
|
<
dependency
>
<
groupId
>org.mybatis</
groupId
>
<
artifactId
>mybatis-spring</
artifactId
>
<
version
>1.1.1</
version
>
<
classifier
>sources</
classifier
>
</
dependency
>
<
dependency
>
<
groupId
>org.mybatis</
groupId
>
<
artifactId
>mybatis-spring</
artifactId
>
<
version
>1.1.1</
version
>
</
dependency
>
|
spring的配置文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<!-- 导入属性配置文件 -->
<
context:property-placeholder
location
=
"classpath:mysql.properties"
/>
<
bean
id
=
"dataSource"
class
=
"org.springframework.jdbc.datasource.DriverManagerDataSource"
>
<
property
name
=
"driverClassName"
value
=
"${driver}"
/>
<
property
name
=
"url"
value
=
"${url1}"
/>
</
bean
>
<
bean
id
=
"transactionManager"
class
=
"org.springframework.jdbc.datasource.DataSourceTransactionManager"
>
<
property
name
=
"dataSource"
ref
=
"dataSource"
/>
</
bean
>
<
bean
id
=
"sqlSessionFactory"
class
=
"org.mybatis.spring.SqlSessionFactoryBean"
>
<
property
name
=
"configLocation"
value
=
"classpath:org/mybatisspring/test/mybatis-config.xml"
/>
<
property
name
=
"dataSource"
ref
=
"dataSource"
/>
</
bean
>
|
接下来是如何配置mapper文件
1
2
3
4
5
6
7
8
9
|
<!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">-->
<!--<property name="annotationClass" value="org.springframework.stereotype.Repository"/>-->
<!--<property name="basePackage" value="org.mybatisspring.test"/>-->
<!--<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>-->
<!--</bean>-->
<
bean
id
=
"studentMapper"
class
=
"org.mybatis.spring.mapper.MapperFactoryBean"
>
<
property
name
=
"mapperInterface"
value
=
"org.mybatisspring.test.StudentMapper"
/>
<
property
name
=
"sqlSessionFactory"
ref
=
"sqlSessionFactory"
/>
|
1
2
3
4
5
|
@Repository
@Transactional
public
interface
StudentMapper {
public
StudentEntity getStudent(String studentID);
}
|
1
2
3
4
5
6
|
public
static
void
main(String args[]){
ApplicationContext ac=
new
FileSystemXmlApplicationContext(
"applicationcontext.xml"
);
StudentMapper studentMapper= ac.getBean(StudentMapper.
class
);
StudentEntity entity = studentMapper.getStudent(
"123123"
);
System.out.println(
"name:"
+ entity.getStudentName());
}
|
参考文档:
http://sunfish.iteye.com/blog/1472693
http://limingnihao.iteye.com/blog/781671
总结一下。本文主要介绍了mybatis的使用及如何和spring配合使用。关于mybasit的底层实现和动态sql我准备以后单独在准备一个博客来写。