idea项目中classPath路径的定义

写在前面:getResourceAsStream有两种方式

1. 第一种:Class.getClassLoader().getResourceAsStream(),类加载器默认是从classPath路径加载资源

2. 第二种:Class.getResourceAsStream()是从当前类的目录下加载

下面是idea中classPath路径的设置

项目结构的右边列出了4个项目中的文件类型:

  • Source Folders:表示的都是代码源文件目录,生成的class文件会输出到target->classess文件夹中,但是里面的源文件不会复制到target->classes文件夹中
  • Test Source Folders: 表示的都是测试代码源文件目录,生成的class文件同样会输出到target-classess文件夹中,并且里面的源文件不会复制到target->classes文件夹中
  • Resource Folders: 表示的都是资源文件目录,这些目录里面的文件会在代码编译运行被直接复制到target->classess文件夹中
  • Excluded Folders:表示的是target文件夹生成的位置,target是IDEA编译后的一些class信息存放地,里面有子目录target->classes来存储编译后的字节码。
  • target->classes即为classpath路径位置,任何我们需要在classpath:前缀中获取的资源都必须在target->classes文件夹中找到,否则将出现java.io.FileNotFoundException的错误信息。 
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
IDEA 使用 MyBatis 项目,你需要进行以下步骤: 1. 首先,你需要在你的项目添加 MyBatis 的依赖项。可以在 Maven 或 Gradle 添加: ``` <!-- Maven --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> // Gradle implementation 'org.mybatis:mybatis:3.5.6' ``` 2. 在你的项目,创建一个 MyBatis 的配置文件,例如 `mybatis-config.xml`。在这个文件配置 MyBatis 的一些基本信息,例如数据库连接信息、类型别名、mapper 配置等。 3. 在你的项目创建一个 mapper 接口,定义 SQL 语句和参数映射。例如: ```java public interface UserMapper { @Select("SELECT * FROM user WHERE id = #{id}") User getUserById(int id); } ``` 4. 在你的项目创建 mapper 接口对应的 XML 文件,例如 `UserMapper.xml`。在这个文件配置 SQL 语句和参数映射。例如: ```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"> <mapper namespace="com.example.mapper.UserMapper"> <select id="getUserById" parameterType="int" resultMap="userResultMap"> SELECT * FROM user WHERE id = #{id} </select> <resultMap id="userResultMap" type="com.example.entity.User"> <id column="id" property="id"/> <result column="username" property="username"/> <result column="password" property="password"/> <result column="email" property="email"/> <result column="phone" property="phone"/> <result column="created_time" property="createdTime"/> <result column="updated_time" property="updatedTime"/> </resultMap> </mapper> ``` 5. 在你的项目,配置 MyBatis 的扫描包路径和 mapper 配置文件路径。例如: ```xml <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath*:com/example/mapper/*.xml"/> </bean> <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.example.mapper.UserMapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> ``` 6. 最后,在你的项目调用 mapper 接口方法进行数据库操作。例如: ```java @Autowired private UserMapper userMapper; public User getUserById(int id) { return userMapper.getUserById(id); } ``` 以上就是在 IDEA 使用 MyBatis 项目的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值