java MyBatis

什么是MyBatis

  1. MyBatis 持久层:简化工作量、灵活
  2. Spring 粘合剂:整合框架、AOP、IOC、DI
  3. SpringMVC 表現层:方便前后端数据的传输

MyBatis 是对 jdbc 的封装,将 SQL 语句放在映射文件中(XML),可以自动将输入参数映射到 SQL 语句的动态参数上,自动将 SQL 语句映射成 Java 对象。

入门示例

步骤:

  1. 创建项目(meaven项目)

  2. 导入响应的 jar 包,pom.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
    <!-- 让IDEA不要忽略src目录下的xml文件 -->
    <build>
    <resources>
    <resource>
    <directory>src/main/java</directory>
    <includes>
    <include>**/*.xml</include>
    </includes>
    </resource>
    </resources>
    </build>

    <dependencies>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.38</version>
    </dependency>

    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.0</version>
    </dependency>

    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
    </dependency>

    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    </dependency>
    </dependencies>
  3. 加入配置文件 db.properties, log4j.properties, mybatis 核心配置文件 mybatis-config.xml,MmalWapper.xml

db.properties

1
2
3
4
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test_mybatis
jdbc.username=root
jdbc.password=123456

log4j.properties

1
2
3
4
5
6
7
log4j.rootLogger=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n

log4j.category.org.springframework.beans.factory=DEBUG

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
31
32
33
<?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>
<!-- 引入数据库链接配置 -->
<properties resource="db.properties"></properties>

<!-- 定义别名 -->
<typeAliases>
<!--<package name="com.test.mybatis.pojo"></package>-->
<typeAlias type="com.test.mybatis.pojo.User" alias="User"></typeAlias>
</typeAliases>

<!-- 定义数据源 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>

<!-- 定义映射文件 -->
<mappers>
<!--<mapper resource="com/test/mybatis/mapper/MmalWapper.xml"></mapper>-->
<package name="com.test.mybatis.mapper"></package>
</mappers>
</configuration>

MmalWapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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="com.test.mybatis.mapper.MmalWapper">
<!-- statement -->
<select id="selectUser" parameterType="_int" resultType="User">
select * from mmall_user where id = #{id}
</select>
<!--<insert id=""></insert>-->
<!--<delete id=""></delete>-->
<!--<update id=""></update>-->
</mapper>
  1. 编写接口

MmalWapper.class

1
2
3
4
5
6
7
package com.test.mybatis.mapper;

import com.test.mybatis.pojo.User;

public interface MmalWapper {
User selectUser(int id);
}
  1. pojo
1
2
3
4
5
6
7
8
9
10
11
12
13
public class User {
private int id;
private String username;
private String password;
private String email;
private String phone;
private String question;
private String answer;
private int role;
private String createTime;
private String updateTime;
// 省略 get set 和 toString
}
  1. 创建工具类

MybatisUtil.clss

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 com.test.mybatis.util;

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 java.io.IOException;
import java.io.InputStream;


public class MybatisUtil {
private static SqlSessionFactory sqlSessionFactory = null;

static {
try {
// 从 XML 中构建 SqlSessionFactory
InputStream in = Resources.getResourceAsStream("com/test/mybatis/mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);

} catch (IOException e) {
e.printStackTrace();
}
}

private MybatisUtil(){}

public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}

}
  1. 创建测试用例

MmalWapperTest.class

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
package com.test.mybatis.mapper;

import com.test.mybatis.pojo.User;
import com.test.mybatis.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class MmalWapperTest {


@Test
public void testSelectUserNoInterface() {
SqlSession session = MybatisUtil.getSqlSession();

User user = (User) session.selectOne("com.test.mybatis.mapper.MmalWapper.selectUser",1);

session.close();

System.out.println(user);
}

@Test
public void testSelectUser() {
SqlSession session = MybatisUtil.getSqlSession();

MmalWapper mmalWapper = session.getMapper(MmalWapper.class);

User user = mmalWapper.selectUser(1);

session.close();

System.out.println(user);
}
}

mybatics菜单

mybatis官方文档