這篇文章將為大家詳細(xì)講解有關(guān)Java項(xiàng)目怎么利用ibatis進(jìn)行搭建,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
成都創(chuàng)新互聯(lián)公司的客戶來(lái)自各行各業(yè),為了共同目標(biāo),我們?cè)诠ぷ魃厦芮信浜希瑥膭?chuàng)業(yè)型小企業(yè)到企事業(yè)單位,感謝他們對(duì)我們的要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。專業(yè)領(lǐng)域包括網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、電商網(wǎng)站開發(fā)、微信營(yíng)銷、系統(tǒng)平臺(tái)開發(fā)。
IBATIS簡(jiǎn)介
ibatis是 Apache的開源項(xiàng)目,一個(gè)ORM 解決方案,ibatis最大的特點(diǎn)就是小巧,上手很快。
使用 ibatis提供的ORM機(jī)制,對(duì)業(yè)務(wù)邏輯實(shí)現(xiàn)人員而言,面對(duì)的是純粹的Java對(duì)象,這一層與通過(guò)Hibernate 實(shí)現(xiàn)ORM而言是基本一致的。
iBatis是一個(gè)基于SQL映射支持Java和·NET的持久層框架,相對(duì)Hibernate和ApacheOJB等“一站式”O(jiān)RM解決方案而言,iBatis 是一種“半自動(dòng)化”的ORM實(shí)現(xiàn)。
一、JAR包依賴
ibatis-2.3.4.726.jar
MySQL-connector-java-5.0.8-bin.jar
二、SqlMap.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/test username=root password=root
三、SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
四、Student.xml
<?xml version="1.0" encoding="UTF-8" ?>insert into Student(id,name,age,address) values(#id#,#name#,#age#,#address#) select @@identity as inserted delete from student where id = #id# delete from Student where id = #id# update student set name=#name#,age=#age#,address=#address# where id = #id#
五、JAVA代碼
實(shí)體類:略
Dao:略
DaoImpl:
package com.ligang; import java.io.IOException; import java.io.Reader; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; public class StudentDaoImpl implements StudentDao { public static SqlMapClient sqlMapClient = null; static{ try { Reader reader = Resources.getResourceAsReader("com/ligang/SqlMapConfig.xml"); sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader); } catch (IOException e) { e.printStackTrace(); } } public ListfindAll() { List list = null; try { list = sqlMapClient.queryForList("findAll"); } catch (SQLException e) { e.printStackTrace(); } return list; } public Student findByID(String id){ Student student = null; try { student = (Student) sqlMapClient.queryForObject("findByID", id); } catch (SQLException e) { e.printStackTrace(); } return student; } public void addStudent(Student student){ try { sqlMapClient.insert("insertStudent",student); } catch (SQLException e) { e.printStackTrace(); } } public void deleteStudentByID(String id){ try { sqlMapClient.delete("deleteStudentByID",id); } catch (SQLException e) { e.printStackTrace(); } } public void deleteStudent(Student student){ try { sqlMapClient.delete("deleteStudent",student); } catch (SQLException e) { e.printStackTrace(); } } public void updateStudent(Student student){ try { sqlMapClient.update("updateStudent", student); } catch (SQLException e) { e.printStackTrace(); } } public List findByCon(String name){ List stuList = new ArrayList (); try { stuList = sqlMapClient.queryForList("selectByLike",name); } catch (SQLException e) { e.printStackTrace(); } return stuList; } public List findByCon(Student student){ List stuList = new ArrayList (); try { stuList = sqlMapClient.queryForList("findByCon1",student); } catch (SQLException e) { e.printStackTrace(); } return stuList; } public List findByCon(Map map){ List stuList = new ArrayList (); try { stuList = sqlMapClient.queryForList("findByCon2",map); } catch (SQLException e) { e.printStackTrace(); } return stuList; } }
關(guān)于Java項(xiàng)目怎么利用ibatis進(jìn)行搭建就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。