0
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.myBatis.Dao.VillageDAO.save(VillageDAO.java:14)
at com.myBatis.service.RunMybatis.main(RunMybatis.java:17)
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in SQL Mapper Configuration
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error registering typeAlias for 'village'. Cause: java.lang.ClassNotFoundException: Cannot find class: com.myBatis.mapper.Village
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:82)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:66)
at com.myBatis.service.MyBatisUtil.<clinit>(MyBatisUtil.java:23)
... 2 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error registering typeAlias for 'village'. Cause: java.lang.ClassNotFoundException: Cannot find class: com.myBatis.mapper.Village
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:109)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:92)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
... 4 more
Caused by: org.apache.ibatis.builder.BuilderException: Error registering typeAlias for 'village'. Cause: java.lang.ClassNotFoundException: Cannot find class: com.myBatis.mapper.Village
at org.apache.ibatis.builder.xml.XMLConfigBuilder.typeAliasesElement(XMLConfigBuilder.java:130)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:99)
... 6 more
Caused by: java.lang.ClassNotFoundException: Cannot find class: com.myBatis.mapper.Village
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:190)
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:89)
at org.apache.ibatis.io.Resources.classForName(Resources.java:256)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.typeAliasesElement(XMLConfigBuilder.java:123)
... 7 more
,當我試圖使用MyBatis的configuration.When執行在搖籃項目一個簡單的CRUD操作,我用我所有的java文件在SRC/java/main與單個包它都很好。但是當我試圖在同一個src/main/java目錄下分割多個包的文件時,它會拋出java.lang.ClassNotFoundException異常。幫我出去。異常線程「main」 java.lang.ExceptionInInitializerError改變搖籃項目的文件夾結構
VillageMapper.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.myBatis.mapper.VillageMapper" >
<resultMap id="villageResult" type="village">
<id property="id" column="id" />
<result property="name" column="name"/>
<result property="district" column="district"/>
</resultMap>
<select id="selectVillage" resultType="village" parameterType="int" resultMap="villageResult">
SELECT id, name, district from village WHERE id = #{id}
</select>
<insert id="insertVillage" parameterType="village" keyProperty="id" useGeneratedKeys="true">
INSERT into village(name,district) VALUES(#{name}, #{district})
</insert>
<update id="updateVillage" parameterType="village">
UPDATE village SET name=#{name}, district =#{district} WHERE id =#{id}
</update>
<delete id="deleteVillage" parameterType="int">
DELETE FROM village WHERE id =#{id}
</delete>
</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.myBatis.mapper.Village" alias="village"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.11.122:3306/srikanth_k"/>
<property name="username" value="srikanth_k"/>
<property name="password" value="demo"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/myBatis/mapper/VillageMapper.xml" />
</mappers>
</configuration>
MyBatisUtil.java
package com.myBatis.service;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory;
static {
String resource = "mybatis-config.xml";
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
}
的build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
archivesBaseName = 'myBatis'
version = '1'
repositories {
mavenCentral()
}
dependencies {
compile 'org.mybatis:mybatis:3.2.8'
compile 'mysql:mysql-connector-java:5.1.34'
}
我的項目目錄多個包版本
myBatis
-src/main/java
-com.myBatis.api
-Village.java
-com.myBatis.Dao
-VillageDao.java
-com.myBatis.service
-MyBatisUtil.java
-RunMyBatis.java
-src/main/resources
-com.myBatis.mapper
-VillageMapper.xml
-mybatis-config.xml
我的項目目錄單封裝版本
myBatis
-src/main/java
-com.myBatis.api
-Village.java
-VillageDao.java
-MyBatisUtil.java
-RunMyBatis.java
-src/main/resources
-com.myBatis.mapper
-VillageMapper.xml
-mybatis-config.xml
就可以把'build.gradle'? – tkhm
我已附加build.gradle。請現在檢查出來。 –
謝謝。你也可以在'src/main/java'單個包版本和分割版本下附加你的目錄結構嗎?你的'build.gradle'很簡單,錯誤日誌說它找不到'com.myBatis.mapper.Village',所以目錄結構可能與這個問題有關。 – tkhm