动态SQL环境搭建以及常用标签

1、导包(maven)

2、编写配置文件

<?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>
    <properties resource="db.properties"/>

    <settings>
        <setting name="logImpl" value="LOG4J"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <typeAliases>
        <package name="com.kuang.pojo"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper class="com.kuang.dao.blogMapper"/>

    </mappers>
</configuration>

3、编写实体类

package com.kuang.pojo;

import lombok.Data;

import java.util.Date;
@Data
public class Blog {
    private String id;
    private String title;
    private String author;
    private Date create_time;
    private int views;
}

4、编写实体类对应Mapper接口和Mapper.xml文件

blogMapper

package com.kuang.dao;

import com.kuang.pojo.Blog;

import java.util.List;
import java.util.Map;

public interface blogMapper {

    int insertBlog(Blog blog);

    List<Blog> selectAllIf(Map map);

    List<Blog> selectAllChoose(Map map);


    int update(Map map);
}

blogMapper.xml(常见标签 if、set、where、choose、when)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kuang.dao.blogMapper">


    <insert id="insertBlog">
        insert into mybatis.blog (id, title, author, create_time, views)
        values (#{id}, #{title}, #{author}, #{create_time}, #{views})
    </insert>

    <select id="selectAll" resultType="Blog">
        select * from blog
        <where>
            <if test="title != null">
                title=#{title}
            </if>
            <if test="author!=null">
                and author=#{author}
            </if>
        </where>

    </select>


    <select id="selectAllChoose" resultType="Blog">
        select * from blog
        <where>
            <choose>
                <when test="title!=null">
                    title=#{title}
                </when>
                <when test="author!=null">
                    and author=#{author}
                </when>
                <otherwise>
                    and views=#{views}
                </otherwise>
            </choose>
        </where>
    </select>

    <update id="update" parameterType="map">
        update blog
        <set>
            <if test="title!=null">
                title=#{title},
            </if>
            <if test="author!=null">
                author=#{author}
            </if>
            where id=#{id}
        </set>
    </update>

</mapper>

5、测试

package com.lisai;

import com.kuang.dao.blogMapper;
import com.kuang.pojo.Blog;
import com.kuang.utils.IDUtils;
import com.kuang.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class TestDemo {

    @Test
    public void Test01(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        blogMapper mapper = sqlSession.getMapper(blogMapper.class);

        Blog blog = new Blog();
        blog.setId(IDUtils.getId());
        blog.setTitle("java");
        blog.setAuthor("狂神");
        blog.setCreate_time(new Date());
        blog.setViews(88);
        mapper.insertBlog(blog);

        blog.setId(IDUtils.getId());
        blog.setTitle("Java");
        mapper.insertBlog(blog);

        blog.setId(IDUtils.getId());
        blog.setTitle("Spring");
        mapper.insertBlog(blog);

        blog.setId(IDUtils.getId());
        blog.setTitle("微服务");
        mapper.insertBlog(blog);

        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void Test02(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        blogMapper mapper = sqlSession.getMapper(blogMapper.class);
        HashMap map = new HashMap();
//        map.put("title","java");
        map.put("author","狂神");
        List<Blog> blogs = mapper.selectAllIf(map);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

    @Test
    public void Test03(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        blogMapper mapper = sqlSession.getMapper(blogMapper.class);
        HashMap map = new HashMap();
        map.put("views",88);
        map.put("author","狂神");
        map.put("title","java");
        List<Blog> blogs = mapper.selectAllChoose(map);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

    @Test
    public void Test04(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        blogMapper mapper = sqlSession.getMapper(blogMapper.class);
        HashMap map = new HashMap();

        map.put("title","李赛");
        map.put("author","陈辰龙");
        map.put("id","2360d44628b341c392d4cf3b919880c1");
        mapper.update(map);
        sqlSession.commit();
        sqlSession.close();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值