MyBatis

官网:https://mybatis.org/mybatis-3/zh/index.html

快速入门

 

  1. 创建user表,添加数据
  2. 创建模块,导入坐标
  3. 编写Mybatis核心配置文件-->替换链接信息,解决硬编码问题
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    <?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:///mybatis?useSSL=false"/>
                    <property name="username" value="root"/>
                    <property name="password" value="root"/>
                </dataSource>
            </environment>
        </environments>
        <mappers>
    <!--        加载sql的映射文件-->
            <mapper resource="UserMapper.xml"/>
        </mappers>
    </configuration>
  4. 编写SQL映射文件-->统一管理sql语句,解决硬编码问题
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <?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 namespace="test">
        <select id="selectAll" resultType="com.itheima.pojo.User">
            select * from tb_user;
        </select>
    </mapper>
  5. 编码
    1. 定义POJO类
    2. 加载核心文件,获取SqlSessionFactory
    3. 获取SqlSession对象,执行SQL语句
  6. 释放资源
    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
    package com.itheima;
     
    import com.itheima.pojo.User;
    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;
    import java.util.List;
     
    /**
     * mybatis快速入门
     */
    public class MybatisDemo {
        public static void main(String[] args) throws IOException {
            //1.加载mybatis核心配置文件,获取sqlSessionFactory
     
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
     
            //2.获取sqlSession对象,用它执行sql
            SqlSession sqlSession = sqlSessionFactory.openSession();
            //3.执行sql
            List<User> users = sqlSession.selectList("test.selectAll");
     
            System.out.println(users);
            sqlSession.close();
        }
    }

 

解决sql映射文件警告信息

 

 Mapper代理开发

目的:解决原生方式中硬编码

 

简化后期执行SQL

1
2
//3.执行sql
        List<User> users = sqlSession.selectList("test.selectAll");

  

1
2
3
4
//3.获取接口代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//4.执行方法,执行sql
userMapper.selectAll();

  

步骤:

  1. 定义SQL映射文件同名的Mapper接口,并且将Mapper接口和SQL映射文件放置在同一目录下(将SQL映射文件放在resource下与Mapper接口相同的包中,compiler后生成的classes中即在同一目录)

     
  2. 设置SQL映射文件的namespace属性为Mapper接口全限定名
    1
    2
    3
    4
    5
    6
    <!--namespace名称空间-->
    <mapper namespace="com.itheima.mapper.UserMapper">
        <select id="selectAll" resultType="com.itheima.pojo.User">
            select * from tb_user;
        </select>
    </mapper>

      

  3. 在Mapper接口中定义方法,方法名就是SQL映射文件中sql语句的id,并保持参数类型和返回值类型一致

     

     

    ** 不要忘记改config中映射文件的路径 **

     

     

     

  4. 编码
    1. 通过SqlSession的getMapper方法获取Mapper接口的代理对象
    2. 调用对应方法完成sql的执行
      1
      2
      3
      4
      //3.获取接口代理对象
      UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
      //4.执行方法,执行sql
      List<User> users = userMapper.selectAll();

        

  细节:如果Mapper接口名称和SQL映射文件名称相同,并在同一目录下,则可以使用包扫描的方式简化SQL映射文件的而加载

1
2
3
4
5
6
    <mappers>
<!--        加载sql的映射文件-->
<!--        <mapper resource="com/itheima/mapper/UserMapper.xml"/>-->
<!--       Mapper代理方式-->
        <package name="com.itheima.mapper"/>
    </mappers>

  

  MyBatis核心配置文件详解

 

 

 

 

1
2
3
4
5
6
7
8
9
10
<mapper namespace="com.itheima.mapper.BrandMapper">
<!-- SQL片段 -->
    <sql id="brand_column">
        id,brand_name brandName,company_name companyName,ordered,description,status
    </sql>
    <select id="selectAll" resultType="Brand">
        select <include refid="brand_column"/>
        from tb_brand;
    </select>
</mapper>

  

<!-- 
resultMap:
1.定义resultMap标签
2.<select>标签中使用resultMap属性替换resultType属性
id:唯一标识
type:映射的类型,支持别名

-->
<resultMap id="brandResultMap" type="Brand">
<!--
id:完成主键字段映射
column:表的列名
property:实体类的属性名
result:完成一般字段的映射
column:表的列名
property:实体类的属性名
-->
<result column="brand_name" property="brandName"></result>
<result column="company_name" property="companyName"></result>
</resultMap>
<select id="selectAll" resultMap="brandResultMap">
select *
from tb_brand;
</select>
</mapper>

  

配置文件完成增删改查

    <resultMap id="brandResultMap" type="Brand">
<result column="brand_name" property="brandName"></result>
<result column="company_name" property="companyName"></result>
</resultMap>
<select id="selectAll" resultMap="brandResultMap">
select *
from tb_brand;
</select>
<select id="selectById" parameterType="int" resultMap="brandResultMap">
-- select * from ${tb_brand} where id = #{id};
-- select * from tb_brand where id 小于号 #{id};
-- select * from tb_brand where id &lt; #{id};
select *
from tb_brand
where id
<![CDATA[
<=
]]>
#{id}
</select>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

 

posted @   格兰芬多剁剁剁  阅读(34)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示