Mybatis
Mybatis Poc
Advandages found in Poc Mybatis
1) Compare other ORM tools (Hibernate ,Toplinks) learning curve is easy
2) In compare to Hibernate we are writing one xml file for one POJO but in Mybatsis we configure morethan 1 pojo in the single mappefile
3) In MyBatis only one Mabatis. jar files are enough CRUD Operation
4) All the crud operation are done on the same mapper XML
5) We can to the Association (one to Many,Manyto one ,Many to Many)are done on the same mapper.xml
6) We can also do the Store Procedure
7) Development Time is very less when compare to other ORM and JDBC
8) In is compatible with spring for integration we use Mybatis-spring.jar
9) Developers can Quickly learn and implement in limited Time
10) The one of the advantage of ORM is result s are mapped into Objects compare to JDBC
11) All the SQL Queries are come inside the XML so our code is good to see
12) Simple API to do our data
13) My batis join with Spring we handle transaction. So we avoid transaction kind of worries
Using Mybatis CRUD Operation
IN POJO
Single Row
Brand brand = ( Brand) session.selectOne("selectBrand", 10053);
IN XML
<select id="selectBrand" parameterType="int" resultMap="brandResultMap">
select * from BRAND where BRAND_SYSID = #{brandSysid}
</select>
Muitiple Row
IN POJO
ArrayList < Brand> brands = (ArrayList<Brand>) session.selectList("selectMutipleBrand");
IN XML
<select id="selectMutipleBrand" resultMap="brandResultMap">
select * from BRAND
</select>
Insert Row
IN POO
Try{
session.insert("insertbrand", brand);
}catch(Exception e){}
session.commit;
IN XML
<insert id="insertbrand" parameterType="com.exmple.Brand">
INSERT INTO BRAND (BRAND_SYSID, BRAND_DESCRIPTION, UPDATE_USER_ID,UPDATE_DATE,UPDATE_SYSTEM)
VALUES (#{brandSysid}, #{brandDescription}, #{updateUserId}, #{updateDate}, #{updateSystem})
</insert>
IN POJO
Try{
session.update("updatebrand", brand);
}catch(Exception e){}
session.commit;
IN XML
<update id="updatebrand" parameterType="com.exmple.Brand">
UPDATE BRAND SET
BRAND_DESCRIPTION=#{brandDescription},
UPDATE_USER_ID=#{updateUserId},
UPDATE_DATE=#{updateDate},
UPDATE_SYSTEM=#{updateSystem}
where BRAND_SYSID=#{brandSysid}
</update>
IN POJO
session.delete("deletebran", id);
IN XML
<delete id="deletebran" parameterType="int">
DELETE FROM BRAND where
BRAND_SYSID = #{brandSysid}
</delete>
Relations or Association
INPOJO
MorSaveReport morsavereport = (MorSaveReport) session.selectOne("selectMorsave", "reportBame6");
ArrayList<MORSsveDetails>savedetails= morsavereport.getMorSaveDetailsCollection();
IN XML
<mapper namespace="onetomanymap">
<resultMap id="MorsaveResultMap" type="onetomanymap.MorSaveReport">
<id property="reportName" column="REPORT_NAME" />
<result property="reportType" column="REPORT_TYPE" />
……………
<collection property="morSaveDetailsCollection" ofType="MorSaveDetailsMap" javaType="ArrayList" resultMap="MorSaveDetailsMap"/>
PROCEDURE CALL
INPOJO
Brand e = (Brand)smc.queryForObject
("Brand.getBrnadInfo ", id);
XML
<procedure id="getBrnadInfo" resultClass="Employee"
parameterMap="getBrandInfoCall">
{ call getBrand( #acctID# ) }
</procedure>
<parameterMap id=" getBrandInfoCall " class="map">
<parameter property="acctID" jdbcType="INT"
javaType="java.lang.Integer" mode="IN"/>
</parameterMap>
for Details and source code goto Mybatis Details gotoSpring Jdbc Template
Data access using spring JDBC template
Spring JDBC Template advantages compare to Standard JDBC
1. Jdbc Template class is the central class in the JDBC core package.
2. Jdbc Templates handles the creation, release the resources and clean-up the resources automatically.
3. Helps to avoid common errors such as forgetting the close connection and etc.
4. The Spring JDBC template offers several ways to query the database.
· returns a list of HashMaps
· allows to translates the SQL result direct into an object
· return list of objects
· Populate the objects in a list via matching the bean property names to the column names.
5. Spring JDBC provides exception translation. This allows the programmer to react more flexible to the errors. Well defined API for database exceptions.
6. Using Jdbc templates API packages do
· CRUD operation
· Bulk batch updated
· Stored procedures and SQL function call
· Named parameter call
· Convert result into user custom ways.
7. Understanding APIs and learning curves easy.
Using JDBC Templates - CRUD Operation
1.Updating (INSERT/UPDATE/DELETE)
jdbcTemplate.update("insert into t_actor (first_name, surname) values (?, ?)", new Object[] {"Leonor", "Watling"});
jdbcTemplate.update("update t_actor set weapon = ? where id = ?", new Object[] {"Banjo", new Long(5276)});
jdbcTemplate.update("delete from orders"); // :)
2. Query single row
Customer customer = (Customer)getJdbcTemplate().queryForObject(sql, new Object[] { custId }, new CustomerRowMapper());
3. Query single row with BeanPropertyRowMapper
Customer customer = (Customer)getJdbcTemplate().queryForObject(sql, new Object[] { custId },new BeanPropertyRowMapper(Customer.class));
5. Query multiple rows with manual mapping
List<Map<String, Object>> rows = getJdbcTemplate().queryForList(sql);
6. Query multiple rows with BeanPropertyRowMapper
List<Customer> customers = getJdbcTemplate().query(sql, new BeanPropertyRowMapper(Customer.class));
7. Named Parameter Query
namedParameterJdbcTemplate.queryForObject(query,namedParameters, new RowMapper() {
public Object mapRow(ResultSet resultSet, int rowNum) throws SQLException {
return new Forum(resultSet.getInt("FORUM_ID"),resultSet.getString("FORUM_NAME"),
resultSet.getString("FORUM_DESC"));
}
});
8. Batch Update Query
jdbcTemplate.batchUpdate("insert into z_customer1 (id, first_name, last_name, last_login, comments) values (?, ?, ?, ?, ?)",new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setLong(1, i + 1);
ps.setString(2, firstNames.get(i));
ps.setString(3, lastNames.get(i));
}
public int getBatchSize() { return count; }
});
9. Stored procedures call
countryProcedure = new SimpleJdbcCall(dataSource)
.withoutProcedureColumnMetaDataAccess()
.withProcedureName(procedureName)
.declareParameters(new SqlOutParameter("RETURNCODE", Types.INTEGER))
.declareParameters(new SqlOutParameter("RETURNMSG", Types.VARCHAR))
.returningResultSet("countries", new ParameterizedRowMapper<Country>() {
public Country mapRow(ResultSet rs, int rowNum)
throws SQLException {
Country country = new Country();
country.setId(rs.getInt(1));
country.setName(rs.getString(2));
return country;
}
});
No comments:
Post a Comment