Solve the problem that IntelliJ IDEA cannot read the configuration file.
Recently, in Mybatis, follow the video to create a configuration file in a package of the project and read the configuration file. However, an exception is always prompted.
The official code is used to read the configuration file:
String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);SqlSession session = sqlSessionFactory.openSession();
After thinking over and over again, the video uses eclipse as the ide, And the configuration file (. propoties and. after compilation, it is automatically copied to the compiled folder, that is, the classes directory. Therefore, the code can be loaded to the configuration file through a path.
Now we use IntelliJ IDEA. The idea file method for these configurations is obviously different from eclipse. There is a concept of Content Roots in idea. You need to configure the corresponding Content Roots for each folder. Content Roots includes resources, sources, and tests.
:
- If the java folder is marked as Sources, all the subfolders below are Sources, and the. class file is generated after compilation.
- The Resources folder is marked as Resources. The configuration files in this folder are automatically copied to the compilation folder during compilation.
For idea, there are the following solutions.
- Put the configuration file in the Resources folder and add the virtual path of the configuration file to the code. Select a file and right-click Copy Reference to obtain the virtual path of the file.
- Place the configuration file in the com. zjut. ssm. config package and modify the Content Roots in the config folder to Resources. After compilation, the code can also be identified.
The above two solutions apply to the make project method to compile the project.
For maven projects. You can also configure pom. xml.
<build> <finalName>springmvc-study</finalName> <resources> <resource> <directory>${basedir}/src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> <resource> <directory>${basedir}/src/main/resources</directory> </resource> </resources> </build>
In this way, you can use the maven compilation command to compile the project without setting idea, and copy the configuration file to the compiled folder.
Use IntelliJ IDEA 13 to build an integrated Android Development Environment graphic tutorial
IntelliJ IDEA 12 create a Web project graphic tutorial
Graphic tutorial for Android program development using IntelliJ IDEA
IntelliJ IDEA 12 haXe NME application Configuration Guide
IntelliJ IDEA test mode for running the Play Framework
Install IntelliJ IDEA 12 on Ubuntu 13.04
IntelliJ IDEA 12 create a Java Web project managed by Maven (illustration)
IntelliJ IDEA common shortcut keys and tips
IntelliJ IDEA details: click here
IntelliJ IDEA: click here
This article permanently updates the link address: