Mybatis_05/25

本文档展示了如何在配置文件缺失的情况下,通过Maven进行项目的重编译。首先,通过读取'mybatis.xml'配置文件来创建SqlSessionFactory。然后,测试方法包括插入和删除学生数据,涉及事务的提交。代码中包含了StudentDao接口及其XML映射文件,用于数据库操作。最后,配置日志文件设置为STDOUT_LOGGING,以便输出mybatis的日志信息。

配置文件缺失

  1. 通过Maven进行重编译
    法一
  2. Rebuild Project
    法二
  3. 粘贴复制

插入和删除

TestMybatis

package com.sdut;

import com.sdut.entity.Student;
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;

public class TestMybatis {
    //测试方法
    @Test
    public void testInsert() throws IOException {

        String config = "mybatis.xml";
        InputStream in = Resources.getResourceAsStream(config);
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        SqlSession sqlSession = factory.openSession();

        String sqlId = "com.sdut.dao.StudentDao.insertStudent";
        Student  student = new Student();
        student.setId(1003);
        student.setName("王五");
        student.setEmail("110@qq.com");
        student.setAge(20);
        int result = sqlSession.insert(sqlId, student);
        System.out.println(result);

        //mybatis默认不自动提交事务,insert, delete, update后要手动提交事务
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void deleteStudentById() throws IOException {
        String config = "mybatis.xml";
        InputStream in = Resources.getResourceAsStream(config);
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        SqlSession sqlSession = factory.openSession();


        String sqlId =  "com.sdut.dao.StudentDao.deleteStudentById";
        sqlSession.delete(sqlId, 1003);

        sqlSession.commit();
        sqlSession.close();
    }
}

StudentDao

package com.sdut.dao;

import com.sdut.entity.Student;

import java.util.List;

//一个接口,操作student表
public interface StudentDao {
    //查询所有数据
    public List<Student> selectAllStudents();

    //插入一条记录
    public int insertStudent(Student student);

    //删除一条记录
    public int deleteStudentById(int id);
}

StudentDao.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.sdut.dao.StudentDao">

