0%

2.第一个Mybatis程序

思路:环境搭建–>导入Mybatis–>编写代码–>测试!

参考官方文档入门https://mybatis.org/mybatis-3/zh/getting-started.html

2.1搭建环境

1.搭建数据库

1
2
3
4
5
6
7
8
9
10
11
create table user(
id int(20) not null,
name VARCHAR(30) DEFAULT NULL,
pwd VARCHAR(30) DEFAULT NULL,
PRIMARY KEY(id)
);

INSERT INTO USER(id,name,pwd) VALUES
(1,'张三','123456'),
(2,'李四','123123'),
(3,'王五','009090')

2.新建普通Maven项目

3.删除src目录(这里是父工程)

4.导入依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!--mysql驱动-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!--Mybatis-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>

<!--junit-->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

2.2创建一个模块

  • 导入核心配置文件(mybatis-config.xml)并配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <!--核心配置文件-->
    <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/mybatis_test"/>
    <property name="username" value="root"/>
    <property name="password" value="liwenjie"/>
    </dataSource>
    </environment>
    </environments>
    </configuration>

2.3编写代码

  • 实体类

    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
    package com.lwj.pojo;

    public class User {
    private int id;
    private String name;
    private String pwd;

    public User(int id, String name, String pwd) {
    this.id = id;
    this.name = name;
    this.pwd = pwd;
    }

    public User() {
    }

    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 getPwd() {
    return pwd;
    }

    public void setPwd(String pwd) {
    this.pwd = pwd;
    }
    }
  • Dao接口

    1
    2
    3
    4
    public interface UserDao {
    User getUser();

    }
  • 接口实现类由原来的UserDaoImpl转变为一个Mapper配置文件

    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="org.mybatis.example.UserMapper">
    <select id="selectUser" resultType="pojo.User">
    select * from user where id = #{id}
    </select>
    </mapper>

2.4测试

注意点1:

1
org.apache.ibatis.binding.BindingException: Type interface com.lwj.dao.UserDao is not known to the MapperRegistry.

解决方案:

1
2
3
4
<!--每一个Mapper.xml都需要在Mybatis核心配置文件中注册!(mybatis-config.xml)-->
<mappers>
<mapper resource="mapper/UserMapper.xml"></mapper>
</mappers>

注意点2:

maven由于他的约定大于配置,我们可能会遇到我们写的配置文件,无法被导出或者生效的问题

解决方案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!--在build中配置resources,来防止资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
  • 执行测试类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    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 pojo.User;

    import java.io.InputStream;

    public class Test1 {
    @Test
    public void test() throws Exception{

    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    SqlSession session = sqlSessionFactory.openSession();
    User user = (User) session.selectOne("org.mybatis.example.UserMapper.selectUser", 1);

    System.out.println(user.getName());
    }
    }