1
2
3
4
|
<
mappers
>
<
mapper
resource
=
"mapper.xml文件的地址"
/>
<
mapper
resource
=
"mapper.xml文件的地址"
/>
mappers
>
|
1
2
3
|
<
bean
id
=
" "
class
=
"mapper接口的实现"
>
<
property
name
=
"sqlSessionFactory"
ref
=
"sqlSessionFactory"
>
property
>
bean
>
|
1
2
3
4
|
<
mappers
>
<
mapper
resource
=
"mapper.xml文件的地址"
/>
<
mapper
resource
=
"mapper.xml文件的地址"
/>
mappers
>
|
1
2
3
4
|
<
bean
id
=
""
class
=
"org.mybatis.spring.mapper.MapperFactoryBean"
>
<
property
name
=
"mapperInterface"
value
=
"mapper接口地址"
/>
<
property
name
=
"sqlSessionFactory"
ref
=
"sqlSessionFactory"
/>
bean
>
|
1
2
3
4
|
<
bean
class
=
"org.mybatis.spring.mapper.MapperScannerConfigurer"
>
<
property
name
=
"basePackage"
value
=
"mapper接口包地址"
>
property
>
<
property
name
=
"sqlSessionFactoryBeanName"
value
=
"sqlSessionFactory"
/>
bean
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
configuration
>
<
environments
default
=
"development"
>
<
environment
id
=
"development"
>
<
transactionManager
type
=
"JDBC"
/>
<
dataSource
type
=
"POOLED"
>
<
property
name
=
"driver"
value
=
"com.mysql.jdbc.Driver"
/>
<
property
name
=
"url"
value
=
"jdbc:mysql://localhost:3306/mybatis45?characterEncoding=utf-8"
/>
<
property
name
=
"username"
value
=
"root"
/>
<
property
name
=
"password"
value
=
"root"
/>
dataSource
>
environment
>
environments
>
<
mappers
>
<
mapper
resource
=
"sqlmap/User.xml"
/>
mappers
>
configuration
>
|
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
|
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<
mapper
namespace
=
"user"
>
<
select
id
=
"findUserById"
parameterType
=
"Integer"
resultType
=
"com.github.pojo.User"
>select * from user where id = #{id}
select
>
<
insert
id
=
"addUser"
parameterType
=
"com.github.pojo.User"
>
insert into user
(username, birthday,sex,address)
values(#{username},#{birthday},#{sex},#{address})
insert
>
<
delete
id
=
"deleteUser"
parameterType
=
"Integer"
>
delete from user where id = #{id}
delete
>
<
update
id
=
"updateUser"
parameterType
=
"com.github.pojo.User"
>
update user set
username = #{username},
birthday = #{birthday},
sex = #{sex},
address = #{address}
where id = #{id}
update
>
<
select
id
=
"likeQuery"
parameterType
=
"String"
resultType
=
"com.github.pojo.User"
>
select * from user where username like '%${value}%'
select
>
mapper
>
|
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
/**
* @Title MybatisFirstTest.java
* @Description 查增删改
*
* @author Charles7c
*/
public
class
MybatisFirstTest {
private
SqlSessionFactory sqlsessionFactory;
@Before
public
void
before()
throws
Exception {
// 根据流创建sqlSessionFactory
String resource =
"sqlMapConfig.xml"
;
// Resources 是mybatis工具
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlsessionFactory =
new
SqlSessionFactoryBuilder().build(inputStream);
}
// 查询测试
@Test
public
void
selectTest() {
// 工厂开启session
SqlSession openSession = sqlsessionFactory.openSession();
// 查询结果为一个所以用selectOne
User user = openSession.selectOne(
"user.findUserById"
,
10
);
System.out.println(user);
// 释放资源
openSession.close();
}
// 添加测试
@Test
public
void
addTest() {
SqlSession openSession = sqlsessionFactory.openSession();
// 创建user对象 填充对象
User user =
new
User();
user.setUsername(
"秋香"
);
user.setSex(
"1"
);
user.setBirthday(
new
Date());
user.setAddress(
"华府"
);
// rows : 影响行数
int
rows = openSession.insert(
"addUser"
, user);
// 增删改必须提交事务
openSession.commit();
// 释放资源
openSession.close();
}
// 删除测试
@Test
public
void
deleteTest() {
SqlSession openSession = sqlsessionFactory.openSession();
openSession.delete(
"user.deleteUser"
,
28
);
openSession.commit();
// 释放资源
openSession.close();
}
// 修改测试
@Test
public
void
updateTest() {
SqlSession openSession = sqlsessionFactory.openSession();
User user =
new
User();
user.setId(
27
);
user.setUsername(
"白风"
);
user.setBirthday(
new
Date());
user.setSex(
"2"
);
user.setAddress(
"苟且"
);
openSession.delete(
"user.updateUser"
, user);
// 提交
openSession.commit();
// 释放资源
openSession.close();
}
// 模糊查询测试
@Test
public
void
likeTest() {
SqlSession openSession = sqlsessionFactory.openSession();
// 查询的不清楚多少 用selectList
List
"likeQuery"
,
'明'
);
for
(User user : list) {
System.out.println(user);
}
// 释放资源
openSession.close();
}
}
|
#{}表示一个占位符号,通过#{}可以实现preparedStatement向占位符中设置值,自动进行java类型和jdbc类型转换。#{}可以有效防止sql注入。 #{}可以接收简单类型值或pojo属性值。 如果parameterType传输单个简单类型值,#{}括号中可以是value或其它名称。
${}表示拼接sql串,通过${}可以将parameterType 传入的内容拼接在sql中且不进行jdbc类型转换, ${}可以接收简单类型值或pojo属性值,如果parameterType传输单个简单类型值,${}括号中只能是value。
<
typeAliases
>
<
package
name
=
"com.github.pojo"
/>
typeAliases
>
<
environments
default
=
"development"
>
<
environment
id
=
"development"
>
<
transactionManager
type
=
"JDBC"
/>
<
dataSource
type
=
"POOLED"
>
<
property
name
=
"driver"
value
=
"com.mysql.jdbc.Driver"
/>
<
property
name
=
"url"
value
=
"jdbc:mysql://localhost:3306/mybatis45?characterEncoding=utf-8"
/>
<
property
name
=
"username"
value
=
"root"
/>
<
property
name
=
"password"
value
=
"root"
/>
dataSource
>
environment
>
environments
>
<
mappers
>
<
package
name
=
"com.github.mapper"
/>
mappers
>
configuration
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
// 2.Mapper接口的方法名和Mapper.xml中statement的id必须相同
// 3.Mapper接口的入参类型必须和Mapper.xml中定义的每个sql的parameterType相同
// 4.Mapper接口的输出参数类型必须和Mapper.xml中定义的每个sql的resultType相同
public
interface
UserMapper {
// 根据用户id查询一个用户信息
public
User findUserById(Integer id);
// 根据用户名称模糊查询用户信息列表
public
List
// 添加用户信息
public
void
addUser(User user);
}
|
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
|
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<
mapper
namespace
=
"com.github.mapper.UserMapper"
>
<
select
id
=
"findUserById"
parameterType
=
"Integer"
resultType
=
"user"
>
select * from user where id = #{id}
select
>
<
insert
id
=
"addUser"
parameterType
=
"user"
>
insert into user
(username, birthday,sex,address)
values(#{username},#{birthday},#{sex},#{address})
insert
>
<
delete
id
=
"deleteUser"
parameterType
=
"Integer"
>
delete from user where id = #{id}
delete
>
<
update
id
=
"updateUser"
parameterType
=
"user"
>
update user set
username = #{username},
birthday = #{birthday},
sex = #{sex},
address = #{address}
where id = #{id}
update
>
<
select
id
=
"likeQuery"
parameterType
=
"String"
resultType
=
"user"
>
select * from user where username like '%${value}%'
select
>
mapper
>
|
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
|
public
class
UserMapperTest {
private
SqlSessionFactory sqlSessionFactory;
@Before
public
void
init()
throws
Exception {
// 1.加载核心配置文件 在src下
String resource =
"SqlMapConfig.xml"
;
// 2.获取xml文件流
InputStream in = Resources.getResourceAsStream(resource);
// 3.通过流构造SqlSessionFactory SqlSessionFactoryBuilder将其当成一个工具
sqlSessionFactory =
new
SqlSessionFactoryBuilder().build(in);
}
@Test
public
void
testFindUserById() {
// 工厂开启session
SqlSession sqlSession = sqlSessionFactory.openSession();
// 从sqlSession中获取UserMapper接口的代理对象
UserMapper mapper = sqlSession.getMapper(UserMapper.
class
);
// 执行查询
User user = mapper.findUserById(
27
);
System.out.println(user);
// 释放资源
sqlSession.close();
}
@Test
public
void
testLikeQuery() {
// 工厂开启session
SqlSession sqlSession = sqlSessionFactory.openSession();
// 从sqlSession中获取UserMapper接口的代理对象
UserMapper mapper = sqlSession.getMapper(UserMapper.
class
);
// 执行查询
List
"明"
);
// 因为我知道数据不为空所以不进行防止空指针操作了
for
(User user : list) {
System.out.println(user);
}
// 释放资源
sqlSession.close();
}
@Test
public
void
testAddUser() {
// 工厂开启session
SqlSession sqlSession = sqlSessionFactory.openSession();
// 从sqlSession中获取UserMapper接口的代理对象
UserMapper mapper = sqlSession.getMapper(UserMapper.
class
);
User user =
new
User();
user.setUsername(
"赵云"
);
user.setAddress(
"常山"
);
// 执行添加
mapper.addUser(user);
// 执行提交事务!!!
sqlSession.commit();
// 释放资源
sqlSession.close();
}
}
|