iBatis的增、删、查、改(执行通过)

本文详细介绍了SQLMap配置文件、属性文件、学生数据实体类、数据访问接口及实现类,展示了如何通过SQLMap进行学生数据的增删改查操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、首先要有表


2、目录结构


3、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>

<properties resource="SqlMapConfig.properties"/>

<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="sqlMap_student.xml"/>

</sqlMapConfig>

4、SqlMapConfig.properties文件:

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://127.0.0.1:3306/zlx

username=root

password=123456


5、StudentDto.java

public class StudentDto

{

private int id = 0;

private String name = "";

private String sex = "";

private int age = 0;

private String address = "";

public int getId()

{

return id;

}

public void setId(int id)

{

this.id = id;

}

public String getName()

{

return name;

}

public void setName(String name)

{

this.name = name;

}

public String getSex()

{

return sex;

}

public void setSex(String sex)

{

this.sex = sex;

}

public int getAge()

{

return age;

}

public void setAge(int age)

{

this.age = age;

}

public String getAddress()

{

return address;

}

public void setAddress(String address)

{

this.address = address;

}

}

6、StudentDao.java

public interface StudentDao

{

//添加student表的数据

public void addStudent(SqlMapClient sqlMap,StudentDto studentdto);

//删除student表的数据

public void delStudent(SqlMapClient sqlMap);

//删除student表的指定ID数据

public void delStudentByID(SqlMapClient sqlMap,StudentDto studentdto);

//更新student表的数据

public void updateStudent(SqlMapClient sqlMap,StudentDto studentdto);

//查询student表的所有数据

public ArrayList selectStudent(SqlMapClient sqlMap);

//查询student表的指定ID数据

public StudentDto selectStudentByID(SqlMapClient sqlMap,StudentDto studentdto);

}

7、StudentImpl.java

public class StudentImpl implements StudentDao

{

//添加student表的数据

public void addStudent(SqlMapClient sqlMap,StudentDto studentdto)

{

try {

sqlMap.insert("insert_student",studentdto);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


//删除student表的数据

public void delStudent(SqlMapClient sqlMap)

{

try {

sqlMap.delete("delete_all_student",null);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


//删除student表的指定ID数据

public void delStudentByID(SqlMapClient sqlMap, StudentDto studentdto)

{

try {

sqlMap.delete("deleteByID_student",studentdto);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


//更新student表的数据

public void updateStudent(SqlMapClient sqlMap, StudentDto studentdto)

{

try {

sqlMap.update("updateStudent_test",studentdto);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


//查询student表的所有数据

public ArrayList selectStudent(SqlMapClient sqlMap)

{

//保存查询结果

ArrayList rsList = new ArrayList();

try {

rsList = (ArrayList)sqlMap.queryForList("select_all_student","");

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return rsList;

}


//查询student表的指定ID数据

public StudentDto selectStudentByID(SqlMapClient sqlMap, StudentDto studentdto)

{

StudentDto info = new StudentDto();

try {

info = (StudentDto)sqlMap.queryForObject("selectByID_student",studentdto);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return info;

}

}


8、MainTest.java 测试类

public class MainTest

{

public StudentImpl impl = new StudentImpl();

public StudentDto info = new StudentDto();

public static SqlMapClient sqlmapclient = null;

static{

try{

Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");

sqlmapclient = SqlMapClientBuilder.buildSqlMapClient(reader);

reader.close();

}catch(IOException e)

{

e.printStackTrace();

}

}

public static void main(String[] args)

{

MainTest stu = new MainTest();

System.out.println("---------start--------");

//以下为各种方法测试

//stu.addStudent_test();

//stu.delStudentByID_test();

//stu.selectStudentByID_test();

//stu.selectStudent_test();

stu.updateStudent_test();

System.out.println("---------end----------");

}

//添加student表的数据

public void addStudent_test()

{

info.setId(5);

info.setName("zhangxiang");

info.setSex("男");

info.setAge(24);

info.setAddress("临沂");

impl.addStudent(sqlmapclient, info);

}

//删除student表的指定ID数据

public void delStudentByID_test()

{

info.setId(5);

impl.delStudentByID(sqlmapclient, info);

}

//更新student表的数据

public void updateStudent_test()

{

info.setId(6);

info.setName("jingnan");

info.setSex("女");

info.setAge(20);

info.setAddress("上海");

impl.updateStudent(sqlmapclient, info);

}

//查询student表的所有数据

public void selectStudent_test()

{

StudentDto stu_dto = new StudentDto();

ArrayList resultList = impl.selectStudent(sqlmapclient);

for(int i = 0; i < resultList.size(); i++)

{

stu_dto = (StudentDto)resultList.get(i);

show(stu_dto);

}

}

//查询student表的指定ID数据

public void selectStudentByID_test()

{

StudentDto stu_dto = new StudentDto();

info.setId(1);

stu_dto = impl.selectStudentByID(sqlmapclient, info);

if(stu_dto != null)

{

show(stu_dto);

}

else

{

System.out.println("no data!!!!");

}

}

//打印查询结果

public void show(StudentDto stu_dto)

{

System.out.println("学生:" + stu_dto.getId() + ";");

System.out.println("学生姓名:" + stu_dto.getName() + ";");

System.out.println("学生性别:" + stu_dto.getSex() + ";");

System.out.println("学生年龄:" + stu_dto.getAge() + ";");

System.out.println("学生地址:" + stu_dto.getAddress());

System.out.println();

}

}

9、sqlMap_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 namespace="Test">

<statement id="insert_student"

parameterClass="dto.StudentDto">

insert into student(id,name,age,sex,address) values(#id#,#name#,#age#,#sex#,#address#)

</statement>


<statement id="delete_all_student">

delete from student

</statement>


<statement id="deleteByID_student"

parameterClass="dto.StudentDto">

delete from student where id=#id#

</statement>


<statement id="updateStudent_test"

parameterClass="dto.StudentDto">

update student set name=#name#,sex=#sex#,age=#age#,address=#address# where id=#id#

</statement>


<statement id="select_all_student" resultClass="dto.StudentDto">

select * from student order by id

</statement>


<statement id="selectByID_student"

parameterClass="dto.StudentDto"

resultClass="dto.StudentDto">

select * from student where id=#id# order by id

</statement>

</sqlMap>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值