Last month i got chance to learn some new techonologies like GWT (Google Web ToolKit) and iBatis. Firstly i would like to demonstrate application on iBatis. In upcoming posts, i would cover GWT.
I found iBatis very useful because With Ibatis I find myself getting the Job done faster, I create any POJO any set of tables, and I link them, much flixability in that field, but little extra work, building Queries and managing Maps (Little time required here). I never worked or got chance to learn hibernate.So bit difficult for me to tell that which is much better.But anyway here i developed a small application which only tells you that how you can use iBatis in your projects.
Download iBatis.jar
http://ibatis.apache.org/javadownloads.html
iBatis:
Step1: create 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>
<settings useStatementNamespaces="true"/>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL"
value="jdbc:mysql://localhost:3306/mhrddemo"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value="mysql"/>
</dataSource>
</transactionManager>
<sqlMap resource="DataBean.xml"/>
</sqlMapConfig>
you can create property file and then use that values in this config file.
2)create another xml where you write query:
DataBean.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Contact">
<!--- Showing all data of table -->
<!--- Showing all data of table -->
<select id="getAll" resultClass="ibdeep.DataBean">
select university_id,entity_type,entity_description from entity_type
</select>
<resultMap id="result" class="ibdeep.DataBean">
<result property="university_id" column="university_id"/>
<result property="entity_type" column="entity_type"/>
<result property="entity_description" column="entity_description"/>
</resultMap>
<select id="getById" resultMap="result">
select * from entity_type where entity_type=#entity_type#
</select>
</sqlMap>
3) create a java file which has getter and setter of table fields or properties.
package ibdeep;
class DataBean
{
private String university_id;
private String entity_type;
private String entity_description;
public DataBean(){
}
public DataBean(String university_id,String entity_type,String entity_description){
this.university_id=university_id;
this.entity_type=entity_type;
this.entity_description=entity_description;
}
public String getUniversity_id(){
return university_id;
}
public void setUniversity_id(String university_id){
this.university_id=university_id;
}
public String getEntity_type(){
return entity_type;
}
public void setEntity_type(String entity_type){
this.entity_type=entity_type;
}
public String getEntity_description(){
return entity_description;
}
public void setEntity_description(String entity_description){
this.entity_description=entity_description;
}
}
Step 4): Create another java file
package ibdeep;
import com.ibatis.common.resources.Resources;
import com.ibatis.db.sqlmap.SqlMap;
import com.ibatis.sqlmap.client.*;
import java.io.Reader;
public class SqlMapManager {
private static SqlMap sqlMap = null;
static SqlMapManager smm;
public static SqlMapClient getSqlMapClient() {
try {
Reader reader = Resources
.getResourceAsReader("ibdeep/SqlMapConfig.xml");
SqlMapClient sqlMapper = SqlMapClientBuilder
.buildSqlMapClient(reader);
reader.close();
return sqlMapper;
} catch (Exception e) {
System.out.println("Map Exception");
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e);
}
}
public void setSqlMap(SqlMap sqlMap) {
SqlMapManager.sqlMap = sqlMap;
}
public static SqlMap getSqlMap() {
return sqlMap;
}
}
Step 5) create properties file.you can use it.You can pass these values directly
url=jdbc:mysql://localhost:3306/mhrddemo
login=root
password=mysql
Step 6) Finally create java file which uses
package ibdeep;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
class IBatisExample
{
public static void main(String[] args)
throws IOException,SQLException{
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient sqlMap =
SqlMapClientBuilder.buildSqlMapClient(reader);
//Output all contacts
System.out.println("All Contacts");
List<DataBean> contacts = (List<DataBean>)
sqlMap.queryForList("Contact.getAll",null);
DataBean contact = null;
for (DataBean c : contacts) {
System.out.print(" " + c.getUniversity_id());
System.out.print(" " + c.getEntity_type());
System.out.print(" " + c.getEntity_description());
contact = c;
System.out.println("");
}
}
}
I hope it is useful.
Hibernate Online Training Hibernate Online Training Spring Online Training Spring Online Training Spring Batch Training Online Spring Batch Training Online
ReplyDeleteJava Training Institutes Java Training Institutes Java Spring Hibernate Training Institutes in Chennai | Hibernate Training Institutes in Chennai Spring Training Institutes in Chennai Spring Training Institutes in Chennai