使用mybatis-config.xml配置MyBatis,设置数据库访问路径有坑

mybatis-config.xml配置文件配置:

<!--MyBatis的全局配置文件-->
<configuration>
    <!--1.配置开发环境-->
    <environments default="test">
        <environment id="test">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <!--2.导入项目中所有的XxxMapper.xml文件
            resource属性会到类目录(classes)下
            读取相应的XxxMapper.xml文件,而EmpMapper.xml文件就在类目录下-->
    <mappers>
        <mapper resource="EmpMapper.xml" />
    </mappers>
</configuration>

EmpMapper.xml配置:

<mapper namespace="EmpMapper">
    <!-- 通过select、insert、update、delete标签声明要执行的SQL -->
    <select id="findAll" resultType="com.example.pojo.Emp">
        select * from emp
    </select>
</mapper>

pojo实体:

@Data
@ToString
public class Emp {
   
    private Integer id;
    private String name;
    private String job;
    private Double salary;
}

测试方法:

    @Test
    public void findAll() throws IOException {
   
        //1.读取mybatis的核心配置文件
        String resource="mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resource);
        //2.基于上面读取的配置信息获取一个SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //3.基于SqlSessionFactory对象获取一个SqlSession对象
        SqlSession session
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值