Mybatis

1.Mybtis的快速开发

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

看word

解决SQL映射文件的警告提示:

在入门案例映射配置文件中存在报红的情况。问题如下:
在这里插入图片描述

  • 产生的原因:Idea和数据库没有建立连接,不识别表信息。但是大家一定要记住,它并不影响程序的执行。
  • 解决方式:在Idea中配置MySQL数据库连接。

IDEA中配置MySQL数据库连接
1.点击IDEA右边框的 Database ,在展开的界面点击 + 选择 Data Source ,再选择 MySQL
在这里插入图片描述
2.在弹出的界面进行基本信息的填写
在这里插入图片描述


2.Mapper代理开发

  • 解决原生方式中的硬编码
  • 简化后期执行SQL

使用Mapper代理要求
1.定义与SQL映射文件同名的Mapper接口,并且将Mapper接口和SQL映射文件放置在同一目录下。如下图:
在这里插入图片描述
2.设置SQL映射文件的namespace属性为Mapper接口全限定名
在这里插入图片描述
3.在 Mapper接口中定义方法,方法名就是SQL映射文件中sql语句的id,并保持参数类型和返回值类型一致
在这里插入图片描述
在这里插入图片描述

/**
 * Mybatis 代理开发
 */
public class MyBatisDemo2 {

    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  -----当为true 时 自动提交 
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 执行sql
        //3.1 获取UserMapper接口的代理对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.selectAll();

        System.out.println(users);
        //4. 释放资源
        sqlSession.close();
    }
}

注意: -------别忘了加载sql映射文件
如果Mapper接口名称和SQL映射文件名称相同,并在同一目录下,则可以使用包扫描的方式简化SQL映射文件的加载。也就是将核心配置文件的加载映射配置文件的配置修改为
在这里插入图片描述

注意:
在这里插入图片描述
一定要有 / 进行建立



3.Mybatis核心配置文件

注意: 习惯上命名为mybatis-config.xml,这个文件名仅仅只是建议,并非强制要求。将来整合Spring
之后,这个配置文件可以省略,所以大家操作时可以直接复制、粘贴。
核心配置文件主要用于配置连接数据库的环境以及MyBatis的全局配置信息
核心配置文件存放的位置是src/main/resources目录下

1.在核心配置文件的 environments 标签中其实是可以配置多个 environment ,使用 id 给每段环境起名,在 environments 中使用 default='环境id' 来指定使用哪儿段配置。我们一般就配置一个 environment 即可。

<?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>
	<!--
         MyBatis核心配置文件中,标签的顺序:
         properties?,settings?,typeAliases?,typeHandlers?,
         objectFactory?,objectWrapperFactory?,reflectorFactory?,
         plugins?,environments?,databaseIdProvider?,mappers?
	-->
	<!--引入properties文件-->
	<properties resource="jdbc.properties" />
	<!--设置类型别名-->
	<typeAliases>
	<!--
		typeAlias:设置某个类型的别名
		属性:
			type:设置需要设置别名的类型
			alias:设置某个类型的别名,若不设置该属性,那么该类型拥有默认的别名,即类名且不区分大小写
	-->
		<!--<typeAlias type="com.atguigu.mybatis.pojo.User"></typeAlias>-->
		<!--以包为单位,将包下所有的类型设置默认的类型别名,即类名且不区分大小写-->
		<package name="com.atguigu.mybatis.pojo"/>
	</typeAliases>
	
	<!--
		environments:配置多个连接数据库的环境
		属性:
			default:设置默认使用的环境的id
	-->
	<environments default="development">
	 <!--
		environment:配置某个具体的环境
		属性:
			id:表示连接数据库的环境的唯一标识,不能重复
	-->
	<environment id="development">
		<!--
			transactionManager:设置事务管理方式
			属性:
				type="JDBC|MANAGED"
				JDBC:表示当前环境中,执行SQL时,使用的是JDBC中原生的事务管理方式,事务的提交或回滚需要手动处理
				MANAGED:被管理,例如Spring
		-->
	<transactionManager type="JDBC"/>
			<!--
				dataSource:配置数据源
				属性:
              type:设置数据源的类型
              type="POOLED|UNPOOLED|JNDI"
                 POOLED:表示使用数据库连接池缓存数据库连接
                 UNPOOLED:表示不使用数据库连接池
                 JNDI:表示使用上下文中的数据源
			-->
				<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>
			<environment id="test">
          <transactionManager type="JDBC"/>
          <dataSource type="POOLED">
             <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
             <property name="url"value="jdbc:mysql://localhost:3306/ssmserverTimezone=UTC"/>
             <property name="username" value="root"/>
             <property name="password" value="123456"/>
			</dataSource>
		</environment>
	</environments>
	<!--引入映射文件-->
	<mappers>
	<!--<mapper resource="mappers/UserMapper.xml"/>-->
	<!--
      以包为单位引入映射文件
         要求:
         1、mapper接口所在的包要和映射文件所在的包一致
         2、mapper接口要和映射文件的名字一致
	-->
		<package name="com.atguigu.mybatis.mapper"/>
	</mappers>
