mybatis 流程

一、前提

1、创建maven项目

2、添加依赖

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
    </dependencies>

3、创建module(删除原来的src)

4、在module中创建配置文件

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>

    <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?characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="@WSX3edc"/>
            </dataSource>
        </environment>
    </environments>

<!--    <mappers>-->
<!--        <mapper resource="org/mybatis/example/BlogMapper.xml"/>-->
<!--    </mappers>-->
</configuration>

 二、编写mybatis类

1、构建 SqlSessionFactory

2、从 SqlSessionFactory 中获取 SqlSession

三、编写代码

1、实体类

2、Dao类(接口)

package com.wt.dao;

import com.wt.pojo.User;

import java.util.List;

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

3、新建xml文件

<?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与UserDao产生交集-->
<mapper namespace="com.wt.dao.UserDao">
<!--
id 对应UserDao接口的方法名字
resultType 表的实现类
-->
    <select id="getUserList" resultType="com.wt.pojo.User">
        select * from user
    </select>
</mapper>

sqlSession调用xml文件

四、测试

代码

package com.wt.dao;

import com.wt.pojo.User;
import com.wt.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class UserDaoTest {
    @Test
    public void test(){
        SqlSession sqlSession = MyBatisUtil.getSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        List<User> userList = userDao.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }
        // 关闭sqlSession
        sqlSession.close();
  }
}

Type interface com.wt.dao.UserDao is not known to the MapperRegistry

mybatis-config.xml

每个mapper的配置文件都需要在核心配置文件中配置
   <mappers>
        <mapper resource="com/wt/dao/UserMapper.xml"/>
    </mappers>

 2、没找到路径文件

maven的文件添加

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

 

3、WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).

在src/main/resourecs目录下添加

log4j.properties 文件

# Global logging configuration 开发时候建议使用 debug
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 

posted @ 2020-07-17 09:05  市丸银  阅读(129)  评论(0编辑  收藏  举报