    <select id="selectAllStudents" resultType="com.sdut.entity.Student">
        select id, name, email, age from student order by id
    </select>
    <insert id="insertStudent">
        insert into student values(#{id}, #{name}, #{email}, #{age})
    </insert>
    <delete id="deleteStudentById">
        delete from student where id=#{id}
    </delete>
    
</mapper>

运行结果:

运行结果

配置日志文件

mybatis.xml:

<?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>
    <!-- 控制mybatis全局行为的 -->
    <settings>
        <!-- 设置mybatis输出日志 -->
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
    
    <environments default="mydev">
        <environment id="mydev">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
                <property name="username" value="root"/>
                <property name="password" value="010203"/>
            </dataSource>
        </environment>
    </environments>
    
    <mappers>
        <mapper resource="com/sdut/dao/StudentDao.xml"/>
    </mappers>
    
</configuration>

配置效果:

日志显示

2025-11-04 16:47:05.714 INFO 25365 --- [[ACTIVE] ExecuteThread: '99' for queue: 'weblogic.kernel.Default (self-tuning)'] c.o.group.apps.core.aop.ConsolePrintAop : 浏览器 : CHROME11 2025-11-04 16:47:05.714 INFO 25365 --- [[ACTIVE] ExecuteThread: '99' for queue: 'weblogic.kernel.Default (self-tuning)'] c.o.group.apps.core.aop.ConsolePrintAop : 浏览器版本 : 117.0.0.0 2025-11-04 16:47:11.740 ERROR 25365 --- [[ACTIVE] ExecuteThread: '22' for queue: 'weblogic.kernel.Default (self-tuning)'] c.o.g.a.c.c.e.Error500Controller : 捕获到错误: MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: Error selecting key or setting result to parameter object. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is weblogic.jdbc.extensions.PoolLimitSQLException: weblogic.common.resourcepool.ResourceLimitException: No resources currently available in pool JDBC-ORACLE-COA to allocate to applications, please increase the size of the pool and retry.. 2025-11-04 16:47:11.741 ERROR 25365 --- [[ACTIVE] ExecuteThread: '25' for queue: 'weblogic.kernel.Default (self-tuning)'] c.o.g.a.c.c.e.Error500Controller : 捕获到错误: MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: Error selecting key or setting result to parameter object. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is weblogic.jdbc.extensions.PoolLimitSQLException: weblogic.common.resourcepool.ResourceLimitException: No resources currently available in pool JDBC-ORACLE-COA to allocate to applications, please increase the size of the pool and retry.. 2025-11-04 16:47:16.388 ERROR 25365 --- [[ACTIVE] ExecuteThread: '56' for queue: 'weblogic.kernel.Default (self-tuning)'] c.o.group.apps.core.aop.ConsolePrintAop : 抛出异常 : nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is weblogic.jdbc.extensions.PoolLimitSQLException: weblogic.common.resourcepool.ResourceLimitException: No resources currently available in pool JDBC-ORACLE-COA to allocate to applications, please increase the size of the pool and retry.. ### The error may exist in URL [zip:/home/weblogic/weblogic/user_projects/domains/smecs/servers/AdminServer/tmp/_WL_user/coa_1.0.0/svf14e/war/WEB-INF/lib/_wl_cls_gen.jar!/mybatis/mapper/CoaLotParameterMapper.xml] ### The error may involve com.smec.apps.coa.mapper.CoaLotParameterMapper.listLotParameterForDashboardStdDev ### The error occurred while executing a query ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is weblogic.jdbc.extensions.PoolLimitSQLException: weblogic.common.resourcepool.ResourceLimitException: No resources currently available in pool JDBC-ORACLE-COA to allocate to applications, please increase the size of the pool and retry.. 2025-11-04 16:47:16.417 ERROR 25365 --- [[ACTIVE] ExecuteThread: '4' for queue: 'weblogic.kernel.Default (self-tuning)'] c.o.group.apps.core.aop.ConsolePrintAop : 抛出异常 : nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is weblogic.jdbc.extensions.PoolLimitSQLException: weblogic.common.resourcepool.ResourceLimitException: No resources currently available in pool JDBC-ORACLE-COA to allocate to applications, please increase the size of the pool and retry.. ### The error may exist in URL [zip:/home/weblogic/weblogic/user_projects/domains/smecs/servers/AdminServer/tmp/_WL_user/coa_1.0.0/svf14e/war/WEB-INF/lib/_wl_cls_gen.jar!/mybatis/mapper/CoaLotParameterMapper.xml] ### The error may involve com.smec.apps.coa.mapper.CoaLotParameterMapper.listLotParameterForDashboardStdDev ### The error occurred while executing a query ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is weblogic.jdbc.extensions.PoolLimitSQLException: weblogic.common.resourcepool.ResourceLimitException: No resources currently available in pool JDBC-ORACLE-COA to allocate to applications, please increase the size of the pool and retry.. 2025-11-04 16:47:16.427 ERROR 25365 --- [[ACTIVE] ExecuteThread: '99' for queue: 'weblogic.kernel.Default (self-tuning)'] c.o.group.apps.core.aop.ConsolePrintAop : 抛出异常 : nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is weblogic.jdbc.extensions.PoolLimitSQLException: weblogic.common.resourcepool.ResourceLimitException: No resources currently available in pool JDBC-ORACLE-COA to allocate to applications, please increase the size of the pool and retry.. ### The error may exist in URL [zip:/home/weblogic/weblogic/user_projects/domains/smecs/servers/AdminServer/tmp/_WL_user/coa_1.0.0/svf14e/war/WEB-INF/lib/_wl_cls_gen.jar!/mybatis/mapper/CoaLotParameterMapper.xml] ### The error may involve com.smec.apps.coa.mapper.CoaLotParameterMapper.listLotParameterForDashboardStdDev ### The error occurred while executing a query ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is weblogic.jdbc.extensions.PoolLimitSQLException: weblogic.common.resourcepool.ResourceLimitException: No resources currently available in pool JDBC-ORACLE-COA to allocate to applications, please increase the size of the pool and retry.. 2025-11-04 16:47:16.511 ERROR 25365 --- [[ACTIVE] ExecuteThread: '63' for queue: 'weblogic.kernel.Default (self-tuning)'] c.o.group.apps.core.aop.ConsolePrintAop : 抛出异常 : nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is weblogic.jdbc.extensions.PoolLimitSQLException: weblogic.common.resourcepool.ResourceLimitException: No resources currently available in pool JDBC-ORACLE-COA to allocate to applications, please increase the size of the pool and retry.. ### The error may exist in URL [zip:/home/weblogic/weblogic/user_projects/domains/smecs/servers/AdminServer/tmp/_WL_user/coa_1.0.0/svf14e/war/WEB-INF/lib/_wl_cls_gen.jar!/mybatis/mapper/CoaLotParameterMapper.xml] ### The error may involve com.smec.apps.coa.mapper.CoaLotParameterMapper.listLotParameterForDashboardStdDev ### The error occurred while executing a query ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is weblogic.jdbc.extensions.PoolLimitSQLException: weblogic.common.resourcepool.ResourceLimitException: No resources currently available in pool JDBC-ORACLE-COA to allocate to applications, please increase the size of the pool and retry.. 2025-11-04 16:47:16.521 ERROR 25365 --- [[ACTIVE] ExecuteThread: '36' for queue: 'weblogic.kernel.Default (self-tuning)'] c.o.group.apps.core.aop.ConsolePrintAop : 抛出异常 : nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is weblogic.jdbc.extensions.PoolLimitSQLException: weblogic.common.resourcepool.ResourceLimitException: No resources currently available in pool JDBC-ORACLE-COA to allocate to applications, please increase the size of the pool and retry.. ### The error may exist in URL [zip:/home/weblogic/weblogic/user_projects/domains/smecs/servers/AdminServer/tmp/_WL_user/coa_1.0.0/svf14e/war/WEB-INF/lib/_wl_cls_gen.jar!/mybatis/mapper/CoaLotParameterMapper.xml] ### The error may involve com.smec.apps.coa.mapper.CoaLotParameterMapper.listLotParameterForDashboardStdDev ### The error occurred while executing a query ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is weblogic.jdbc.extensions.PoolLimitSQLException: weblogic.common.resourcepool.ResourceLimitException: No resources currently available in pool JDBC-ORACLE-COA to allocate to applications, please increase the size of the pool and retry.. 2025-11-04 16:47:16.547 ERROR 25365 --- [[ACTIVE] ExecuteThread: '7' for queue: 'weblogic.kernel.Default (self-tuning)'] c.o.group.apps.core.aop.ConsolePrintAop : 抛出异常 : nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is weblogic.jdbc.extensions.PoolLimitSQLException: weblogic.common.resourcepool.ResourceLimitException: No resources currently available in pool JDBC-ORACLE-COA to allocate to applications, please increase the size of the pool and retry.. ### The error may exist in URL [zip:/home/weblogic/weblogic/user_projects/domains/smecs/servers/AdminServer/tmp/_WL_user/coa_1.0.0/svf14e/war/WEB-INF/lib/_wl_cls_gen.jar!/mybatis/mapper/CoaLotParameterMapper.xml] ### The error may involve com.smec.apps.coa.mapper.CoaLotParameterMapper.listLotParameterForDashboardStdDev 这是什么异常
11-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值