• MyBatis-实现增删查改


    MyBatis-新建项目: https://www.cnblogs.com/chenliugou/p/14502135.html

    实现:主要是实现了查询单条数据、删除数据、修改、更新、查询全部数据的功能。

    问题:在查询全部当中只能用:List students=session.selectList(statement);,如果用老师写的:List<Student> students=session.selectList(statement); 会报错。唉,还好知道用其他方法。

    注:conf1没有作用的,只是笔记

    • 整体情况

    • studentMapper.xml
    <?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">
            <!--namespace:该mapper.xml映射文件的唯一标识  -->
            <mapper namespace="org.lanqiao.entity.studentMapper">
            <!--parameterType:输入参数的类型
                resultType查询返回结果的类型,返回类型
              -->
             <select id="queryStudentById" resultType="org.lanqiao.entity.Student" parameterType="int">
                 select * from student where stuno =#{stuno}
             </select>
             
             <insert id="addStudent" parameterType="org.lanqiao.entity.Student">
                 insert into student(stuno,stuname,stuage,graname) values(#{stuNo},#{stuName},#{stuAge},#{graName})
             </insert>
             
             <delete id="deleteStudentByStuno" parameterType="int" >
                 delete from student where stuno =#{stuno}
             </delete>
             
             <update id="updateStudentByStuno" parameterType="org.lanqiao.entity.Student">
                 update student set stuname=#{stuName},stuage=#{stuAge},graname=#{graName} where stuno=#{stuNo}
             </update>
             
             <select id="querAllStudents" resultType="org.lanqiao.entity.Student">
                 select * from student
             </select>
    </mapper>
    • 测试Test.class
    package org.lanqiao.entity;
    
    import java.io.IOException;
    import java.io.Reader;
    import java.util.List;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    
    public class Test {
            public static void queryStudentById() throws IOException{
                //加载MyBatis文件(为了访问数据库)
                Reader reader=Resources.getResourceAsReader("conf.xml");
                // SqlsessionFactory - connection
                SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
                //session - connection
                SqlSession session=sessionFactory.openSession();
                String statement="org.lanqiao.entity.studentMapper.queryStudentById";
                Student student= (Student) session.selectOne(statement,Integer.valueOf(1));
                System.out.println(student);
                session.close();
            }
            
            public static void queryAllStudent() throws IOException{
                //加载MyBatis文件(为了访问数据库)
                Reader reader=Resources.getResourceAsReader("conf.xml");
                // SqlsessionFactory - connection
                SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
                //session - connection
                SqlSession session=sessionFactory.openSession();
                String statement="org.lanqiao.entity.studentMapper.querAllStudents";
                List students=session.selectList(statement);
                System.out.println(students);
                session.close();
            }
            //增加id
            public static void addStudent() throws IOException{
                //加载MyBatis文件(为了访问数据库)
                Reader reader=Resources.getResourceAsReader("conf.xml");
                // SqlsessionFactory - connection
                SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
                //session - connection
                SqlSession session=sessionFactory.openSession();
                String statement="org.lanqiao.entity.studentMapper.addStudent";//执行的sql
                Student student=new Student(3,"22",21,"g1");
                int count =session.insert(statement,student);
                session.commit();
                System.out.println("增加"+count+"个学生");
                session.close();
                }    
            //删除id
            public static void deleteStudentByStuno() throws IOException{
                //加载MyBatis文件(为了访问数据库)
                Reader reader=Resources.getResourceAsReader("conf.xml");
                // SqlsessionFactory - connection
                SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
                //session - connection
                SqlSession session=sessionFactory.openSession();
                String statement="org.lanqiao.entity.studentMapper.deleteStudentByStuno";//执行的sql
                int count=session.delete(statement, Integer.valueOf(3));
                
                session.commit();
                System.out.println("删除"+count+"个学生");
                session.close();
                }
            //修改id
            public static void updateStudentByStuno() throws IOException{
                //加载MyBatis文件(为了访问数据库)
                Reader reader=Resources.getResourceAsReader("conf.xml");
                // SqlsessionFactory - connection
                SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
                //session - connection
                SqlSession session=sessionFactory.openSession();
                String statement="org.lanqiao.entity.studentMapper.updateStudentByStuno";//执行的sql
                //修改那个人
                Student student=new Student();
                student.setStuNo(2);
                student.setStuName("lxs");
                student.setStuAge(19);
                student.setGraName("s2");
                int count =session.update(statement, student);
                session.commit();
                System.out.println("修改"+count+"个学生");
                session.close();
                }
            
            
            public static void main(String[] arg) throws IOException{
                //查询全部
                queryAllStudent();
                //更新
                updateStudentByStuno();
                //删除
                deleteStudentByStuno();
                //新增
                addStudent();
                //查询全部
                queryAllStudent();
            }
                
    }
    • Student.java
    package org.lanqiao.entity;
    
    public class Student {
        private int stuNo;
        private String stuName;
        private int stuAge;
        private String graName;
        public int getStuNo() {
            return stuNo;
        }
        public void setStuNo(int stuNo) {
            this.stuNo = stuNo;
        }
        public String getStuName() {
            return stuName;
        }
        public void setStuName(String stuName) {
            this.stuName = stuName;
        }
        public int getStuAge() {
            return stuAge;
        }
        public void setStuAge(int stuAge) {
            this.stuAge = stuAge;
        }
        public String getGraName() {
            return graName;
        }
        public void setGraName(String graName) {
            this.graName = graName;
        }
        public Student(int stuNo, String stuName, int stuAge, String graName) {
            super();
            this.stuNo = stuNo;
            this.stuName = stuName;
            this.stuAge = stuAge;
            this.graName = graName;
        }
        public Student() {
            
        }
        public String toString() {
            return "Student [stuNo=" + this.stuNo + ", stuName=" + this.stuName + ", stuAge="
                    + this.stuAge + ", graName=" + this.graName + "]";
        }
        
    }
  • 相关阅读:
    企业级Nginx负载均衡与keepalived高可用实战(一)Nginx篇
    Elasticsearch由浅入深(十一)内核原理
    Elasticsearch由浅入深(十一)索引管理
    Elasticsearch由浅入深(十)搜索引擎:相关度评分 TF&IDF算法、doc value正排索引、解密query、fetch phrase原理、Bouncing Results问题、基于scoll技术滚动搜索大量数据
    Elasticsearch由浅入深(九)搜索引擎:query DSL、filter与query、query搜索实战
    Elasticsearch由浅入深(八)搜索引擎:mapping、精确匹配与全文搜索、分词器、mapping总结
    Elasticsearch由浅入深(七)搜索引擎:_search含义、_multi-index搜索模式、分页搜索以及深分页性能问题、query string search语法以及_all metadata原理
    Elasticsearch由浅入深(六)批量操作:mget批量查询、bulk批量增删改、路由原理、增删改内部原理、document查询内部原理、bulk api的奇特json格式
    Elasticsearch由浅入深(五)_version乐观锁、external version乐观锁、partial update、groovy脚本实现partial update
    Elasticsearch由浅入深(四)ES并发冲突、悲观锁与乐观锁、_version乐观锁并发
  • 原文地址:https://www.cnblogs.com/chenliugou/p/14521175.html
Copyright © 2020-2023  润新知