温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-11 22:01:23 来源:网络 阅读:303 作者:知止内明 栏目:MySQL数据库

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();
        }
    }

    /**
     * 根据ID查询学生
     */
    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>
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI