本文的主要内容是简单介绍了ibatis和如何通过ibatis搭建java项目,包含了一个相关实例,需要的朋友可以参考下。
ibatis简介
ibatis是 apache的开源项目,一个orm 解决方案,ibatis最大的特点就是小巧,上手很快。
使用 ibatis提供的orm机制,对业务逻辑实现人员而言,面对的是纯粹的java对象,这一层与通过hibernate 实现orm而言是基本一致的。
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.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的事务管理 -->
<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;
}
}
总结
以上就是java项目搭建之ibatis学习详解的详细内容。