Mybatis入门(二)——CRUD

Mybatis入门(二)

添加数据

1.在studentMapping.xml写入SQL

<insert id="studentAdd" parameterType="com.bean.Student">
         insert into student values(#{sno},#{sName},#{sSex},#{sAge},#{sDept});
     </insert>

2.Main函数调用插入数据

String statement="com.bean.studentMapping";
String sIn=statement+".studentAdd";
Student stu=new Student("234","dexx","nvd","de","eddd"); int s=sqlSession.insert(sIn, stu);
sqlSession.commit();

更新数据
1.在studentMapping.xml写入SQL

 

<parameterMap type="Map" id="updMap">
         <parameter property="sName" javaType="String"/>
         <parameter property="sno" javaType="String"/>
     </parameterMap>
     <update id="studentUp" parameterMap="updMap">
         update student set sname=#{sName} where sno=#{sno};
     </update>

 

2.Main函数调用更新数据

String stuSelect=statement+".getStudent";
Student studentInfor=sqlSession.selectOne(stuSelect, "111");
System.out.println(studentInfor.getsName()); String stuUpd
=statement+".studentUp"; Map<String,Object> updMap=new HashMap<String,Object>(); updMap.put("sno", studentInfor.getSno()); updMap.put("sName", "Xu"); sqlSession.update(stuUpd, updMap); sqlSession.commit(); sqlSession.close();

删除数据

1.在studentMapping.xml写入SQL

 

<delete id="stuDel" parameterType="String">
         delete from student where sno=#{sno};
 </delete>

 

2.Main函数调用删除数据

String stuDel=statement+".stuDel";
sqlSession.delete(stuDel, "234");
sqlSession.commit();
sqlSession.close();

 

查询获取多条数据

1.在studentMapping.xml写入SQL

 

 <select id="getStudentAll" resultType="com.bean.Student">
        select * from student;
    </select>

 

2.Main函数调用查询数据

String stuSelectAll=statement+".getStudentAll";
List<Student> StuList=sqlSession.selectList(stuSelectAll);
System.out.println(StuList);
        
sqlSession.close();

 

转载于:https://www.cnblogs.com/Ckeyboard/p/5637008.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值