• 【MyBatis框架】Mybatis实战之基础篇


    😊😊😊欢迎来到本博客😊😊😊
    📫作者简介:阿斯卡码,专注于研究Java框架/Vue,就读于河南中医药大学,刚刚入门项目开发📫
    🏆 CSDN编程比赛奖章获得者/Java领域创作者🏆
    🔥计划学习:深入学习Spring全家桶,Vue, mybatis,Mysql等领域。(目前涉及不深入)🔥
    👍如果此文还不错的话,还请👍关注、点赞、收藏三连支持👍一下博主~

    项目代码:
    链接:https://pan.baidu.com/s/1POo7Gscj3JgnUji9sXmWbw
    提取码:6666
    –来自百度网盘超级会员V3的分享

    一:结构框架

    mapper下的xml文件都会被package识别。
    pojo下的所有类都会被package识别。
    所以递归也会识别。
    在这里插入图片描述

    二:开发流程

    1.基本配置:

    1.1配置Application.properties

    jdbc.driver=com.mysql.cj.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/Stu?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
    #jdbc.url=jdbc:mysql://localhost:3306/?????????????serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
    jdbc.username=root
    jdbc.password=XXXXXXXX
    
    • 1
    • 2
    • 3
    • 4
    • 5

    1.2.配置mybatis-config

    
    DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    
        
        <properties resource="Application.properties" />
    
        <settings>
            <setting name="logImpl" value="STDOUT_LOGGING"/>
        settings>
    
        
        
        
        <typeAliases>
            <package name="com.mybatis.ch01.pojo"/>
        typeAliases>
    
    
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                
                <dataSource type="POOLED">
                    <property name="driver" value="${jdbc.driver}"/>
                    <property name="url" value="${jdbc.url}"/>
                    <property name="username" value="${jdbc.username}"/>
                    <property name="password" value="${jdbc.password}"/>
                dataSource>
            environment>
        environments>
    
        
        
        <mappers>
            <package name="com.mybatis.ch01.mapper"/>
        mappers>
    configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    1.3配置MyBatisUtil工具类

    package com.mybatis.ch01.utils;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    /**
     *  工具类:创建SqlSession对象
     */
    public class MyBatisUtil {
    
        private static SqlSessionFactory factory  = null;
    
        static {
    
            String config= "mybatis-config.xml";
            try {
                InputStream inputStream  =Resources.getResourceAsStream(config);
                factory  = new SqlSessionFactoryBuilder().build(inputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        //创建方法,获取SqlSession对象
        public static SqlSession getSqlSession(){
            SqlSession session = null;
            if( factory != null ){
                session =factory.openSession(true);// openSession(true);
            }
            return session;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    1.4配置pom.xml

    build标签

     
     <resources>
       <resource>
         <directory>src/main/javadirectory>
         <includes>
           <include>**/*.propertiesinclude>
           <include>**/*.xmlinclude>
         includes>
         <filtering>falsefiltering>
       resource>
     resources>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    dependencies标签

    	//Test测试使用
        <dependency>
          <groupId>junitgroupId>
          <artifactId>junitartifactId>
          <version>4.11version>
          <scope>testscope>
        dependency>
    	//JDBC使用
        <dependency>
          <groupId>mysqlgroupId>
          <artifactId>mysql-connector-javaartifactId>
          <version>8.0.30version>
        dependency>
    
    	//mybatis依赖
        <dependency>
          <groupId>org.mybatisgroupId>
          <artifactId>mybatisartifactId>
          <version>3.4.6version>
        dependency>
    	
    	//@Data注解需要
        <dependency>
          <groupId>org.projectlombokgroupId>
          <artifactId>lombokartifactId>
          <version>1.18.12version>
          <scope>providedscope>
        dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    2.编写pojo层(对应数据库中的表||导出的类)

    Student.java

    package com.mybatis.ch01.pojo;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.ToString;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @ToString
    public class Student {
        private Integer id;
        private String name;
        private String email;
        private Integer age;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    Teacher.java

    package com.mybatis.ch01.pojo;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.ToString;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @ToString
    public class Teacher {
        private Integer id;
        private String name;
        private String tno;
        private String classname;
        private Integer money;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    ST.java:导出类

    package com.mybatis.ch01.pojo.utils;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.ToString;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @ToString
    public class ST {
        private Integer studentid;
        private String studentname;
        private Integer teacherid;
        private String teachername;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    3.编写mapper层

    StudentMapper.java

    package com.mybatis.ch01.mapper.student;
    
    import com.mybatis.ch01.pojo.Student;
    import org.apache.ibatis.annotations.Param;
    
    import java.util.List;
    import java.util.Map;
    
    public interface StudentMapper {
        //传入一个简单类型的参数
        Student selectByIdOne(Integer id);
    
        //传入多个简单类型的参数
        List<Student> selectByNameEmailTwo (@Param("myName") String name,
                                            @Param("myEmail") String email
                                            );
        //传入一个对象
        List<Student> selectByNameEmailTwo2 (Student student);
    
        //使用位置,获取参数
        List<Student> selectByNameEmailTwo3 (String name,String email);
    
        //参数是一个Map
        List<Student> selectByNameEmailTwo4 (Map<String,Object> data);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    StudentMapper.xml

    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.mybatis.ch01.mapper.student.StudentMapper">
        
        <select id="selectByIdOne" resultType="Student">
            select * from student where id = #{myId}
        select>
    
        
        <select id="selectByNameEmailTwo" resultType="Student">
             select * from student where email = #{myEmail} and name = #{myName}
        select>
    
        
        <select id="selectByNameEmailTwo2" resultType="Student">
            select * from student where email = #{email} and name = #{name}
        select>
    
        
        <select id="selectByNameEmailTwo3" resultType="Student">
            select * from student where name = #{arg0} and email = #{arg1}
        select>
    
        
        <select id="selectByNameEmailTwo4" resultType="Student">
            select * from student where name = #{name} and email = #{email}
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    TeacherMapper.java

    package com.mybatis.ch01.mapper.teacher;
    
    import com.mybatis.ch01.pojo.Teacher;
    import org.apache.ibatis.annotations.Param;
    
    import java.util.List;
    import java.util.Map;
    
    public interface TeacherMapper {
        List<Teacher> selectByTows(Map<String,Object> data);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    TeacherMapper.xml

    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.mybatis.ch01.mapper.teacher.TeacherMapper">
        <select id="selectByTows" resultType="Teacher">
            select * from teacher where classname = #{classname} and money = #{money}
      select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    重点:
    STMapper.java

    package com.mybatis.ch01.mapper.utils;
    
    import com.mybatis.ch01.pojo.utils.ST;
    
    import java.util.List;
    
    public interface STMapper {
        List<ST> getList();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.mybatis.ch01.mapper.utils.STMapper">
        <select id="getList" resultType="ST">
            select s.id studentid,s.name studentname,t.id teacherid,t.name teachername from student s,teacher t where s.id = t.id
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    4.测试:

    package com.mybatis.ch01;
    
    import static org.junit.Assert.assertTrue;
    
    import com.mybatis.ch01.mapper.student.StudentMapper;
    import com.mybatis.ch01.mapper.teacher.TeacherMapper;
    import com.mybatis.ch01.mapper.utils.STMapper;
    import com.mybatis.ch01.pojo.Student;
    import com.mybatis.ch01.pojo.Teacher;
    import com.mybatis.ch01.pojo.utils.ST;
    import com.mybatis.ch01.utils.MyBatisUtil;
    import org.apache.ibatis.session.SqlSession;
    import org.junit.Test;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * Unit test for simple App.
     */
    public class AppTest 
    {
        /**
         * 传入一个简单类型的参数
         */
        @Test
        public void selectByIdOne()
        {
            //1.获取SqlSession
            SqlSession session = MyBatisUtil.getSqlSession();
            //2.获取dao的代理
            StudentMapper dao = session.getMapper(StudentMapper.class);
            Student stu  = dao.selectByIdOne(1007);
    
            System.out.println(stu);
            //3.关闭SqlSession对象
            session.close();
        }
    
        /**
         * 传入多个简单类型的参数
         */
        @Test
        public void selectByNameEmailTwo()
        {
            SqlSession session = MyBatisUtil.getSqlSession();
            StudentMapper dao = session.getMapper(StudentMapper.class);
            List<Student> stus = dao.selectByNameEmailTwo("yxc1007","yxc1@qq.com");
            for (Student student:stus){
                System.out.println("stu==" + student);
            }
            session.close();
        }
    
        /**
         * 传入一个对象
         */
        @Test
        public void selectByNameEmailTwo2()
        {
            SqlSession session = MyBatisUtil.getSqlSession();
            StudentMapper dao = session.getMapper(StudentMapper.class);
            Student student = new Student();
            student.setName("yxc1007");
            student.setEmail("yxc1@qq.com");
            List<Student> stus = dao.selectByNameEmailTwo2(student);
            for (Student stu:stus){
                System.out.println("stu==" + stu);
            }
            session.close();
        }
    
        /**
         * 使用位置,获取参数
         */
        @Test
        public void selectByNameEmailTwo3()
        {
            SqlSession session = MyBatisUtil.getSqlSession();
            StudentMapper dao = session.getMapper(StudentMapper.class);
            List<Student> stus = dao.selectByNameEmailTwo3("yxc1007","yxc1@qq.com");
            for (Student stu:stus){
                System.out.println("stu==" + stu);
            }
            session.close();
        }
        /*
           使用Map作为参数
         */
        @Test
        public void selectByNameEmailTwo4()
        {
            SqlSession session = MyBatisUtil.getSqlSession();
            StudentMapper dao = session.getMapper(StudentMapper.class);
            Map<String,Object> data = new HashMap<>();
            data.put("name","yxc1007");
            data.put("email","yxc1@qq.com");
            List<Student> stus = dao.selectByNameEmailTwo4(data);
            for (Student stu:stus){
                System.out.println("stu==" + stu);
            }
            session.close();
        }
    
        /*
           使用Map作为参数(练习2)
         */
        @Test
        public void selectByTwos()
        {
            SqlSession session = MyBatisUtil.getSqlSession();
            TeacherMapper dao = session.getMapper(TeacherMapper.class);
            Map<String,Object> data = new HashMap<>();
            data.put("classname","计科");
            data.put("money",3000);
            List<Teacher> teachers = dao.selectByTows(data);
            for (Teacher tea:teachers){
                System.out.println("tea==" + tea);
            }
            session.close();
        }
    
        /**
         * 返回多表的数据
         */
        @Test
        public void getList()
        {
            SqlSession session = MyBatisUtil.getSqlSession();
            STMapper dao = session.getMapper(STMapper.class);
            List<ST> sts = dao.getList();
            for (ST s:sts){
                System.out.println("st==" + s);
            }
            session.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
  • 相关阅读:
    分布式 PostgreSQL 集群(Citus),官方快速入门教程
    Android Framework——进程间通讯学习,从Binder使用看起
    perf性能分析工具使用分享
    浙政钉用户登陆
    React Native调用摄像头画面及拍照和保存图片到相册全流程
    遥感影像正射矫正及高分二号遥感影像获取
    LLM下半场之Agent基础能力概述:Profile、Memory、Plan、Action、Eval学习笔记
    Acwing 491. 字符串的展开
    异硫氰酸荧光素标记磁性碲化钨纳米粒FITC-WTe2 NPs|FITC-Hyd-PEG-WTe2|齐岳
    文档图片阴影去除
  • 原文地址:https://blog.csdn.net/qq_52384627/article/details/126653154