iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >mybatis-普通sq增删改查学习笔记
  • 154
分享到

mybatis-普通sq增删改查学习笔记

2024-04-02 19:04:59 154人浏览 薄情痞子
摘要

import java.util.*; import cn.mybatis.entity.Student; import cn.mybatis.util.MybatisUtil; import org.


import java.util.*;

import cn.mybatis.entity.Student;
import cn.mybatis.util.MybatisUtil;
import org.apache.ibatis.session.sqlSession;

public class StudentDao {

    
    public   void add(Student student) throws Exception{
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.getSqlSession();
            //事务开始(默认)
            //读取StudentMapper.xml映射文件中的SQL语句
            int i = sqlSession.insert(Student.class.getName()+".add",student);
            System.out.println("本次操作影响了"+i+"行");
            //事务提交
            sqlSession.commit();
        }catch(Exception e){
            e.printStackTrace();
            //事务回滚
            sqlSession.rollback();
            throw e;
        }finally{
            //MybatisUtil.closeSqlSession();
        }
    }

    
    public Student findById(int id) throws Exception{
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.getSqlSession();
            Student student = sqlSession.selectOne(Student.class.getName()+".findById",id);
            sqlSession.commit();
            return student;
        }catch(Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw e;
        }finally{
            MybatisUtil.closeSqlSession();
        }
    }

    
    public List<Student> findAll() throws Exception{
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.getSqlSession();
            return sqlSession.selectList(Student.class.getName()+".findAll");
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }finally{
            MybatisUtil.closeSqlSession();
        }
    }

    
    public void update(Student student) throws Exception{
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.getSqlSession();
            sqlSession.update(Student.class.getName()+".update",student);
            sqlSession.commit();
        }catch(Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw e;
        }finally{
            MybatisUtil.closeSqlSession();
        }
    }

    
    public void delete(Student student) throws Exception{
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.getSqlSession();
            sqlSession.delete(Student.class.getName()+".delete",student);
            //事务
            sqlSession.commit();
        }catch(Exception e){
            e.printStackTrace();
            // 回滚
            sqlSession.rollback();
            throw e;
        }finally{
            MybatisUtil.closeSqlSession();
        }
    }

         public static void main(String[] args) throws Exception {
        StudentDao dao = new StudentDao();
//        dao.add(new Student(3,"美丽",70030.3));
//        dao.add(new Student(4,"加油",70030.3));
//        dao.add(new Student(5,"关系",70030.3));
//        dao.add(new Student(6,"规律",70030.3));
//        dao.add(new Student(7,"古蔺",70030.3));

//     List<Student> studentslist = dao.findAll();
//        for (Student student : studentslist ) {
//            System.out.print(student.getId()+":"+student.getName()+":"+student.getSal());
//        }
//        Student student = dao.findById(4);
//        student.setName("liwen");
//        dao.update(student);
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "Http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="cn.mybatis.entity.Student">

    <resultMap type="cn.mybatis.entity.Student" id="studentMap">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="sal" column="sal"/>
    </resultMap>

        <!-- 增加学生 -->
    <insert id="add" parameterType="cn.mybatis.entity.Student">
        insert into students(id,name,sal) values(#{id},#{name},#{sal})
    </insert>

    <!-- 根据ID查询学生
         如果参数不是一个实体的话,只是一个普通变量,例如:int,double,String
         这里的#{中间的变量名可以随便写},不过提倡就用方法的形参
     -->
    <select id="findById" parameterType="int" resultType="cn.mybatis.entity.Student">
        select id,name,sal from students where id = #{id}
    </select>

    <!-- 查询所有学生
         理论上resultType要写List<Student>
         但这里只需书写List中的类型即可,即只需书写Student的全路径名
    -->
    <select id="findAll" resultType="cn.mybatis.entity.Student">
        select id,name,sal from students
    </select>

    <!-- 更新学生 -->
    <update id="update" parameterType="cn.mybatis.entity.Student">
        update students set name=#{name},sal=#{sal} where id=#{id}
    </update>

    <!-- 删除学生 -->
    <delete id="delete" parameterType="cn.mybatis.entity.Student">
        delete from students where id = #{id}
    </delete>

    <!-- 无条件分页 -->
    <select id="findAllWithFy" parameterType="map" resultMap="studentMap">
        select id,name,sal from students limit #{pstart},#{psize}
    </select>
您可能感兴趣的文档:

--结束END--

本文标题: mybatis-普通sq增删改查学习笔记

本文链接: https://www.lsjlt.com/news/41058.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作