mybatis

什么是MyBatis?

mybatis是一款优秀的持久层框架,用于简化jdbc开发

持久层:负责将数据保存到数据库的那一层代码
JavaEE三层架构:表现层、业务层、持久层

使用

如果使用Maven来构建项目,则需将下面的依赖代码置于pom.xml中即可

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>版本号</version>
</dependency>
具体使用

1:创建user表,添加数据
2:创建模块,导入坐标
3:编写mybatis核心配置文件,替换连接信息,解决硬编码问题
4:编写sql映射文件,统一管理sql语句,解决硬编码问题
5:编码:定义pojo类、加载核心配置文件获取SqlSessionFactory对象、获取SqlSessionu对象,执行sql语句、释放资源

代码

    @Test
    public void testSelectAll() throws IOException {

        // 1、获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 2、获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 3、获取Mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        // 4、执行方法
        List<Brand> brands = brandMapper.selectAll();
        System.out.println(brands);

        // 5、释放资源
        sqlSession.close();
    }
记录一些增删改查代码
<?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">


<mapper namespace="it.gsw.mapper.BrandMapper">

    <resultMap id="brandResultMap" type="brand">
        <result column="brand_name" property="brandName"/>
        <result column="company_name" property="companyName"/>
    </resultMap>

    <!---->
    <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>

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

    <!--批量删除-->
    <delete id="deleteByIdS">
        delete
        from tb_brand
        where id in(
        <foreach collection="array" item="id" separator=",">
            #{id}
        </foreach>
        )
    </delete>

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

        where id = #{id};
    </update>

    <!--查取全部数据-->
    <select id="selectAll" resultMap="brandResultMap">
        select *
        from tb_brand;
    </select>

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

    <!--条件查询,动态sql-->
    <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>
    <!--单条件的动态查询-->
    <select id="selectByConditionSingle" resultMap="brandResultMap">
        select *
        from tb_brand
        <where>
            <choose>
                <when test="status!=null">and status = #{status}</when>
                <when test="companyName!=null and companyName!='' ">and company_name like #{companyName}</when>
                <when test="brandName!=null and brandName!='' ">and brand_name like #{brandName}</when>
            </choose>
        </where>
    </select>

</mapper>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值