关于一个mybatis的,通过SqlsessionFactory打开一个数据库会话老是报错
package com.zoey.utils; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class DBAccess { public SqlSession getSqlSession() throws IOException{ //通过配置文件获取数据库连接信息 String resource = "mybatis.cfg.xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(resource); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //通过配置信息构建一个SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //通过SqlsessionFactory打开一个数据库会话 SqlSession sqlSession = sqlSessionFactory.openSession(); return sqlSession; } }
这个是工具类
下面这个是调用的
public void addUser(User u) throws Exception { DBAccess dbAccess = new DBAccess(); SqlSession sqlSession = null; sqlSession = dbAccess.getSqlSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); userMapper.addUser(u); sqlSession.commit(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); sqlSession.rollback(); } sqlSession.close(); }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zoey.model.UserMapper"> <insert id="addUser"> insert into user(username,password,name,tel,email) values (#{username},#{password},#{name},#{tel},#{email}) </insert> <select id="find" parameterType="String" resultType="User"> select * from user where username=#{username} and password=#{password} </select> </mapper>
报错信息如下:
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in com/zoey/mapper/UserMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Wrong namespace. Expected 'com.zoey.mapper.UserMapper' but found 'com.zoey.model.UserMapper'.
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.zoey.utils.DBAccess.getSqlSession(DBAccess.java:23)
at com.zoey.dao.UserDao.addUser(UserDao.java:17)