Child pages
  • Wicket, Guice and Ibatis example
Skip to end of metadata
Go to start of metadata

http://thread.gmane.org/gmane.comp.web.click.user/1034
http://article.gmane.org/gmane.comp.web.click.user/1034
Thanks J.F. Zarama.

Replace Spring with Guice.

MyModule.java

import static com.google.inject.name.Names.named;
import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
import com.ibatis.sqlmap.client.SqlMapClient;

public class MyModule extends AbstractModule {

	@Override
	protected void configure() {
		bind(MyDao.class).to(MyDaoImpl.class).in(Scopes.SINGLETON);
		bind(SqlMapClient.class).toProvider(SqlMapClientP.class).in(
				Scopes.SINGLETON);
		bindConstant().annotatedWith(named("ibatis.configXml"))
				.to("myapp/xml/SqlMapConfig.xml"); //from classpath
	}

}

SqlMapClientP.java

import java.io.IOException;
import java.io.Reader;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.name.Named;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class SqlMapClientP implements Provider<SqlMapClient> {
	   
    private static SqlMapClient sqlMapClient;
    @Inject @Named("ibatis.configXml") String configXml;
   
    public SqlMapClient get() {
        if(sqlMapClient != null){
        	return sqlMapClient;
        }
        try {
            Reader reader = Resources.getResourceAsReader(configXml);
            sqlMapClient  = SqlMapClientBuilder.buildSqlMapClient(reader);
            reader.close();
        }catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("SqlMapClientP unable to create instance." + e);
        }
        return sqlMapClient;
    }

}

MyDao.java

public interface MyDao {
	int getSomething() throws SQLException;
}

MyDaoImpl.java

import java.sql.SQLException;
import com.google.inject.Inject;

public class MyDaoImpl implements MyDao {

	@Inject
	private SqlMapClientP client;

	public int getSomething() throws SQLException {
		return ((Integer)client.get().queryForObject("getSomething")).intValue();
	}
}

MyPage.java

import com.google.inject.Inject;

public class MyPage extends WebPage {
	@Inject
	protected MyDao myDao;

	public MyPage() {
		super();
		System.out.println("something: "+myDao.getSomething());
	}
}

MyApp.java

public class MyApp extends WebApplication {
	...
	@Override
	public void init() {
		...
		addComponentInstantiationListener(new GuiceComponentInjector(this, new MyModule()));
		...
	}
	...
}

SqlMapConfig.xml

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

  <typeAlias alias="My" type="myapp.My"/>

  <transactionManager type="JDBC" commitRequired="false">
    <dataSource type="SIMPLE">
      <property name="JDBC.Driver" value="${driver}"/>
      <property name="JDBC.ConnectionURL" value="${url}"/>
      <property name="JDBC.Username" value="${username}"/>
      <property name="JDBC.Password" value="${password}"/>
    </dataSource>
  </transactionManager>

  <sqlMap resource="myapp/xml/My.xml"/>
</sqlMapConfig>

That should basically be it.

  • No labels