我正在使用mybatis,这是我的配置,它正在运行:
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>
<settings>
<setting name="cacheEnabled" value="true" />
<setting name="lazyLoadingEnabled" value="true" />
<setting name="multipleResultSetsEnabled" value="true" />
<setting name="useColumnLabel" value="true" />
<setting name="useGeneratedKeys" value="false" />
<setting name="autoMappingBehavior" value="PARTIAL" />
<setting name="defaultExecutorType" value="SIMPLE" />
<setting name="defaultStatementTimeout" value="25" />
<setting name="safeRowBoundsEnabled" value="false" />
<setting name="mapUnderscoreToCamelCase" value="false" />
<setting name="localCacheScope" value="SESSION" />
<setting name="jdbcTypeForNull" value="OTHER" />
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" />
</settings>
<typeAliases>
<typeAlias type="com.beitech.cagb.secure.api.model.UserDataResult"
alias="UserData"></typeAlias>
</typeAliases>
<mappers>
<mapper resource="com/payulatam/ppp4/secure/api/interfaces/user- mapper.xml" />
<!-- <mapper class="com.beitech.cagb.secure.api.interfaces.UserMapper" /> -->
</mappers>
</configuration>
user-mapper.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.beitech.cagb.secure.api.interfaces.IUserDao">
<select id="getUserData" resultType="UserData">
select
usuario_web_id as usuario_web_id,
tipo_usuario_web as tipo_usuario_web,
email as email,
nombres as nombres,
apellidos as apellidos
from
pps.usuario_web u
where
u.usuario_web_id=#{userId}
and u.email=#{mail}
</select>
</mapper>
IUserDao:
package com.beitech.cagb.secure.api.interfaces;
import org.apache.ibatis.annotations.Param;
import com.beitech.cagb.secure.api.model.UserDataResult;
public interface IUserDao {
public UserDataResult getUserData(
@Param("mail") final String mail,
@Param("userId") final Integer userId);
}
但如果我在:
之间切换<mappers>
<!-- <mapper resource="com/payulatam/ppp4/secure/api/interfaces/user- mapper.xml" /> -->
<mapper class="com.beitech.cagb.secure.api.interfaces.UserMapper" />
使用此类代替xml文件UserMapper
:
package com.beitech.cagb.secure.api.interfaces;
import org.apache.ibatis.annotations.Select;
import com.beitech.cagb.secure.api.model.UserDataResult;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Result;
public interface UserMapper {
@Select("select usuario_web_id as usuario_web_id, tipo_usuario_web as tipo_usuario_web, email as email, nombres as nombres, apellidos as apellidos from pps.usuario_web u where u.usuario_web_id=#{userId} and u.email=#{mail}")
@Results(value = {
@Result(property="usuario_web_id",javaType=Integer.class,column="usuario_web_id"),
@Result(property="tipo_usuario_web",javaType=String.class,column="tipo_usuario_web"),
@Result(property="email",javaType=String.class,column="email"),
@Result(property="nombres",javaType=String.class,column="nombres"),
@Result(property="apellidos",javaType=String.class,column="apellidos")
})
UserDataResult getUserData(String mail, Integer userId);
}
我收到了这个错误:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.beitech.cagb.secure.api.interfaces.IUserDao.getUserData
我有这个测试类:
package com.beitech.cagb.secure.api;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.beitech.cagb.secure.api.interfaces.IUserDao;
import com.beitech.cagb.secure.api.model.UserDataResult;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/META-INF/spring/pu.xml")
public class UserDaoTest {
@Autowired
private IUserDao userDao;
@Test
public void testUserDAO(){
Assert.assertNotNull(userDao);
UserDataResult userDataResult = userDao.getUserData("ana.sandoval@pagosonline.com", 500101);
Assert.assertNotNull(userDataResult);
System.out.println("UserData: " + userDataResult.getUsuario_web_id() + " "
+ userDataResult.getEmail() + " "
+ userDataResult.getTipo_usuario_web() + " "
+ userDataResult.getNombres() + " "
+ userDataResult.getApellidos());
}
}
我想也许是因为在xml文件中我定义了这样的命名空间而不是在界面中:
<mapper namespace="com.beitech.cagb.secure.api.interfaces.IUserDao">
答案 0 :(得分:1)
问题是在测试中我应该使用mapper(Interface)的实例来使其工作:
@Autowired
private UserMapper userMapper;
@Test
public void testUserDAO(){
Assert.assertNotNull(userMapper);
UserDataResult userDataResult = userMapper.getUserData("ana.sandoval@pagosonline.com", 500101);
Assert.assertNotNull(userDataResult);
System.out.println("UserData: " + userDataResult.getUsuario_web_id() + " "
+ userDataResult.getEmail() + " "
+ userDataResult.getTipo_usuario_web() + " "
+ userDataResult.getNombres() + " "
+ userDataResult.getApellidos());
}
现在可行。