gpt4 book ai didi

java - 更改Gradle Project的文件夹结构时,线程 “main” java.lang.ExceptionInInitializerError中的异常

转载 作者:行者123 更新时间:2023-12-03 05:59:24 25 4
gpt4 key购买 nike

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

I tried to perform a simple CRUD operations in Gradle project using myBatis configuration.When i used all my java files at src/java/main with single package it is all good. but when i tried to split up the files with multiple packages inside the same src/main/java dir it throws the java.lang.ClassNotFoundException exception. help me out guys.



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

最佳答案

问题出在 mybatis-config.xml

<typeAlias type="com.myBatis.mapper.Village" alias="village"/>

代替
<typeAlias type="com.myBatis.api.Village" alias="village"/>

我在 VillageDAO.java 的所有CRUD方法中都引用了 Village.java ,相反,我应该将其映射到 VillageMapper.xml

VillageDAO.java
package com.myBatis.Dao;


import org.apache.ibatis.session.SqlSession;

import com.myBatis.api.Village;
import com.myBatis.service.MyBatisUtil;


public class VillageDAO {

public void save(Village village) {

SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession();
session.insert("com.myBatis.mapper.VillageMapper.insertVillage", village);
session.commit();
session.close();
}

public void update(Village village) {

SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession();
session.update("com.myBatis.mapper.VillageMapper.updateVillage", village);
session.commit();
session.close();
}

public void delete(Integer id) {

SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession();
session.delete("com.myBatis.mapper.VillageMapper.deleteVillage", id);
session.commit();
session.close();
}

public Village getData(Integer id) {

SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession();
Village village = session.selectOne("com.myBatis.mapper.VillageMapper.selectVillage", id);
session.close();
return village;
}
}

关于java - 更改Gradle Project的文件夹结构时,线程 “main” java.lang.ExceptionInInitializerError中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42666726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com