iBatis搭建JAVA項目

來源:互聯網
上載者:User

標籤:apache   sqlmap   action   類型   class   res   hiberna   span   cti   

iBatis是一個基於SQL映射支援Java和·NET的持久層架構,相對Hibernate和ApacheOJB等“一站式”ORM解決方案而言,iBatis 是一種“半自動化”的ORM實現。

一、JAR包依賴ibatis-2.3.4.726.jar
mysql-connector-java-5.0.8-bin.jar
二、SqlMap.properties
driver=com.mysql.jdbc.Driverurl=jdbc:mysql://127.0.0.1:3306/testusername=rootpassword=root
三、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><!-- 引用JDBC屬性的設定檔 --><properties resource="com/ligang/SqlMap.properties"/><!-- 使用JDBC的交易管理 --><transactionManager type="JDBC"><!-- 資料來源 --><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="com/ligang/Student.xml"/></sqlMapConfig>
四、Student.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><!-- 通過typeAlias使得我們在下面使用Student實體類的時候不需要寫包名 --><typeAlias alias="Student" type="com.ligang.Student"/><!-- id表示select裡的sql語句,resultClass表示返回結果的類型 --><select id="findAll" resultClass="Student">select * from student</select><!-- parameterClass表示參數的內容 --><select id="findByID" parameterClass="String" resultClass="Student">select * from student where id = #id#</select><insert id="insertStudent" parameterClass="Student">insert into Student(id,name,age,address) values(#id#,#name#,#age#,#address#)<!-- 返回自動成長值 --><selectKey resultClass="String" keyProperty="id">select @@identity as inserted</selectKey></insert><delete id="deleteStudentByID" parameterClass="String">delete from student where id = #id#</delete><delete id="deleteStudent" parameterClass="Student">delete from Student where id = #id#</delete><update id="updateStudent" parameterClass="Student">update student set name=#name#,age=#age#,address=#address# where id = #id#</update><!-- 模糊查詢,使用$代替#。此種方法就是去掉了類型檢查,使用字串串連,不過可能會有sql注入風險--><select id="selectByLike" parameterClass="String" resultClass="Student">select * from student where name like ‘%$name$%‘</select><!-- 多條件組合查詢 --><!-- 方法一(物件建構查詢參數) --><!-- 項目中在寫ibatis中的sql語句時,where user_id in (#user_id_list# ),運行時總是不行,這裡不該用#,而應該用$,區別如下:1.#是把傳入的資料當作字串,如#user_id_list#傳入的是1,2,則sql語句產生是這樣,in (‘1,2‘) ,當然不可以2.$傳入的資料直接產生在sql裡,如#user_id_list#傳入的是1,2,則sql語句產生是這樣,in(1,2) 這就對了. 3.#方式能夠很大程度防止sql注入. 4.$方式無法方式sql注入. 5.$方式一般用於傳入資料庫物件.例如傳入表名. 6.一般能用#的就別用$. 直觀的說 #str# 出來的效果是  ‘str‘ $str$ 出來的效果是  str 另外  ##只能用在特定的幾個地方 $$可以用在任何地方  比如 order by $str$ 你甚至可以直接寫  $str$  把 order by 這個字串放在str裡傳進來  --><select id="findByCon1" parameterClass="Student" resultClass="Student">select * from student where name like ‘%$name$%‘ and age >= #age#</select><!-- 方法二(map封裝查詢參數) --><parameterMap class="java.util.HashMap" id="paramMap"><parameter property="name"/><parameter property="age"/></parameterMap><select id="findByCon2" parameterMap="paramMap" resultClass="Student">select * from student where name like ? and age >= ?</select></sqlMap>
五、JAVA代碼實體類:略
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 List<Student> findAll() {List<Student> 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<Student> findByCon(String name){List<Student> stuList = new ArrayList<Student>();try {stuList = sqlMapClient.queryForList("selectByLike",name);} catch (SQLException e) {e.printStackTrace();}return stuList;}public List<Student> findByCon(Student student){List<Student> stuList = new ArrayList<Student>();try {stuList = sqlMapClient.queryForList("findByCon1",student);} catch (SQLException e) {e.printStackTrace();}return stuList;}public List<Student> findByCon(Map map){List<Student> stuList = new ArrayList<Student>();try {stuList = sqlMapClient.queryForList("findByCon2",map);} catch (SQLException e) {e.printStackTrace();}return stuList;}}


iBatis搭建JAVA項目

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.