参考
请注意后面文章中的注释
MyBatis官方入门:http://mybatis.github.io/mybatis-3/zh/getting-started.html
代码结构
1
2
3
4
|
1
. config:配置文件目录
2
. pojo:Bean类包
3
. controller:数据接口
4
. sql:数据库源文件
|
数据库创建
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
CREATE
DATABASE
IF
NOT
EXISTS `mybatis`;
USE `mybatis`;
DROP
TABLE
IF EXISTS `users`;
CREATE
TABLE
`users` (
`id`
int
(11)
NOT
NULL
AUTO_INCREMENT,
`
name
`
varchar
(45)
DEFAULT
NULL
,
`age`
int
(11)
DEFAULT
NULL
,
`phone`
varchar
(45)
DEFAULT
NULL
,
PRIMARY
KEY
(`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3
DEFAULT
CHARSET=utf8;
LOCK TABLES `users` WRITE;
INSERT
INTO
`users`
VALUES
(1,
'jimmy.song'
,22,
'13810001234'
),(2,
'sunspot'
,24,
'13812341234'
);
UNLOCK TABLES;
|
创建链接数据库的配置
位置:cc/fozone/demo/mybatis/config/mybatis.config.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<?
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
>
<!-- 配置数据库链接 -->
<
environments
default
=
"development"
>
<
environment
id
=
"development"
>
<
transactionManager
type
=
"JDBC"
/>
<
dataSource
type
=
"POOLED"
>
<!-- 数据库链接驱动 -->
<
property
name
=
"driver"
value
=
"org.gjt.mm.mysql.Driver"
/>
<!-- 数据库链接 -->
<
property
name
=
"url"
value
=
"jdbc:mysql://localhost:3306/mybatis?charset=utf8"
/>
<!-- 数据库帐号密码 -->
<
property
name
=
"username"
value
=
"root"
/>
<
property
name
=
"password"
value
=
"root"
/>
</
dataSource
>
</
environment
>
</
environments
>
<!-- 与数据库映射的配置文件列表 -->
<
mappers
>
<
mapper
resource
=
"cc/fozone/demo/mybatis/config/mapper/User.mapper.xml"
/>
</
mappers
>
</
configuration
>
|
创建User类
位置:cc/fozone/demo/mybatis/pojo/User.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package
cc.fozone.demo.mybatis.pojo;
public
class
User {
private
int
id;
private
String name;
private
int
age;
private
String phone;
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id = id;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
int
getAge() {
return
age;
}
public
void
setAge(
int
age) {
this
.age = age;
}
public
String getPhone() {
return
phone;
}
public
void
setPhone(String phone) {
this
.phone = phone;
}
public
String toString(){
return
"{id:"
+id+
",name:"
+name+
",age:"
+age+
",phone:"
+phone+
"}"
;
}
}
|
创建User的数据接口
位置:cc/fozone/demo/mybatis/controller/IUserController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package
cc.fozone.demo.mybatis.controller;
import
java.util.List;
import
cc.fozone.demo.mybatis.pojo.User;
public
interface
IUserController {
// 查询所有的用户
public
List<User> getAllUsers();
// 根据电话查询用户
public
User getUser(String phone);
}
|
创建User表对应的映射配置
位置:cc/fozone/demo/mybatis/config/mapper/User.mapper.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
<?
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">
<!--
namespace:与Controller接口匹配
-->
<
mapper
namespace
=
"cc.fozone.demo.mybatis.controller.IUserController"
>
<!--
User表映射
type:类地址
id:实例化ID
-->
<
resultMap
type
=
"cc.fozone.demo.mybatis.pojo.User"
id
=
"userResultMap"
>
<
id
property
=
"id"
column
=
"id"
/>
<
result
property
=
"name"
column
=
"name"
/>
<
result
property
=
"age"
column
=
"age"
/>
<
result
property
=
"phone"
column
=
"phone"
/>
</
resultMap
>
<!--
select 查询语句
id:唯一标示符,与Controller的接口方法一致
resultMap: 查询的数据实例化注入的对象标示引用
-->
<
select
id
=
"getAllUsers"
resultMap
=
"userResultMap"
>
<![CDATA[
select id,name,age,phone from users
]]>
</
select
>
<!--
#{phone}:参数,与Controller中的getUser方法中的参数一致
resultType:查询的数据库实例化注入的类型,与resultMap不能同时使用,但建议使用resultMap
-->
<
select
id
=
"getUser"
resultType
=
"cc.fozone.demo.mybatis.pojo.User"
>
<![CDATA[
select id,name,age,phone from users where phone = #{phone}
]]>
</
select
>
</
mapper
>
|
模拟服务调用
位置:cc/fozone/demo/mybatis/App.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
package
cc.fozone.demo.mybatis;
import
java.io.IOException;
import
java.io.InputStream;
import
java.util.List;
import
org.apache.ibatis.io.Resources;
import
org.apache.ibatis.session.SqlSession;
import
org.apache.ibatis.session.SqlSessionFactory;
import
org.apache.ibatis.session.SqlSessionFactoryBuilder;
import
cc.fozone.demo.mybatis.controller.IUserController;
import
cc.fozone.demo.mybatis.pojo.User;
/**
* Hello world!
*
*/
public
class
App {
// 数据库链接配置
private
static
final
String CONFIG =
"cc/fozone/demo/mybatis/config/mybatis.config.xml"
;
// SQL会话工厂
private
SqlSessionFactory factory;
public
App() {
// 初始化
this
.init();
}
// 初始化SQL会话工厂
private
void
init() {
InputStream input;
try
{
input = Resources.getResourceAsStream(CONFIG);
this
.factory =
new
SqlSessionFactoryBuilder().build(input);
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 查询所有用户列表
public
List<User> getUsers() {
// 打开会话
SqlSession session =
this
.factory.openSession();
// 根据映射实例化控制器对象,这里的IUserController.class与Mapper配置中的namespace一致
IUserController controller = session.getMapper(IUserController.
class
);
// 获得所有用户
List<User> list = controller.getAllUsers();
// 关闭会话
session.close();
return
list;
}
// 根据电话号查询用户
public
User getUser(String phone) {
User user =
null
;
// 会话
SqlSession session =
this
.factory.openSession();
IUserController controller = session.getMapper(IUserController.
class
);
// 查询用户,这里传入的参数会被替换在Mapper中的#{phone}占位符
user = controller.getUser(phone);
session.close();
return
user;
}
}
|
创建测试用例
位置:cc/fozone/demo/mybatis/AppTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package
cc.fozone.demo.mybatis;
import
java.util.List;
import
org.junit.Before;
import
org.junit.Test;
import
static
org.junit.Assert.*;
import
cc.fozone.demo.mybatis.pojo.User;
public
class
AppTest {
private
App app;
@Before
public
void
setUp()
throws
Exception {
app =
new
App();
assertNotNull(app);
}
@Test
public
void
testList() {
List<User> list = app.getUsers();
assertEquals(
2
,list.size());
}
@Test
public
void
testUser(){
User user = app.getUser(
"13810001234"
);
assertNotNull(user);
assertEquals(
"jimmy.song"
, user.getName());
}
}
|
结果
AppTest.java文件鼠标右键 -> Run -> JUnit Test
本文转自 sundunjam 51CTO博客,原文链接:http://blog.51cto.com/sunspot/1554965,如需转载请自行联系原作者