sqlSessionFactory初始化报错

sqlSessionFactory初始化报错

登录时报错

严重: Servlet.service() for servlet [LoginServlet] in context with path [] threw exception [Servlet execution threw an exception] with root cause

java.lang.StringIndexOutOfBoundsException: String index out of range: 24

at java.lang.String.charAt(String.java:658)

at org.apache.ibatis.builder.ParameterExpression.trimmedStr(ParameterExpression.java:131)

at org.apache.ibatis.builder.ParameterExpression.option(ParameterExpression.java:124)

at org.apache.ibatis.builder.ParameterExpression.jdbcTypeOpt(ParameterExpression.java:99)

at org.apache.ibatis.builder.ParameterExpression.property(ParameterExpression.java:70)

at org.apache.ibatis.builder.ParameterExpression.parse(ParameterExpression.java:47)

at org.apache.ibatis.builder.ParameterExpression.<init>(ParameterExpression.java:39)

。。。

at com.imooc.canvas.servlet.MyBatisUtils.<clinit>(MyBatisUtils.java:20)

at com.imooc.canvas.service.UserService.login(UserService.java:22)

at com.imooc.canvas.servlet.LoginServlet.service(LoginServlet.java:25)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)

MyBatisUtils类为:

package com.imooc.canvas.servlet;

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 java.io.IOException;
import java.io.Reader;

public class MyBatisUtils {

    private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader;

    static {
        try {
            String resource = "config.xml";
            reader = Resources.getResourceAsReader(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession openSession(){
        return sqlSessionFactory.openSession();
    }
}

config.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>
    <properties resource="jdbc.properties"/>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <package name="com.imooc.canvas.mapper"/>
    </mappers>
</configuration>

jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/canvas?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true
username=root
password=123465

之前没有这种情况的

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
提问者 慕粉4057600 2018-06-16 14:57:37

原来是mapper里面的语句写错了

提问者 慕粉4057600 2018-06-16 00:35:58

后来我试着绕过登录依然报错

Servlet.service() for servlet [CanvasServlet] in context with path [] threw exception [Servlet execution threw an exception] with root cause

java.lang.NoClassDefFoundError: Could not initialize class com.imooc.canvas.servlet.MyBatisUtils

at com.imooc.canvas.service.CanvasService.getCanvas(CanvasService.java:14)

at com.imooc.canvas.servlet.CanvasServlet.service(CanvasServlet.java:41)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)

CanvasService类

package com.imooc.canvas.service;

import com.imooc.canvas.entity.Canvas;
import com.imooc.canvas.mapper.CanvasMapper;
import com.imooc.canvas.servlet.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

public class CanvasService {

    public List<Canvas> getCanvas(Integer page, Integer size){
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            CanvasMapper mapper = sqlSession.getMapper(CanvasMapper.class);
            return mapper.getCanvas((page-1)*size,size);
        }finally {
            sqlSession.close();
        }
    }
    public List<Canvas> getCanvasByCategoryId(Long categoryId, Integer page, Integer size){
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            CanvasMapper mapper = sqlSession.getMapper(CanvasMapper.class);
            return mapper.getCanvasByCategoryId(categoryId,(page-1)*size,size);
        }finally {
            sqlSession.close();
        }
    }

    public void addCanvas(Canvas canvas){
        Date now = new Date();
        canvas.setCreateTime(now);
        canvas.setUpdateTime(now);
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            CanvasMapper mapper = sqlSession.getMapper(CanvasMapper.class);
            mapper.addCanvas(canvas);
            sqlSession.commit();
        }finally {
            sqlSession.close();
        }
    }

    public void delete(Long id){
        SqlSession sqlSession = MyBatisUtils.openSession();
        try {
            CanvasMapper mapper = sqlSession.getMapper(CanvasMapper.class);
            mapper.delCanvas(id);
            sqlSession.commit();
        }finally {
            sqlSession.close();
        }
    }

    public int countCanvasByCategoryId(Long categoryId){
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            CanvasMapper mapper = sqlSession.getMapper(CanvasMapper.class);
            return mapper.countCakesByCategoryId(categoryId);
        }finally {
            sqlSession.close();
        }
    }

    public Canvas getCanvasImg(Long id){
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            CanvasMapper mapper = sqlSession.getMapper(CanvasMapper.class);
            return mapper.getImg(id);
        }finally {
            sqlSession.close();
        }
    }

    public Canvas getCanvasById(Long id){
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            CanvasMapper mapper = sqlSession.getMapper(CanvasMapper.class);
            return mapper.getCanvasById(id);
        }finally {
            sqlSession.close();
        }
    }

    public void updateCanvas(Canvas canvas){
        SqlSession sqlSession = MyBatisUtils.openSession();
        Date now = new Date();
        canvas.setUpdateTime(now);
        try{
            CanvasMapper mapper = sqlSession.getMapper(CanvasMapper.class);
            mapper.updateCanvas(canvas);
            sqlSession.commit();
        }finally {
            sqlSession.close();
        }
    }
}

所以我想是不是

reader = Resources.getResourceAsReader(resource);

sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);

这个环节出问题了?望老师解答

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
Java数据库开发与实战应用2018版
  • 参与学习           人
  • 提交作业       277    份
  • 解答问题       4297    个

Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师