22
Окт
2015

Ошибка в проекте с использованием MyBatis

При компиляции класса Main.java возникает ошибка:

Exception in thread "main" java.lang.ExceptionInInitializerError
at com.sivalabs.mybatisdemo.service.BlogService.insertBlog(BlogService.java:13)
at com.sivalabs.mybatisdemo.main.Main.main(Main.java:22)
Caused by: org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in ./BlogMapper.xml
### The error occurred while processing mapper_resultMap[BlogResult]
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 65; Document root element "mapper", must match DOCTYPE root "null".
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:54)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:38)
at com.sivalabs.mybatisdemo.service.MyBatisUtil.<clinit>(MyBatisUtil.java:22)
... 2 more

Как с этим бороться, подскажите пожалуйста?


Фрагмент проекта.

class Main:

package com.sivalabs.mybatisdemo.main;

import com.sivalabs.mybatisdemo.domain.Blog;
import com.sivalabs.mybatisdemo.service.BlogService;
import java.util.List;

public class Main {

    public static void main(String[] args) {

        Blog blog = new Blog();
        blog.setBlogId(1);
        blog.setBlogName("HELL0");
        blog.setCreatedOn(null);
        BlogService blogService = new BlogService();
        blogService.insertBlog(blog);
        List<Blog> blogs = blogService.getAllBlogs();
        for (Blog bs : blogs) 
            System.out.println(bs);
   }
}

interface BlogMapper:

package com.sivalabs.mybatisdemo.mappers;

import java.util.List;
import com.sivalabs.mybatisdemo.domain.Blog;

public interface BlogMapper {
    public void insertBlog(Blog blog);    
    public List<Blog> getAllBlogs();
}

class BlogService:

package com.sivalabs.mybatisdemo.service;

import com.sivalabs.mybatisdemo.domain.Blog;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.sivalabs.mybatisdemo.mappers.BlogMapper;

public class BlogService {

    public void insertBlog(Blog blog) {
        SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
        try{
            BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
            blogMapper.insertBlog(blog);
            sqlSession.commit();        
        }
        finally{
            sqlSession.close();
        }
    }

    public List<Blog> getAllBlogs() {
        SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
        try{
            BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
            return blogMapper.getAllBlogs();
        }finally{
            sqlSession.close();
        }
    }
}

class MyBatisUtil:

package com.sivalabs.mybatisdemo.service;

import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtil {
    private static SqlSessionFactory factory;
    private MyBatisUtil() {
    }

    static {
        Reader reader = null;
        try {
            reader = Resources.getResourceAsReader("mybatis-config.xml");
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
        factory = new SqlSessionFactoryBuilder().build(reader);
    }

    public static SqlSessionFactory getSqlSessionFactory() {
        return factory;
    }
}

BlogMapper.xml:

<?xml version="1.0" encoding="UTF-8"?>
<mapper namespace="com.sivalabs.mybatisdemo.mappers.BlogMapper">
    <resultMap type="Blog" id="BlogResult">
        <id property="blogId" column="blog_id"/>
        <result property="blogName" column="BLOG_NAME"/>
        <result property="createdOn" column="CREATED_ON"/>        
    </resultMap>

    <insert id="insertBlog" parameterType="Blog"> 
        INSERT INTO blog (blog_id, BLOG_NAME, CREATED_ON)
        VALUES(#{blogId}, #{blogName}, #{createdOn})               
    </insert>

    <select id="getAllBlogs" resultMap="BlogResult">
        SELECT 
            b.blog_id, b.blog_name, b.created_on              
        FROM blog b
    </select>
</mapper>

mybatis-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>
    <typeAliases>
        <typeAlias type="com.sivalabs.mybatisdemo.domain.User" alias="User"/>
        <typeAlias type="com.sivalabs.mybatisdemo.domain.Blog" alias="Blog"/>        
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">   
                <property name="driver" value="org.postgresql.Driver"/>
                <property name="url"     value="jdbc.url=jdbc:postgresql://localhost:5432/mybatis"/>
                <property name="username" value="username"/>
                <property name="password" value="p@ssw0rd"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="./UserMapper.xml"/>
        <mapper resource="./BlogMapper.xml"/>
    </mappers>
</configuration>

Источник: https://ru.stackoverflow.com/questions/460203/%D0%9E%D1%88%D0%B8%D0%B1%D0%BA%D0%B0-%D0%B2-%D0%BF%D1%80%D0%BE%D0%B5%D0%BA%D1%82%D0%B5-%D1%81-%D0%B8%D1%81%D0%BF%D0%BE%D0%BB%D1%8C%D0%B7%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5%D0%BC-mybatis

Share

Тебе может это понравится...