</configuration>

2.类型别名
在映射配置文件中的 resultType 属性需要配置数据封装的类型(类的全限定名)。而每次这样写是特别麻烦的,Mybatis 提供了 类型别名(typeAliases) 可以简化这部分的书写。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.创建模板
在这里插入图片描述



4.配置文件完成增删改查

   1.Mybitsx安装

在这里插入图片描述

   2.查询所有数据

    1.当数据库中的名字和类中的字段不同时,没办法将数据进行封装解决版办法

  • 给字段起别名

1.我们可以在写sql语句时给这两个字段起别名,将别名定义成和属性名一致即可。

<select id="selectAll" resultType="brand">
    select
    id, brand_name as brandName, company_name as companyName, ordered, description, status
    from tb_brand;
</select>

2.SQL片段:

将需要复用的SQL片段抽取到 `sql` 标签中
<sql id="brand_column">
	id, brand_name as brandName, company_name as companyName, ordered, description, status
</sql>
id属性值是唯一标识,引用时也是通过该值进行引用。
在原sql语句中进行引用

使用 `include` 标签引用上述的 SQL 片段,而 `refid` 指定上述 SQL 片段的id值。
<select id="selectAll" resultType="brand">
    select
    <include refid="brand_column" />
    from tb_brand;
</select>

3.使用resultMap定义字段和属性的映射关系
在映射配置文件中使用resultMap定义 字段 和 属性 的映射关系

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

注意:在上面只需要定义 字段名 和 属性名 不一样的映射,而一样的则不需要专门定义出来。

SQL语句正常编写

<select id="selectAll" resultMap="brandResultMap">
    select *
    from tb_brand;
</select>

    2.参数占位符

1.#{} :执行SQL时,会将 #{} 占位符替换为?,将来自动设置参数值。从上述例子可以看出使用#{} 底层使用的是 `PreparedStatement

2.${} :拼接SQL。底层使用的是 Statement,会存在SQL注入问题。如下图将 映射配置文件中的 #{} 替换成 ${} 来看效果

<select id="selectById"  resultMap="brandResultMap">
    select *
    from tb_brand where id = ${id};
</select>

    3.parameterType

对于有参数的mapper接口方法,我们在映射配置文件中应该配置 ParameterType 来指定参数类型。只不过该属性都可以省略。如下图:

<select id="selectById" parameterType="int" resultMap="brandResultMap">
    select *
    from tb_brand where id = ${id};
</select>

    4.SQL语句中特殊字段处理

因为映射配置文件是xml类型的问题,而 > < 等这些字符在xml中有特殊含义,所以此时我们需要将这些符号进行转义,可以使用以下两种方式进行转义

  • 转义字符
  • <![CDATA[内容]]> -----输入CD即可
<![CDATA[

]]>


   3.条件查询

    1.多条件查询

<select id="selectByCondition" resultMap="brandResultMap">
    select *
    from tb_brand
    where status = #{status}
    and company_name like #{companyName}
    and brand_name like #{brandName}
</select>

多参数传递
1.使用 @Param("参数名称") 标记每一个参数,在映射配置文件中就需要使用 #{参数名称} 进行占位

List selectByCondition(@Param(“status”) int status, @Param(“companyName”) String companyName,@Param(“brandName”) String brandName);
其中这个Param中()为sql语句中的参数

