mybatis使用入门

201 阅读1分钟

一、基本配置

1、maven依赖导入

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.4</version>
        </dependency>

2、mybatis-config.xml配置

<?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>

    <typeAliases>
        <package name="com.wzh.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/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <package name="com.wzh.dao"/>
    </mappers>
</configuration>

二、使用

1、编写工具类

public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static SqlSession getSqlSession() {
        return sqlSessionFactory.openSession();
    }
}

2、编写代码

2.1实体类

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

    public User(int id) {
        this.id = id;
    }

    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;
    }
    
    public String toString() {
        return ...
    }
}

2.2Dao\Mapper接口

public interface UserMapper {
    List<User> getUserList();
}

2.3接口类实现口 由原来的UserDaoImpl转换成Mapper配置文件

<?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.wzh.dao.UserDao">
    <select id="getUserList" resultType="com.wzh.pojo.User">
      select * from user
    </select>
</mapper>

2.4、测试

注意点!

报错:org.apache.ibatis.binding.BindingException: Type interface com.wzh.dao.UserMapper is not known to the MapperRegistry.

原因:每一个Mapper.xml都需要在mybatis核心配置文件中注册

<!--放在mybatis-config.xml配置文件中-->
<mappers>
    <package name="com.wzh.dao"/>
</mappers>

报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.wzh.dao.UserMapper.getUserList

原因:maven约定大于配置,资源过滤问题

<!--放在pom.xml配置文件中-->
<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>

三、万能Map

1、产生原因

使用上述方法进行数据库插入或者更新的时候,需要给出数据库包含的所有属性,因此我们使用hashmap来进行模糊化操作。

2、使用方法

利用map<String, Object>进行传参,SQL语句直接调用map键名。

四、模糊查询

  1. java代码执行的时候传递通配符% %
  2. 在sql拼接中使用通配符