• 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
  • 相关阅读:
    【转】:java遍历List时动态添加和删除元素
    【转】:浅析 Comparable和 Comparator的区别
    【转】:Synchronized同步静态方法和非静态方法总结
    【转】:学习路线(自用)
    js原型链的理解
    关于浏览器切换标签或者移动端切换应用时,js不执行的解决方案
    .net core连接mongoDB
    首先从项目结构开始讲起
    iOS应用内支付(IAP)服务端端校验详解
    .net支付宝SDK接入这些坑你必须知道
  • 原文地址:https://www.cnblogs.com/wt7018/p/13327904.html
Copyright © 2020-2023  润新知