工程结构
在IDEA中新建一个Java项目。并加入jar包,工程结构如图。
- 在config文件夹中创建出所需要的配置文件
- log4j.properties
1 | # Global logging configuration |
SqlMapConfig.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<?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>
<!-- 和spring整合后 environments配置将废除 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理 -->
<transactionManager type="JDBC" />
<!-- 数据库连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<!--加载你书写的xml文件-->
<mappers>
<mapper resource="sqlmapper/User.xml"></mapper>
</mappers>
</configuration>User.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<?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 命名空间,作用就是对sql进行分类化管理,理解为sql隔离
注意:使用mapper代理方法开发,namespace有特殊重要的作用
-->
<mapper namespace="test">
<!--
根据id查询单条数据
id方法名,与dao类的方法名相同
parameterType输入参数的类型
resultType指定返回类型
#{}接受参数占位符
-->
<select id="findUserById" parameterType="int" resultType="Pojo.User">
SELECT *
FROM user
WHERE id = #{id}
</select>
<!--
根据用户名模糊查询多条数据
resultType返回的是单条数据的类型
${}:表示拼接sql串,将接收到的参数不加任何修饰的拼接到sql中(可能会引起SQL注入)
-->
<select id="findUserByName" parameterType="String" resultType="Pojo.User">
SELECT *
FROM user
WHERE name LIKE '%${value}%'
</select>
</mapper>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
40package Pojo;
public class User {
private int id;
private String name;
private String password;
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
编写测试类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
55import Pojo.User;
import org.junit.Test;
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 org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;import java.io.InputStream;
/**
* Created by huzhibin on 2018/4/19.
*/
public class Test_1 {
private SqlSessionFactory sqlSessionFactory;
public void beforeTest() throws Exception{
//mybatis配置文件
String resource = "sqlMapConfig.xml";
//得到配置文件流
InputStream inputStream = Resources.getResourceAsStream(resource);
//创建会话工厂
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
public void findUserByIdTest() throws IOException {
//通过工厂得到Session
SqlSession sqlSession = sqlSessionFactory.openSession();
//操作数据库
User user = sqlSession.selectOne("test.findUserById",1);
System.out.println(user);
//释放资源
sqlSession.close();
}
public void findUserByNameTest() throws IOException {
//通过工厂得到Session
SqlSession sqlSession = sqlSessionFactory.openSession();
//操作数据库
List<User> list = sqlSession.selectList("test.findUserByName","ho");
System.out.println(list);
//释放资源
sqlSession.close();
}
}
注意测试返回集合类型的时候使用的是selectList,如果使用selectOne会报错:查询有多个结果