• Mybatis------Mybatis基本操作


    入门_MyBatis中文网

    第一步,写依赖,导入jar包。提供技术支持。

    创建模块,导入坐标,在创建好的模块中的 pom.xml 配置文件中添加依赖的坐标

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.mybatisgroupId>
    4. <artifactId>mybatisartifactId>
    5. <version>3.5.5version>
    6. dependency>
    7. <dependency>
    8. <groupId>mysqlgroupId>
    9. <artifactId>mysql-connector-javaartifactId>
    10. <version>8.0.16version>
    11. dependency>
    12. <dependency>
    13. <groupId>junitgroupId>
    14. <artifactId>junitartifactId>
    15. <version>4.13version>
    16. <scope>testscope>
    17. dependency>
    18. <dependency>
    19. <groupId>org.slf4jgroupId>
    20. <artifactId>slf4j-apiartifactId>
    21. <version>1.7.20version>
    22. dependency>
    23. <dependency>
    24. <groupId>ch.qos.logbackgroupId>
    25. <artifactId>logback-classicartifactId>
    26. <version>1.2.3version>
    27. dependency>
    28. <dependency>
    29. <groupId>ch.qos.logbackgroupId>
    30. <artifactId>logback-coreartifactId>
    31. <version>1.2.3version>
    32. dependency>
    33. dependencies>

    第二部编写Mybatis的配置文件

    在resource文件夹下,新建一个mybatis-config.xml的文件

    然后编写xml文件,首先呢,我们写一下约束

    1. configuration
    2. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    3. "http://mybatis.org/dtd/mybatis-3-config.dtd">

     然后再configuration里写上那啥。

    1. configuration
    2. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    3. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    4. <configuration>
    5. <environments default="development">
    6. <environment id="development">
    7. <transactionManager type="JDBC"/>
    8. <dataSource type="POOLED">
    9. <property name="driver" value="${driver}"/>
    10. <property name="url" value="${url}"/>
    11. <property name="username" value="${username}"/>
    12. <property name="password" value="${password}"/>
    13. dataSource>
    14. environment>
    15. environments>
    16. <mappers>
    17. <mapper resource="org/mybatis/example/BlogMapper.xml"/>
    18. mappers>
    19. configuration>

    看看官方文档:

    1. default="development"> //配置数据源,里面可以有很多environment
    2. <environment id="development"> //其中一个environment
    3. <transactionManager type="JDBC"/> //
    4. <dataSource type="POOLED"> //
    5. //数据库信息
    6. <property name="driver" value="${driver}"/>
    7. <property name="url" value="${url}"/>
    8. <property name="username" value="${username}"/>
    9. <property name="password" value="${password}"/>
    10. dataSource>
    11. environment>
    1. <mappers>
    2. <mapper resource="UserMapper.xml"/>
    3. mappers>

    这里的是啥呢?这就是第三步,在resource文件夹下面创建sql的文件mapper,专门写sql的文件。为了高内聚,低耦合

    文件名必须与 这里的文件名一致!!!!!!!

    1. <mappers>
    2. <mapper resource="Mapper/UserMapper.xml"/>
    3. mappers>

    然后我们编写xml文件

    首先是添加约束:

    1. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    2. <mapper namespace="test">
    3. <select id="selectAll" resultType="com.itheima.User">
    4. select * from tb_user;
    5. select>
    6. mapper>

    id是给这次查询取个名字,而 resultType是返回的结果,要赋值给实体类。

    resultType="com.itheima.User"> 因此这边要创建一个实体类

     这里面的私有字段要和你在数据库里面的一样。

     在 com.itheima 包下编写 MybatisDemo 测试类

    1. public class MyBatisDemo {
    2. public static void main(String[] args) throws IOException {
    3. //1. 加载mybatis的核心配置文件
    4. String resource = "mybatis-config.xml";
    5. //读取配置文件
    6. InputStream inputStream = Resources.getResourceAsStream(resource);
    7. //通过配置文件生产sqlSessionFactory
    8. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    9. //2. 获取SqlSession对象,用它来执行sql
    10. SqlSession sqlSession = sqlSessionFactory.openSession();
    11. //3. 执行sql
    12. List users = sqlSession.selectList("test.selectAll"); //参数是一个字符串,该字符串必须是映射配置文件的namespace.id
    13. System.out.println(users);
    14. //4. 释放资源
    15. sqlSession.close();
    16. }
    17. }

    最后在configuration里添加这句话

    1. <typeAliases>
    2. <package name="com.itheima.Domain">
    3. typeAliases>
  • 相关阅读:
    计算机毕业设计ssm+vue基本微信小程序的小学生兴趣延时班预约小程序
    LeetCode:1465. 切割后面积最大的蛋糕(C++)
    LeetCode - 哈希表专题
    虹科示波器 | 汽车免拆检修 | 2021款广汽丰田威兰达PHEV车发动机故障灯异常点亮
    【算法训练-字符串 三】最长公共子串、最长公共子序列
    C#,数值计算——插值和外推,PolCoef的计算方法与源程序
    《羊了个羊》H5游戏开发挑战,3小时LAYA引擎制作核心玩法
    Java agent 使用详解
    图论16-拓扑排序
    Python Flask 离线环境的搭建
  • 原文地址:https://blog.csdn.net/makabaka12138/article/details/126178236