zoukankan      html  css  js  c++  java
  • mybatis不能find mapper.xml以及全部代码。查询表中所有数据。

    这个问题困扰了我很久。

    这次总算是搞定了mybatis的配置。

    SqlSession session = sqlSessionFactory.openSession();
    这个代码一直报错,最后查到是mybitis.xml的配置文件mapper写错了。用/斜杠隔开是对的。
    <mappers>
    <mapper resource="com/dao/MyBookMapper.xml"/>
    </mappers>

    之前是

    <mappers>
    <mapper resource="com.dao.MyBookMapper.xml"/>
    </mappers>
    这个是错误的。

    第二个困扰pom中必须加一下代码,运行程序会自动把
    resources
    properties
    .xml文件自动添加入class的打包文件中。

    <!--导入resource防止资源导出失败-->
    <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.实体类
    package com.books;

    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;

    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    public class Books {
    private int BOOKID;
    private String BOOKNAME;
    private int BOOKCOUTS;
    private String DETAIL;
    2.接口就是相当于是以前的DAO
    package com.dao;

    import com.books.Books;

    import java.util.List;

    public interface MyBookMapper {

    List<Books> selectBooks();
    }
    3.接口映射文件
    <?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.dao.MyBookMapper">


    <select id="selectBooks" resultType="com.books.Books">
    select * from BOOKS
    </select>

    </mapper>
    4.mybatis的主配置文件
    <?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="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
    <property name="username" value="s"/>
    <property name="password" value="0"/>
    </dataSource>
    </environment>
    </environments>
    <mappers>
    <mapper resource="com/dao/MyBookMapper.xml"/>
    </mappers>
    </configuration>
    5.工具类主要是减少重复代码。
    package com.utils;

    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 Utils {


    private static SqlSessionFactory sqlSessionFactory ;

    static {
    String in = "mybatis.xml";
    try {
    InputStream inputStream = Resources.getResourceAsStream(in);
    System.out.println("获取到了xml");
    System.out.println("sqlSessionFactory的值是:"+sqlSessionFactory);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);



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

    public static SqlSession getSession(){


    System.out.println("sqlSessionFactory:"+ sqlSessionFactory);
    SqlSession session = sqlSessionFactory.openSession();
    System.out.println("已经获取到了SqlSessionFactory");
    return session;
    }
    }
    6.测试类
    import com.books.Books;
    import com.dao.MyBookMapper;
    import com.utils.Utils;
    import org.apache.ibatis.session.SqlSession;
    import org.junit.Test;

    import java.util.List;


    public class MybatisTest {

    @Test
    public void test(){

    SqlSession session = Utils.getSession();
    MyBookMapper mapper = session.getMapper(MyBookMapper.class);
    List<Books> list = mapper.selectBooks();
    for(Books books:list){
    System.out.println(books);
    }


    }
    }
    7.结果

    已经获取到了SqlSessionFactory
    Books(BOOKID=1, BOOKNAME=dddd, BOOKCOUTS=0, DETAIL=discribe)
    Books(BOOKID=2, BOOKNAME=eeee, BOOKCOUTS=0, DETAIL=discribe)
    Books(BOOKID=3, BOOKNAME=java, BOOKCOUTS=0, DETAIL=从入门到放弃)
    Books(BOOKID=4, BOOKNAME=Oracle, BOOKCOUTS=0, DETAIL=从删库到跑路)
    Books(BOOKID=5, BOOKNAME=Linux, BOOKCOUTS=0, DETAIL=从进门到进牢)

    下面是数据库结构和pom。

    BOOKID Integer,

    ,BOOKNAME varchar ,

    BOOKCOUNT Integer ,

    DETAIL varchar.

    pom

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
    <scope>test</scope>
    </dependency>

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

    <dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>12.1.0.1-atlassian-hosted</version>
    </dependency>

    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.16</version>
    </dependency>

    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.12</version>
    </dependency>
    </dependencies>

    <!--导入resource防止资源导出失败-->
    <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>
    </project>


  • 相关阅读:
    《Entity Framework 6 Recipes》中文翻译系列 (34) ------ 第六章 继承与建模高级应用之多条件与QueryView
    《Entity Framework 6 Recipes》中文翻译系列 (33) ------ 第六章 继承与建模高级应用之TPH与TPT (2)
    《Entity Framework 6 Recipes》中文翻译系列 (32) ------ 第六章 继承与建模高级应用之TPH与TPT (1)
    《Entity Framework 6 Recipes》中文翻译系列 (31) ------ 第六章 继承与建模高级应用之自引用关联
    《Entity Framework 6 Recipes》中文翻译系列 (30) ------ 第六章 继承与建模高级应用之多对多关联
    《Entity Framework 6 Recipes》中文翻译系列 (29) ------ 第五章 加载实体和导航属性之过滤预先加载的实体集合和修改外键关联
    《Entity Framework 6 Recipes》中文翻译系列 (28) ------ 第五章 加载实体和导航属性之测试实体是否加载与显式加载关联实体
    《Entity Framework 6 Recipes》中文翻译系列 (27) ------ 第五章 加载实体和导航属性之关联实体过滤、排序、执行聚合操作
    《Entity Framework 6 Recipes》中文翻译系列 (26) ------ 第五章 加载实体和导航属性之延缓加载关联实体和在别的LINQ查询操作中使用Include()方法
    《Entity Framework 6 Recipes》中文翻译系列 (25) ------ 第五章 加载实体和导航属性之加载完整的对象图和派生类型上的导航属性
  • 原文地址:https://www.cnblogs.com/bellwether/p/14544501.html
Copyright © 2011-2022 走看看