2.将多个参数封装成一个 实体对象 ,将该实体对象作为接口的方法参数。该方式要求在映射配置文件的SQL中使用 #{内容} 时,里面的内容必须和实体类属性名保持一致。

List selectByCondition(Brand brand);
其中这个brand(对象)中对象的名字要跟SQL的参数名应该一样

3.将多个参数封装到map集合中,将map集合作为接口的方法参数。该方式要求在映射配置文件的SQL中使用 #{内容} 时,里面的内容必须和map集合中键的名称一致。

List selectByCondition(Map map);
SQL中的参数名和map集合中的 的名称对应上

    2.动态条件条件查询

SQL语句可以根据我的需求进行变换

  • if 标签和 where
    1.IF
    test 属性:逻辑表达式
  <select id="seleccondiction" resultMap="brandResultMap">
        select *
        from tb_brand
            <where>
                <if test="status!=null">
                   and status = #{status}
                </if>
                <if test="companyName!=null and companyName!='' ">
                   and company_name like #{companyName}
                </if>
                <if test="brandName!=null and brandName!='' ">
                   and brand_name like #{brandName}
                </if>
            </where>
    </select>

如上的这种SQL语句就会根据传递的参数值进行动态的拼接。如果此时status和companyName有值那么就会值拼接这两个条件。
在这里插入图片描述

select * from tb_brand where and company_name like ? and brand_name like ?

2.WHERE

  • 替换where关键字
  • 会动态的去掉第一个条件前的 and
  • 如果所有的参数没有值则不加where关键字
<select id="selectByCondition" resultMap="brandResultMap">
    select *
    from tb_brand
    <where>
        <if test="status != null">
            and status = #{status}
        </if>
        <if test="companyName != null and companyName != '' ">
            and company_name like #{companyName}
        </if>
        <if test="brandName != null and brandName != '' ">
            and brand_name like #{brandName}
        </if>
    </where>
</select>

    3.单条件动态查询

choose(when,otherwise)标签 实现, 而 choose 标签类似于Java 中的switch语句。
当都没有时会执行otherwise

    select * from  tb_brand
        <where>
            <choose>
                <when test="status!=null">
                    status = #{status}
                </when>
                <when test="companyName!=null and companyName!='' ">
                    company_name like #{companyName}
                </when>
                <when test="brandName!=null and brandName!='' ">
                    brand_name like #{brandName}
                </when>
            </choose>
        </where>
    </select>


   4.添加

<insert id="add">
    insert into tb_brand (brand_name, company_name, ordered, description, status)
    values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
</insert>

主键返回
在 insert 标签上添加如下属性: 添加在sql映射文件的insert标签上

  • useGeneratedKeys:是够获取自动增长的主键值。true表示获取
  • keyProperty :指定将获取到的主键值封装到哪儿个属性里

   5.修改

    1.修改全部字段

<insert id="add" useGeneratedKeys="true" keyProperty="id">
    insert into tb_brand (brand_name, company_name, ordered, description, status)
    values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
</insert>

    2.修改动态字段

set 标签可以用于动态包含需要更新的列,忽略其它不更新的列。

<update id="update">
    update tb_brand
    <set>
        <if test="brandName != null and brandName != ''">
            brand_name = #{brandName},
        </if>
        <if test="companyName != null and companyName != ''">
            company_name = #{companyName},
        </if>
        <if test="ordered != null">
            ordered = #{ordered},
        </if>
        <if test="description != null and description != ''">
            description = #{description},
        </if>
        <if test="status != null">
            status = #{status}
        </if>
    </set>
    where id = #{id};
</update>

   6.删除

    1.删除一个

    <!--    删除-->
    <delete id="Delete">
        delete from  tb_brand where  id=#{id};
    </delete>

    2.批量删除

编写SQL时需要遍历数组来拼接SQL语句。Mybatis 提供了 foreach 标签供我们使用

foreach 标签

