Mybatis基本应用

本文详细介绍了Mybatis的基本应用,包括CRUD操作、环境配置和配置解析。在CRUD部分,讲解了namespace、select、insert、update、delete及使用Map作为参数的方法。对于环境配置,讨论了适应多种环境的可能性,并指出默认事务管理和连接池类型。配置解析部分涉及映射器的注册和生命周期,强调了正确配置的重要性以避免并发问题。
摘要由CSDN通过智能技术生成

Mybatis

1.CRUD

1、namespace

namespace中的包名要和Dao/mapper接口的包名一样

2、select

选择,查询语句

  • id:就是对应的namespace中的方法名;

  • resultType:Sql语句执行的返回值

  • parameterType:参数类型

    1.编写接口

       //根据ID查询用户
        User getUserById(int id);
    

    2.编写对应的mapper中的sql语句

    <select id="getUserById" parameterType="int" resultType="com.lwl.pojo.User">
        select *from mybatis.user where id=#{id}
    </select>
    
    

    3.测试

        @Test
         public void getUserById()
            {
                SqlSession sqlSession = MybatisUtils.getSqlSession();
                UserMapper mapper = sqlSession.getMapper(UserMapper.class);
                User userById = mapper.getUserById(1);
                System.out.println(userById);
                sqlSession.close();
            }
            //增删改需要提交事务
    

3、Insert

 <insert id="addUser" parameterType="com.lwl.pojo.User">
      insert into mybatis.user(id,name,pwd) values(#{id},#{name},#{pwd})
    </insert>

4、Update

  <update id="updateUser" parameterType="com.lwl.pojo.User">
     update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id} ;
    </update>

5、Delete

  <delete id="deleteUser" parameterType="int">
    delete from mybatis.user where id=#{id};
    </delete>

注意点

  • 增删改需要提交事物

6、万能Map

假设,我们的实体类,或者数据库中的表,字段或者参数过多,我们应当考虑使用Map!

   int addUser(Map<String,Object> map);
 <insert id="addUser" parameterType="map">
        insert into mybatis.user(id,name,pwd) values(#{userId},#{userName},#{userPassword})
    </insert>
public void addUser2(){
       SqlSession sqlSession = MybatisUtils.getSqlSession();
       UserMapper mapper = sqlSession.getMapper(UserMapper.class);

       Map<String, Object> map = new HashMap<String, Object>();
       map.put("userId",5);
       map.put("userName","hello");
       map.put("userPassword","5454");

       mapper.addUser(map);

         sqlSession.close();
   }

Map中传递参数,直接在sql中取出key即可!【parameterType=“map”】

对象传递参数,直接在sql中取出对象的属性即可!【parameterType=“Object”】

只有一个基本类型的情况下,可以直接在sql中取到

多个参数用Map,或者注解!

7、模糊查询拓展

1、java代码执行的时候,传递通配符%%

  List<User> userlist = mapper.getUserLike("%李%");

2、在sql拼接中使用通配符!

select * from mybatis.user where name like "%"#{value}"%"

2、环境配置(environment)

Mybatis可以配置成适应多种环境

不过要记住:尽管可以配置多个环境,但每个SqlSessionFactory实例只能选择一种环境

学会配置多套运行环境

Mybatis默认的事务就是JDBC ,连接池:POOLED

3、配置解析

1、映射器

MapperRegistry:注册绑定我们的Mapper文件;

方式一:

<!--每一个Mapper.xml都需要在MyBatis核心配置文件中注册!>
<mappers>
<mapper resources="com/lwl/dao/UserMapper.xml"/>
</mappers>

方式二:使用class文件绑定

<!--每一个Mapper.xml都需要在MyBatis核心配置文件中注册!>
<mappers>
<mapper resources="com.lwl.dao.UserMapper"/>
</mappers>

注意点

  • 接口和他的Mapper配置文件必须同名!
  • 接口和他的Mapper配置文件必须在同一个包下

方式三:使用包进行注入绑定

<!--每一个Mapper.xml都需要在MyBatis核心配置文件中注册!>
<mappers>
<package name="com.lwl.dao"/>
</mappers>

注意点

  • 接口和他的Mapper配置文件必须同名!
  • 接口和他的Mapper配置文件必须在同一个包下

2、生命周期和作用域

在这里插入图片描述

生命周期,和作用域,是至关重要的。因为错误的使用会导致一系列非常严重的并发问题

SqlSessionFactoryBuilder:

  • 一旦创建了SqlSessionFactory,就不需要他了

  • 局部变量

    SqlSessionFactory:

    • 说白了就是数据库:数据库连接池
    • SQL session Factory一旦被创建就应该在应用的运行期间一直存在,没有任何理由丢弃它或者重新创建另一个实例。

    在这里插入图片描述

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

<!--核心配置文件-->
<configuration>
    <properties resource="db.properties"/>

    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <!--是否开启驼峰名命-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>


<insert id="addBlog" parameterType="blog">
    insert into mybatis.blog(id, title, author, creat_time, views)
    values(#{id},#{title}, #{author}, #{creatTime}, #{views})
</insert>

    <sql id="if-title-author">
        <if test="title!=null">
            and title=#{title}
        </if>
        <if test="author!=bull">
            and author=#{author}
        </if>
    </sql>


    <select id="queryBlogIF" parameterType="map" resultType="blog">
        select * from mybatis.blog
        <where>
        <include refid="if-title-author">
        </include>
        </where>
    </select>

</mapper>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值