• MyBatis 入门


    在这里插入图片描述

    环境搭建

    需要的文件
    依赖文件 日志文件

    mybatis核心配置文件 mybatis-config.xml (放在resources下)
    直接复制

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <!--数据库连接信息-->
                    <property name="driver" value="com.mysql.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://127.0.0.1/mybatis?useSSL=false"/>	// 地址,数据库
                    <property name="username" value="root"/>		// 数据库用户名
                    <property name="password" value="root"/>		// 数据库密码
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <!--加载映射配置文件-->
            <mapper resource="UserMapper.xml"/>			// 配置文件的路径
        </mappers>
    </configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    修改其中的数据库连接信息即可。
    mappers 加载映射配置文件 (存放的就是下面的配置文件)

    映射配置文件 UserMapper.xml 或者 orderMapper.xml
    看你操作的是哪张表。 操作的数据表+Mapper.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:名称空间
    配合要执行的sql语句标签中的id使用
    -->
    <mapper namespace="test">			
        <select id="selectAll" resultType="com.itheima.pojo.User">	// id 唯一标识。 resultType:操作表的全路径
            SELECT * FROM tb_user									// sql语句
        </select>
    </mapper>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    修改namespace (名称空间) 当前映射文件的唯一标识。
    下面会有select标签 update delete insert 等等。

    便签里的属性值 id 唯一标识。
    resultType 返回结果的类型。 全类名

    爆红很正常。不影响

    pojo类

    以前的javaBean

    测试类

    • 加载核心配置文件
    • 获取sqlSessionFactory 工厂对象
    • 获取SqlSession对象
    • 执行sql语句 (selectList、selectOne)
    • 释放资源
    public class MybatisTest {
        public static void main(String[] args) throws IOException {
            //1.加载核心配置文件,获取SqlSessionFactory工厂对象
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //2.获取SqlSession对象
            SqlSession sqlSession = sqlSessionFactory.openSession();
    // 前两行基本不变
    
            List<User> users = sqlSession.selectList("test.selectAll");		// mapper配置文件中的名称空间.id 
    
            // 释放资源
            sqlSession.close();
    
            // 遍历
            for (User user : users) {
                System.out.println(user);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    Mapper 代理

    java下新建mapper包(类似于以前的dao)
    接口名要与 映射配置文件名称一样。
    方法名 要与sql语句的id一样。返回值一样。
    namespace 要写接口的全类名

    创建maven项目并配置myBatis流程

    • 创建Maven项目
    • 导入依赖
    • 编写核心配置文件
    • 编写映射配置文件
    • 编写映射接口
    • 编写实体类
    • 编写测试类
    第一步:
    
    file   --->  project Structure  --->  新建new models 项目   --->  name(项目名),build system(maven), 
      jdk(看需求) ,  groupId(域名倒序   com.xxx), artifactId( 项目名)
    然后点击创建。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    第二步:
    
    新建完项目以后,pom.xml  文件会自动打开。		即可添加各种依赖。
    https://blog.csdn.net/qq_43635902/article/details/125517778?spm=1001.2014.3001.5501
    复制即可。(仅仅是入门需要的,根据自己需求添加)
    完成。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    第三步:编写核心配置文件  mybatis-config.xml
    
    存放在resources下。
    
    • 1
    • 2
    • 3
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    
        <typeAliases>
            <!--给某个包下的所有类起别名-->
            <package name="com.itheima.pojo"/>
        </typeAliases>
        
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                <!--数据库连接信息-->
                    <property name="driver" value="com.mysql.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://127.0.0.1/mybatis?useSSL=false"/>
                    <property name="username" value="root"/>
                    <property name="password" value="root"/>
                </dataSource>
            </environment>
        </environments>
        
        <mappers>
    	<!--代理开发,包扫描-->
            <package name="com.itheima.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
    第四步:编写映射配置文件
    
    根据自己的实体类来创建。如果是user表,则创建 UserMapper.xml   student 表-> StudentMapper.xml
    
    在resources下新建 文件夹   com/itheima/mapper     存放于此。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    <?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="com.itheima.mapper.StudentMapper">
    	
    	// sql语句	
        <select id="selectAll" resultType="Student">
            SELECT * FROM tb_user
        </select>
        
    </mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    第五步:编写映射接口
    
    在java文件夹下新建包   com.itheima.mapper
    	编写接口文件。
    	方法名要与映射配置文件中的id一样。  返回值根据实际情况判断。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    public interface StudentMapper {
        List<Student> selectAll();
    }
    
    • 1
    • 2
    • 3
    第六步: 实体类
    
    存放在 java文件夹下的 com.itheima.pojo下。
    根据数据库中对应的表结构设计属性。  数据类型要选择包装类。
    
    • 1
    • 2
    • 3
    • 4
    public class Student {
        private Integer id;
        private String username;
        private String password;
        private Character gender;
        private String addr;
    }
    后面生成 get set 方法。toString();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    最后编写测试类。

    public class Test01 {
        public static void main(String[] args) throws IOException {
            // 加载核心配置文件
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            // 获取sqlSessionFactory对象
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            // 获取sqlSession对象
            SqlSession sqlSession = factory.openSession();
            // 获取接口实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    
            // 调用方法
            List<Student> students = mapper.selectAll();
            // 释放资源
            sqlSession.close();
    
            // 遍历
            for (Student student : students) {
    
                System.out.println(student);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    LeetCode_字符串_简单_412.Fizz Buzz
    Windows 内网渗透之横向渗透
    (附源码)计算机毕业设计SSM健身房管理系统
    R语言基于正则表达式筛选dataframe数据列、使用grepl函数按照正则表达式筛选数据列
    GEO生信数据挖掘(七)差异基因分析
    windows编程之位图绘制
    layui--记录
    Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言
    南卡和漫步者哪款更值得入手?音质高的国产蓝牙耳机推荐
    JAVA 单例模式
  • 原文地址:https://blog.csdn.net/qq_43635902/article/details/125517703