idea to build the anomaly of MyBatis project: Java.io.IOException:Could not find resource Mapping/usermapper.xml

Source: Internet
Author: User
Idea to build the anomaly of MyBatis project: Java.io.IOException:Could not find resource Mapping/usermapper.xml

Switching from eclipse to idea is a bitter tear, the two compilers appear to be the mainstay of development tools in Java development, but programming this thing near misses poor, in the development process, the code is not important to bug, But it's always a bit of a bug that doesn't have a cloud and doesn't have a lot of relationship with the code.

Let me tell you something about my study of the mybatis frame when I found a wonderful strange
Like many people, in the first contact idea because of a variety of shortcut keys will not, English can not understand, so the thief with a few tired, a few annoying thieves. But familiar with some shortcuts after feeling idea is good, code hints function Thief sneak thief, make a departure is all the way to enter add line. It's a bit of a habit to use back to eclipse.

The more said the more biased, the book to the story, we said back to Mybaits:

First step: Create a new MAVEN project. If it is common to learn the framework of MyBatis to learn the function, suggest an empty MAVEN project on the line, if the Web project is
Press the following selection:

Import the appropriate jar package dependencies in Pom.xml

<dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactid >mybatis</artifactId>
            <version>3.1.1</version>
        </dependency>
        < dependency>
            <groupId>mysql</groupId>
            <artifactid>mysql-connector-java</ artifactid>
            <version>5.1.17</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12 </version>
            <scope>test</scope>
        </dependency>
    </dependencies>
Configure MyBatis configuration file
<?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= "MySQL" > <environment id= "mysql" > <tran Sactionmanager type= "JDBC" ></transactionManager> <datasource type= "Pooled" > ;p roperty name= "Driver" value= "Com.mysql.jdbc.Driver"/> <property name= "url" value= "Jdbc:mysql://loc Alhost:3306/mybatis "/> <property name=" username "value=" root "/> <property name
    = "Password" value= "123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource= "Mapping/usermapper.xml"/> </mappers> </configuration>

The environments in the above configuration is the data source, which is the necessary configuration of your JDBC connection data. The following mappers is the path of the MyBatis map, contact MyBatis should know, MyBatis is to map the XML file in the SQL statements to perform the operation of the database, this is mybatis with your project connection point.
each Pojo class corresponds to a data table in the database, and there is a corresponding mapping file. In general, if you want to learn more about the principles, you can search the Internet some of the blog, I do not have to repeat. Create a new basic user

in your project

Package Pojo;
public class User {
    private int id;
    Private String username;
    private String password;

    public int getId () {return
        ID;
    }

    public void setId (int id) {
        this.id = ID;
    }

    Public String GetUserName () {return
        username;
    }

    public void Setusername (String username) {
        this.username = username;
    }

    Public String GetPassword () {return
        password;
    }

    public void SetPassword (String password) {
        this.password = password;
    }

    @Override public
    String toString () {return
        "user{" +
                "id=" + ID +
                ", username= ' + username + ' \ ' +< c27/> ", password= '" + password + ' \ ' + '
                } ';
    }

and for its
Create the appropriate configuration file

<?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= "User" > <select id= "getuser" resulttype= "Pojo"
    . User ">
        SELECT * from users;
    </select>

</mapper>
Create a test class to test
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 java.io.IOException;
Import Java.io.InputStream;

Import java.util.List; public class Usertest {@Test public void find () {try {///load MyBatis configuration file Inputstre
            Am InputStream = Resources.getresourceasstream ("Mybatis.xml"); Build a database session project through the Sqlsession factory creator sqlsessionfactory sqlsessionfactory = new Sqlsessionfactorybuilder (). Builds (
            InputStream);
            Open a database session sqlsession sessions = Sqlsessionfactory.opensession ();
            Write the ID in the Pojo mapping file to find the appropriate SQL statement, execute the statement, and obtain the Fanhuizhi list<user> List = session.selectlist ("User.getuser");
            Prints the return value information for (User user:list) {System.out.println (user.tostring ());
}//Submit session            Session.commit ();
        Closes session session.close ();
        catch (IOException e) {e.printstacktrace (); }
    }
}

Note that the Guide jar bag is best not to guide the wrong, otherwise you have to tangle.

Everything is fine . Carriage return OK?

when you indulge in the joy of the victory, the day of the death of the bug came ...

Surprise not surprised, not unexpected.

Let's confirm the structure of the project, lest it be my old eyesight.

It turns out that 600-degree eyes are not covered.

What is the cause of the anomaly?
You may not believe it, this is actually the pot I was saying before: Ideamaven is an XML file that does not compile the Java directory of SRC, so the XML file is not found in the MyBatis configuration file.

Actually also can say is idea maven pot because if
Built is the ordinary Java project SRC under the XML file can also be read (i test) BB so much can come up with a solution is the hard truth
The solution is to add a build hint compiler to the pom file. That fool, your Java
The following are also the configuration files.

The key is how to add.

Two steps: Take the original pom file out;
2. Copy the code below to the front.

 <build> <resources> <resource> <directory>src/ma
            In/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.