多平台统一管理软件接口,如何实现多平台统一管理软件接口
373
2023-04-01
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
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
四、Student.xml
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
select * from student
select * from student where id = #id#
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#
select * from student where name like '%$name$%'
select * from student where name like '%$name$%' and age >= #age#
select * from student where name like ? and age >= ?
五、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
List
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
List
try {
stuList = sqlMapClient.queryForList("selectByLike",name);
} catch (SQLException e) {
e.printStackTrace();
}
return stuList;
}
public List
List
try {
stuList = sqlMapClient.queryForList("findByCon1",student);
} catch (SQLException e) {
e.printStackTrace();
}
return stuList;
}
public List
List
try {
stuList = sqlMapClient.queryForList("findByCon2",map);
} catch (SQLException e) {
e.printStackTrace();
}
return stuList;
}
}
总结
通过学习我们会发现,Hibernate体系中的内容真的很多,而ibatis更容易上手,小巧灵活。本文有关ibatis搭建Java项目的介绍就到这里,希望对大家有所帮助。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~