PDA

View Full Version : [Java]: Ibatis+Spring


mercury841
19-12-2007, 09:53
Ragazzi sto provando a sviluppare la mia prima applicazione in Spring e Ibatis. Quello che voglio fare è una semplice applicazione che faccia un inserimento in una tabella di un database. Adesso posto i file xml di configurazione e poi vi dico qual'è il problema.


spring-ibatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>sun.jdbc.odbc.JdbcOdbcDriver</value>
</property>
<property name="url">
<value>jdbc:odbc:Univ</value></property>
<property name="username"><value></value></property>
<property name="password"><value></value></property>
</bean>

<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>sql-map-config.xml</value>
</property>
</bean>

<bean id="studentDao"
class="ibatis.StudenteDaoImpl">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="sqlMapClient"><ref local="sqlMapClient"/></property>
</bean>

</beans>

sql-map-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE SqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">


<sql-map-config>
<sql-map resource="Studente.xml"/>
</sql-map-config>


Studente.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap>

<typeAlias type = "ibatis.entity.Studente" alias = "studente"/>

<resultMap class = "studente" id = "result">
<result property = "matricola" column = "mat"/>
<result property = "nome" column = "nome"/>
<result property = "cognome" column = "cognome"/>
<result property = "corso" column = "corso"/>
</resultMap>

<select id = "selectAllStudents" resultMap = "result">
select * from studente
</select>

<select id = "selectStudentsByMat" resultMap = "result" parameterClass = "string">
select * from studente where mat = #value#
</select>

<insert id = "insertStudent" parameterClass="studente">
insert into Jsr (mat, nome, cognome, corso) values (#matricola#, #nome#, #cognome#, #corso#)
</insert>

<delete id = "deleteStudent" parameterClass="string">
delete from studente where mat = #value#
</delete>

<update id = "updateStudent" parameterClass="studente">
update studente set nome = #nome#, cognome = #cognome#, corso = #corso#
where mat = #matricola#
</update>

</sqlMap>

Quando provo ad esguire l'applicazione ottengo questo errore nel file spring-ibatis.xml:
"java.io.FileNotFoundException: class path resource [sql-map-config.xml] cannot be opened because it does not exist".
Com'è possibile? Entrambi i file spring-ibatis.xml e sql-map-config.xml si trovano nella stessa cartella all'interno del progetto (nomeprogetto/config).

Grazie per l'aiuto. CIao

mercury841
19-12-2007, 14:17
nessuno può aiutarmi?

ciao

banryu79
19-12-2007, 15:57
Credo che dovresti postarci la parte di codice che genera quell'eccezione.

mercury841
19-12-2007, 16:20
ma l'eccezione sta nel file spring-ibatis.xml, comunque adesso posto anche l'altro codice

MainApp.java
public class MainApp
{

public static void main(String[] args)
{

Resource resource = new FileSystemResource("config/spring-ibatis.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);

StudenteDaoImpl studDao = (StudenteDaoImpl)beanFactory.getBean("studentDao");

Studente stud = new Studente();
stud.setMatricola("1111");
stud.setNome("tizio");
stud.setCognome("caio");
stud.setCorso("informatica");
studDao.insertStudent(stud);
}

}


StudenteDao.java
package ibatis;

import ibatis.entity.*;
import java.util.List;

public interface StudenteDao
{

public List<Studente> selectAllStudents();
public Studente selectStudentsByMat(String mat);

public void insertStudent(Studente student);
public void deleteStudent(String mat);
public void updateStudent(Studente newStud);

}

StudenteDaoImpl.java
package ibatis;

import ibatis.entity.Studente;

import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class StudenteDaoImpl extends SqlMapClientDaoSupport implements StudenteDao{


public void deleteStudent(String mat)
{
SqlMapClientTemplate template = getSqlMapClientTemplate();
template.delete("deleteStudent", mat);
}

public void insertStudent(Studente student)
{
SqlMapClientTemplate template = getSqlMapClientTemplate();
template.insert("insertStudent", student);
}

public List<Studente> selectAllStudents()
{
SqlMapClientTemplate template = getSqlMapClientTemplate();
return (List<Studente>)template.queryForList("selectAllStudents");
}

public Studente selectStudentsByMat(String mat)
{
SqlMapClientTemplate template = getSqlMapClientTemplate();
Object obj = template.queryForObject("selectStudentsByMat", mat);
if (obj instanceof Studente)
return (Studente)obj;
else return null;

}

public void updateStudent(Studente newStud) {
SqlMapClientTemplate template = getSqlMapClientTemplate();
template.update("updateStudent", newStud);

}

}



Studente.java
package ibatis.entity;

public class Studente {



private String matricola;
private String nome;
private String cognome;
private String corso;


public String getCognome() {
return cognome;
}

public void setCognome(String cognome) {
this.cognome = cognome;
}

public String getCorso() {
return corso;
}

public void setCorso(String corso) {
this.corso = corso;
}

public String getMatricola() {
return matricola;
}

public void setMatricola(String matricola) {
this.matricola = matricola;
}

public String getNome() {
return nome;
}

public void setNome(String nome) {
this.nome = nome;
}

@Override
public String toString() {

return "Matricola = " + matricola + ", Name = " + nome +
", Cognome = " + cognome + ", Corso = " + corso;
}




}

banryu79
19-12-2007, 16:35
ma l'eccezione sta nel file spring-ibatis.xml, comunque adesso posto anche l'altro codice

:doh:
Scusa, hai ragione, svista mia che ho letto di fretta (sono in ufficio)...

mercury841
19-12-2007, 20:30
:doh:
Scusa, hai ragione, svista mia che ho letto di fretta (sono in ufficio)...

figurati, non sai come posso risolvere?????

banryu79
20-12-2007, 09:25
figurati, non sai come posso risolvere?????

No mi spiace, non conosco sufficientemente l'ambiente, speriamo che qualcuno con le competenze adatte legga il 3d e ti aiuti.

In bocca al lupo :)

^TiGeRShArK^
20-12-2007, 14:24
ad occhio devi inserire anche il package prima di spring-ibatis.xml visto che usa il classloader per caricarlo..
prova con
nomeprogetto/config/spring-ibatis.xml
oppure
config/spring-ibatis.xml