欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ibatis學(xué)習(xí)之搭建Java項(xiàng)目

 更新時(shí)間:2017年09月20日 10:07:15   作者:奮飛  
本文的主要內(nèi)容是簡單介紹了ibatis和如何通過iBatis搭建JAVA項(xiàng)目,包含了一個(gè)相關(guān)實(shí)例,需要的朋友可以參考下。

IBATIS簡介

ibatis是 Apache的開源項(xiàng)目,一個(gè)ORM 解決方案,ibatis最大的特點(diǎn)就是小巧,上手很快。

使用 ibatis提供的ORM機(jī)制,對業(yè)務(wù)邏輯實(shí)現(xiàn)人員而言,面對的是純粹的Java對象,這一層與通過Hibernate 實(shí)現(xiàn)ORM而言是基本一致的。

iBatis是一個(gè)基于SQL映射支持Java和·NET的持久層框架,相對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"?>
<!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的事務(wù)管理 -->
	<transactionManager type="JDBC">
		<!-- 數(shù)據(jù)源 -->
		<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>
	<!-- 這里可以寫多個(gè)實(shí)體的映射文件 -->
	<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實(shí)體類的時(shí)候不需要寫包名 -->
	<typeAlias alias="Student" type="com.ligang.Student"/>
	
	<!-- id表示select里的sql語句,resultClass表示返回結(jié)果的類型 -->
	<select id="findAll" resultClass="Student">
		select * from student
	</select>
	<!-- parameterClass表示參數(shù)的內(nèi)容 -->
	<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#)
		<!-- 返回自動(dòng)增長值 -->
		<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注入風(fēng)險(xiǎn)-->
	<select id="selectByLike" parameterClass="String" resultClass="Student">
		select * from student where name like '%$name$%'
	</select>
	<!-- 多條件組合查詢 -->
	<!-- 方法一(對象構(gòu)造查詢參數(shù)) -->
	<!-- 項(xiàng)目中在寫ibatis中的sql語句時(shí),where user_id in (#user_id_list# ),運(yùn)行時(shí)總是不行,這里不該用#,而應(yīng)該用$,區(qū)別如下:
		1.#是把傳入的數(shù)據(jù)當(dāng)作字符串,如#user_id_list#傳入的是1,2,則sql語句生成是這樣,in ('1,2') ,當(dāng)然不可以
		2.$傳入的數(shù)據(jù)直接生成在sql里,如#user_id_list#傳入的是1,2,則sql語句生成是這樣,in(1,2) 這就對了. 
		3.#方式能夠很大程度防止sql注入. 
		4.$方式無法方式sql注入. 
		5.$方式一般用于傳入數(shù)據(jù)庫對象.例如傳入表名. 
		6.一般能用#的就別用$. 
		直觀的說 
		#str# 出來的效果是 'str' 
		$str$ 出來的效果是 str 
		另外 ##只能用在特定的幾個(gè)地方 $$可以用在任何地方 比如 order by $str$ 
		你甚至可以直接寫 $str$ 把 order by 這個(gè)字串放在str里傳進(jìn)來 -->
	<select id="findByCon1" parameterClass="Student" resultClass="Student">
		select * from student where name like '%$name$%' and age >= #age#
	</select>
	<!-- 方法二(map封裝查詢參數(shù)) -->
	<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代碼

實(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 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;
	}
}

總結(jié)

通過學(xué)習(xí)我們會發(fā)現(xiàn),Hibernate體系中的內(nèi)容真的很多,而ibatis更容易上手,小巧靈活。本文有關(guān)ibatis搭建Java項(xiàng)目的介紹就到這里,希望對大家有所幫助。

相關(guān)文章

  • Mybatis調(diào)用MySQL存儲過程的簡單實(shí)現(xiàn)

    Mybatis調(diào)用MySQL存儲過程的簡單實(shí)現(xiàn)

    本篇文章主要介紹了Mybatis調(diào)用MySQL存儲過程的簡單實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-04-04
  • RocketMQ設(shè)計(jì)之異步刷盤

    RocketMQ設(shè)計(jì)之異步刷盤

    本文介紹RocketMQ設(shè)計(jì)之異步刷盤,RocketMQ消息存儲到磁盤上,這樣既保證斷電后恢復(fù),也讓存儲消息量超出內(nèi)存限制,RocketMQ為了提高性能,會盡可能保證磁盤順序?qū)?消息通過Producer寫入RocketMQ的時(shí)候,有兩種方式,上篇介紹了同步刷盤,本文介紹異步刷盤,需要的朋友可以參考下
    2022-03-03
  • SpringBoot整合Security權(quán)限控制登錄首頁

    SpringBoot整合Security權(quán)限控制登錄首頁

    這篇文章主要為大家介紹了SpringBoot整合Security權(quán)限控制登錄首頁示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Java調(diào)用Shell命令的方法

    Java調(diào)用Shell命令的方法

    這篇文章主要介紹了Java調(diào)用Shell命令的方法,實(shí)例分析了java調(diào)用shell命令的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • java+io+swing實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)

    java+io+swing實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java+io+swing實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • Java開發(fā)基礎(chǔ)日期類代碼詳解

    Java開發(fā)基礎(chǔ)日期類代碼詳解

    這篇文章主要介紹了Java開發(fā)基礎(chǔ)日期類的相關(guān)內(nèi)容,代碼通過日期工具類獲取指定月份的星期與日期對應(yīng)關(guān)系,以及獲取指定月份的所有日期與星期集合等,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • SpringBoot在 POM 中引入本地 JAR 包的方法

    SpringBoot在 POM 中引入本地 JAR 包的方法

    在開發(fā) Spring Boot 應(yīng)用程序時(shí),您可能需要使用本地 JAR 包來添加自定義庫或功能,本文將介紹在 Spring Boot 項(xiàng)目的 POM 文件中如何引入本地 JAR 包,感興趣的朋友跟隨小編一起看看吧
    2023-08-08
  • Java創(chuàng)建二叉搜索樹,實(shí)現(xiàn)搜索,插入,刪除的操作實(shí)例

    Java創(chuàng)建二叉搜索樹,實(shí)現(xiàn)搜索,插入,刪除的操作實(shí)例

    下面小編就為大家分享一篇Java創(chuàng)建二叉搜索樹,實(shí)現(xiàn)搜索,插入,刪除的操作實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助
    2017-12-12
  • spring mvc中直接注入的HttpServletRequst安全嗎

    spring mvc中直接注入的HttpServletRequst安全嗎

    這篇文章主要給大家介紹了關(guān)于spring mvc中直接注入的HttpServletRequst是不是安全的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2018-04-04
  • 通過Spring自定義NamespaceHandler實(shí)現(xiàn)命名空間解析(推薦)

    通過Spring自定義NamespaceHandler實(shí)現(xiàn)命名空間解析(推薦)

    這篇文章主要介紹了通過Spring自定義NamespaceHandler實(shí)現(xiàn)命名空間解析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04

最新評論