### The error may exist in SQL Mapper Configuration

mtw512123 2017-11-09 04:14:33
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.serv.test.<clinit>(test.java:6)
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 'userModel'. Cause: java.lang.ClassNotFoundException: Cannot find class: com/model/userModel
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:52)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:36)
at com.util.userUtil.<clinit>(userUtil.java:21)
... 1 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error registering typeAlias for 'userModel'. Cause: java.lang.ClassNotFoundException: Cannot find class: com/model/userModel
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:121)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:99)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:50)
... 3 more
Caused by: org.apache.ibatis.builder.BuilderException: Error registering typeAlias for 'userModel'. Cause: java.lang.ClassNotFoundException: Cannot find class: com/model/userModel
at org.apache.ibatis.builder.xml.XMLConfigBuilder.typeAliasesElement(XMLConfigBuilder.java:171)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:109)
... 5 more
Caused by: java.lang.ClassNotFoundException: Cannot find class: com/model/userModel
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:200)
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:89)
at org.apache.ibatis.io.Resources.classForName(Resources.java:261)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.typeAliasesElement(XMLConfigBuilder.java:164)
... 6 more

结构如下

userModel.java
package com.model;

public class userModel {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

private String name;
private String password;

public String toString(){
return "name:"+name+",password="+password;
}
}

userOperation.java
package com.impl;

import java.util.Map;

public interface userOperation {
public int user_insert(Map map);
}

test.java
package com.serv;

import com.util.userUtil;

public class test {
static userUtil userUtil=new userUtil();
public static void main(String[] args) {
// TODO Auto-generated method stub
userUtil.user_insert("add", "dbbb");
}

}

userUtil.java
package com.util;

import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;

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 com.impl.userOperation;

public class userUtil {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
static{
try {
reader=Resources.getResourceAsReader("com/conf/dbconf.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public void user_insert(String name,String password) {
SqlSession sqlSession=sqlSessionFactory.openSession();
int query_msg=0;
Map map=new HashMap();
map.put("name", name);
map.put("password", password);
try {
userOperation userOperation=sqlSession.getMapper(userOperation.class);
query_msg=userOperation.user_insert(map);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
sqlSession.commit();
sqlSession.close();
}
System.out.println("user_insert:"+query_msg);
}
}

dbconf.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="com/conf/config.properties"></properties>
<typeAliases>
<typeAlias alias="userModel" type="com/model/userModel" />
</typeAliases>

<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${name}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/conf/userdb.xml" />
</mappers>
</configuration>

config.properties
driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/mybatis_new_test
name=root
password=root

userdb.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.impl.userOperation">
<insert id="user_insert" parameterType="Map">
insert into user(name,password) values(#{name},#{password})
</insert>
</mapper>
...全文
872 6 点赞 打赏 收藏 举报
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
mtw512123 2017-11-09
<typeAlias alias="userModel" type="com/model/userModel" /> 嗯,错了,弄混了
  • 打赏
  • 举报
回复
mtw512123 2017-11-09
只定义一个user_insert 方法,除了在dbconf.xml文件中有给userModel的路径取别名外,其他的文件都与userModel无关联
  • 打赏
  • 举报
回复
<typeAlias alias="userModel" type="com.model.userModel" /> 不是都是这样写的吗 <typeAlias alias="userModel" type="com/model/userModel" /> 这样子也可以吗
  • 打赏
  • 举报
回复
mtw512123 2017-11-09
使用断点检测 在 userUtil 中的 21行 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); 出现问题
  • 打赏
  • 举报
回复
ostools 2017-11-09
Error registering typeAlias for 'userModel'. Cause: java.lang.ClassNotFoundException: Cannot find class: com/model/userModel 清理下项目看看,跟userModel相关的文件都去看看
  • 打赏
  • 举报
回复
mtw512123 2017-11-09
用myeclipse创建的java项目,mybatis的配置文件检查了好几遍,没找到哪问题,求帮助
  • 打赏
  • 举报
回复
相关推荐
发帖
Java EE
加入

6.7w+

社区成员

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
申请成为版主
帖子事件
创建了帖子
2017-11-09 04:14
社区公告
暂无公告