mybatis介绍


使用原因

  • 半自动

    • sql语句交由开发人员
    • 其他环境由框架自动实现
  • 比Hiberante学习成本低

    • Hiberante是全自动全映射
    • 想要实现sql语句优化需要掌握hql
  • 低耦合

    • 框架是自动实现,不用大动干戈改代码

初次查询

  • 引入的文件

    • mybatis-3.5.6.jar
    • log4j-1.2.17.jar
    • mysql-connector-java-8.0.13.jar
    • log4j.xml
  • 创建mybatis-config.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/user?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="mysql"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
  • 创建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
public class User {
int id;
String name;
int money;
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 getMoney() {
return money;
}

public void setMoney(int money) {
this.money = money;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
  • 创建UserMapper.xml
1
2
3
4
5
6
7
8
9
<?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="UserMapper">
<select id="selectUser" resultType="bean.User">
select * from user where id = #{id}
</select>
</mapper>
  • 测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void test(){
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession openSession = sqlSessionFactory.openSession();
try {
User user = openSession.selectOne("selectUser",1);
System.out.println(user);
}finally {
openSession.close();
}
}

接口式编程

  • 创建UserMapper接口
1
2
3
public interface UserMapper {
User getUser(Integer integer);
}
  • 创建对应UserMapper.xml
1
2
3
4
5
6
7
8
9
<?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="bean.UserMapper">
<select id="getUser" resultType="bean.User">
select * from user where id = #{id}
</select>
</mapper>
  • 测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void test1(){
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession openSession = sqlSessionFactory.openSession();
try {
UserMapper userMapper = openSession.getMapper(bean.UserMapper.class);
User user = userMapper.getUser(1);
System.out.println(user);
}finally {
openSession.close();
}
}