this is my first project using mybatis, so please someone help.
I've got my ClienteMapper.xml and my ClienteMapper.java at the same
folder(src.org.data) and i dont work right.
i've set the configuration.xml file namespace to "org.data", but i still got
this error.
What is wrong?
this is my *ClienteMapper.java*:
package org.data;
import java.util.List;
import org.apache.ibatis.mapping.ResultMap;
public interface ClienteMapper {
public void inserirCliente(Cliente cliente);
public void updateCliente(Cliente cliente);
public Cliente selectCliente(String cpf);
//public List<ResultMap> selectClienteDetalhado(String cpf);
public void deleteCliente(String cpf);
}
=============================================================
and this is my *ClienteMapper.xml* file:
<?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="org.data">
<insert id="inserirCliente" parameterType="Cliente">
insert into clientes (nome,cpf,telefone)
values (#{nome},#{cpf},#{telefone})
</insert>
<update id="updateCliente" parameterType="Cliente">
update clientes set
nome = #{nome},
telefone=#{telefone},
where cpf=#{cpf}
</update>
<delete id="deleteCliente" parameterType="String">
delete from clientes where cpf=#{cpf}
</delete>
<select id="selectCliente" parameterType="String"
resultType="Cliente">
select * from clientes where cpf = #{cpf}
</select>
</mapper>
===============================================
i was trying to use them at my *ClienteDAO.java*:
package org.data;
import java.io.IOException;
import java.io.Reader;
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 ClienteDAO {
private SqlSessionFactory sessionFactory;
public ClienteDAO() throws IOException {
String resource = "org/data/configuration.xml";
Reader reader = Resources.getResourceAsReader(resource);
sessionFactory = new
SqlSessionFactoryBuilder().build(reader);
sessionFactory.getConfiguration().addMapper(ClienteMapper.class);
}
public void inserirCliente(Cliente cliente){
SqlSession session =sessionFactory.openSession();
try{
ClienteMapper clienteMapper =
session.getMapper(ClienteMapper.class);
System.out.println("iniciou");
System.out.println(cliente.getNome());
clienteMapper.inserirCliente(cliente);
session.commit();
}finally{
session.close();
}
}
}
========================================================
and this is my config file(*configuration.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="config/config.properties">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost/mybatis"/>
<property name="username" value="root" />
<property name="password" value="admin"/>
</properties>
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="useGeneratedKeys" value="false"/>
<setting name="defaultExecutorType" value="SIMPLE"/>
<setting name="defaultStatementTimeout" value="25000"/>
</settings>
<typeAliases>
<typeAlias alias="Cliente" type="org.data.Cliente" />
<typeAlias alias="Endereco" type="org.data.Endereco"/>
</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="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/data/ClienteMapper.xml"/>
<mapper resource="org/data/EnderecoMapper.xml"/>
</mappers>
</configuration>
===============================================================
and, finally, this is the *error* i'm getting:
Exception in thread "main" java.lang.IllegalArgumentException: Mapped
Statements collection does not contain value for
org.data.ClienteMapper.inserirCliente
at
org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:594)
at
org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:436)
at
org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:428)
at
org.apache.ibatis.binding.MapperMethod.setupCommandType(MapperMethod.java:188)
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:51)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:37)
at $Proxy1.inserirCliente(Unknown Source)
at org.data.ClienteDAO.inserirCliente(ClienteDAO.java:56)
at Testando.main(Testando.java:57)
==============================================================
i just dont know what's happening. i thought it was everything ok with my
code.
Help me please.
Thanks and regards.
--
View this message in context: http://mybatis-user.963551.n3.nabble.com/Mapped-Statements-collection-does-not-contain-value-for-my-class-tp3333213p3333213.html
Sent from the mybatis-user mailing list archive at Nabble.com.
1) Try to change the namespace of your ClienteMapper.xml file to "org.data.ClienteMapper.xml". That will prevent name conflicts with your EnderecoMapper.xml file.
2) From your code, it seems that every DAO will have a different instance of SqlSessionFactory. While this may work, it's not what MyBatis expects and it can cause problems.
Try to create only one instance of SqlSessionFactory and use it in all your DAOs. You can use MyBatis-Guice, MyBatis-Spring to help you if you use Guice or Spring.
Christian
-----Message d'origine-----
De : mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] De la part de wass
Envoyé : September-13-11 12:59 PM
À : mybati...@googlegroups.com
Objet : Mapped Statements collection does not contain value for my class