• MyBatis 第一个程序


    MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射

    导包

    我用的idea maven,直接在仓库导入

    • 坐标
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatisartifactId>
                <version> 3.5.2version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 方法
      idea新建maven项目/模块-构建系统选择Maven-在pom.xml文件添加 ,在这个标签中添加坐标-旁边跳出一个刷新符号,点击自动导入
    • 补充
      以后导包,坐标可以去maven仓库中查找,然后用这种方法导就可

    配置文件

    • 核心配置文件

    说明 主要配置了数据库连接信息 、注册xml
    注意 & 用 &;替换

    
    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.cj.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://localhost:3306/girls?user=root&password=root&useUnicode=true&characterEncoding=gbk&serverTimezone=UTC"/>
                    <property name="username" value="root"/>
                    <property name="password" value="root"/>
                dataSource>
            environment>
        environments>
    
        <mappers>
            <mapper resource="dao/TestMapper.xml"/>
        mappers>
    configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • Mapper 替换以前的DAO的实现类

    • resultType 是返回类型,目前只填泛型类型 也就是你的表所对应的类,返回值用LIst<泛型类型>
    • id 写绑定接口中对应方法名
    • #{xx} 代表 以前的填充符?
    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="dao.TestMapper">
        <select id="getTestById" resultType="obj.testObj">
            select * from test where id = #{id}
        select>
    mapper>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • pom.xml

    idea默认的配置文件放在src目录下,如果放在子目录中,则需要加这样几行代码来顺利导出资源
    注意 filtering

        <build>
            <resources>
                <resource>
                    <directory>src/main/resourcesdirectory>
                    <includes>
                        <include>**/*.propertiesinclude>
                        <include>**/*.xmlinclude>
                    includes>
                    <filtering>truefiltering>
                resource>
    
                <resource>
                    <directory>src/main/javadirectory>
                    <includes>
                        <include>**/*.propertiesinclude>
                        <include>**/*.xmlinclude>
                    includes>
                    <filtering>truefiltering>
                resource>
            resources>
        build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    java文件

    • 工具类

    • SqlSession 类似 PrepareStatement+Connection,用完需要关闭
    • 获取SqlSession 流程
      xml-SqlSessionFactoryBuilder-sqlSessionFactory-SqlSession
        private static SqlSessionFactory sqlSessionFactory = null;
    //    通过xml获取 sqlSessionFactory
        static {
            try (InputStream is = Resources.getResourceAsStream("mybatis-config.xml")) {
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public static SqlSession getSqlSession(){
            return sqlSessionFactory.openSession();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • DAO接口

    • 表所对应的具体类

    使用

    看起来在调用接口,因为xml已经绑定接口,所以会调用xml中的sql语句

            TestMapper tm = ss.getMapper(TestMapper.class);
    //        传入参数
            List<testObj> list = tm.getTestById(1);
    
    • 1
    • 2
    • 3

    错误

    • .和/

    被这个折磨了很久,类用. 文件用/

    • error binding…

    没有在核心xml注册xml

    • not found… 并且target文件中没有对应xml

    忘记在pom.xml配置

      <build>
            <resources>
                <resource>
                    <directory>src/main/resourcesdirectory>
                    <includes>
                        <include>**/*.propertiesinclude>
                        <include>**/*.xmlinclude>
                    includes>
                    <filtering>truefiltering>
                resource>
    
                <resource>
                    <directory>src/main/javadirectory>
                    <includes>
                        <include>**/*.propertiesinclude>
                        <include>**/*.xmlinclude>
                    includes>
                    <filtering>truefiltering>
                resource>
            resources>
        build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

  • 相关阅读:
    嵌入式BI的精解与探索
    什么是数据仓库,解释数据仓库的结构和ETL过程
    【Hadoop】HDFS API 操作大全
    服务器遭受攻击之后的常见思路
    云原生Docker网络管理
    JavaSE | 初始Java(十) | 继承和多态
    条件渲染(v-if、v-show)、列表渲染(v-for)、列表中key的原理和作用、列表过滤(filter)、列表排序(sort)
    Serverless是简化的Kubernetes
    clickhouse运维常用语句
    产品升级!全球尺度下原核基因组关键基因共进化无标题
  • 原文地址:https://blog.csdn.net/qq_51682771/article/details/125910180