用来迭代任何可迭代的对象(如数组,集合)。

  • collection 属性:
    • mybatis会将数组参数,封装为一个Map集合。
      • 默认:array = 数组
      • 使用@Param注解改变map集合的默认key的名称
        例如:void DeleteList(@Param(“ids”) int[] ids);
  • item 属性:本次迭代获取到的元素。
  • separator 属性:集合项迭代之间的分隔符。foreach 标签不会错误地添加多余的分隔符。也就是最后一次迭代不会加分隔符。
  • open 属性:该属性值是在拼接SQL语句之前拼接的语句,只会拼接一次
  • close 属性:该属性值是在拼接SQL语句拼接后拼接的语句,只会拼接一次
   <!--    批量删除-->
    <delete id="DeleteList" >
        delete  from  tb_brand where id
        in 
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
        ;
    </delete>

   7.Mybatis参数传递

Mybatis 接口方法中可以接收各种各样的参数,如下:

  • 多个参数
  • 单个参数:单个参数又可以是如下类型
    • POJO 类型
    • Map 集合类型
    • Collection 集合类型
    • List 集合类型
    • Array 类型
    • 其他类型

1.多个参数

User select(@Param("username") String username,@Param("password") String password);

<select id="select" resultType="user">
	select *
    from tb_user
    where 
    	username=#{username}
    	and password=#{password}
</select>

Mybatis 会将这些参数封装成 Map 集合对象,值就是参数值,而键在没有使用 @Param 注解时有以下命名规则:

  • 以 arg 开头 :第一个参数就叫 arg0,第二个参数就叫 arg1,以此类推。如:

    map.put(“arg0”,参数值1);

    map.put(“arg1”,参数值2);

  • 以 param 开头 : 第一个参数就叫 param1,第二个参数就叫 param2,依次类推。如:

    map.put(“param1”,参数值1);

    map.put(“param2”,参数值2);

结论:以后接口参数是多个时,在每个参数上都使用 @Param 注解。这样代码的可读性更高。

2.单个参数

  • POJO 类型

    直接使用。要求 属性名参数占位符名称 一致

  • Map 集合类型

    直接使用。要求 map集合的键名参数占位符名称 一致

  • Collection 集合类型

    Mybatis 会将集合封装到 map 集合中,如下:

    map.put(“arg0”,collection集合);

    map.put(“collection”,collection集合;

    可以使用 @Param 注解替换map集合中默认的 arg 键名。

  • List 集合类型

    Mybatis 会将集合封装到 map 集合中,如下:

    map.put(“arg0”,list集合);

    map.put(“collection”,list集合);

    map.put(“list”,list集合);

    可以使用 @Param 注解替换map集合中默认的 arg 键名。

  • Array 类型

    Mybatis 会将集合封装到 map 集合中,如下:

    map.put(“arg0”,数组);

    map.put(“array”,数组);

    可以使用 @Param 注解替换map集合中默认的 arg 键名。

    例如:void DeleteList(@Param(“ids”) int[] ids);

    <delete id="DeleteList" >
          delete  from  tb_brand where id
          in   
          <foreach collection="ids" item="id" separator="," open="(" close=")">           collection时循环的数组
              #{id}
          </foreach>
          ;
      </delete>
    
  • 其他类型

    比如int类型,参数占位符名称 叫什么都可以。尽量做到见名知意



5.注解完成增删改查

简单的SQL可以使用注解
难的一般使用配置文件的方式
在这里插入图片描述


6.常见问题

1.SqlSessionFactory工具类抽取

  • 重复代码不利于后期的维护
  • SqlSessionFactory工厂类进行重复创建
    • 就相当于每次买手机都需要重新创建一个手机生产工厂来给你制造一个手机一样,资源消耗非常大但性能却非常低。所以这么做是不允许的。

我们一般创建一个工具类

public class SqlSessionFactoryT {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }
}

使用

  SqlSessionFactory sqlSessionFactory = SqlSessionFactoryT.getSqlSessionFactory();
  SqlSession sqlSession = sqlSessionFactory.openSession();

2.Mybatis-config.xml完整代码

<?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:///dbl?useSSL=false&amp;useServerPrepStmts=true"/>
                <property name="username" value="root"/>
                <property name="password" value="1234"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--扫描mapper-->
        <package name="com.itheima.mapper"/>
    </mappers>
</configuration>

3.resource的路径填写

在这里插入图片描述
在这里插入图片描述

4.注解开发注意事项

虽然使用注解开发,不需要mapper.xml了 ,但是mybatis的配置文件中的加载Sql映射文件和起别名一点都不能少。 -------如果使用注解就没有该映射文件
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值