mybatis-动态SQL,SQL标签
2021/4/23 19:28:35
本文主要是介绍mybatis-动态SQL,SQL标签,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
BlogMapper.java
package dao; import pojo.Blog; import java.util.List; import java.util.Map; public interface BlogMapper { //添加博客 public int addBlog(Blog blog); //查询博客 public List<Blog> queryBlogIF(Map map); public List<Blog> queryBlogChoose(Map map); public List<Blog> queryBlogForeach(Map map); //更新博客 public int updateBlog(Map map); }
BlogMapper.xml
<?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=绑定一个对应的Dao/Mapper接口--> <mapper namespace="dao.BlogMapper"> <!--SQL片段,可多次引用SQL片,实现代码的复用--> <sql id="if_title_author"> <if test="title != null "> and title = #{title} </if> <if test="author != null"> and author = #{author} </if> </sql> <!--paraeterType传入的参数类型--> <!--resultType返回的参数类型--> <insert id="addBlog" parameterType="Blog"> insert into blog values(#{id},#{title},#{author},#{createTime},#{views}) </insert> <select id="queryBlogIF" parameterType="map" resultType="Blog"> select * from blog <where> <include refid="if_title_author"></include> </where> </select> <select id="queryBlogChoose" parameterType="map" resultType="Blog"> select * from blog <where> <choose> <when test="title != null"> and title = #{title} </when> <when test="author != null"> and author = #{author} </when> <otherwise> and views =#{views} </otherwise> </choose> </where> </select> <update id="updateBlog" parameterType="map"> update blog <set> <if test="title != null"> title = #{title}, </if> <if test="author != null"> author = #{author} </if> </set> where id = #{id} </update> <!-- /* <trim>很重要的了啊</trim> */ --> <select id="queryBlogForeach" parameterType="map" resultType="Blog"> select * from blog <where> <foreach collection="ids" item="id" open="and (" close=")" separator="or"> id= #{id} </foreach> </where> </select> </mapper>
MyTest.java
package test; import dao.BlogMapper; import org.apache.ibatis.session.SqlSession; import org.apache.log4j.Logger; import org.junit.Test; import pojo.Blog; import utils.IdUtils; import utils.MybatisUtils; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; public class MyTest { static Logger logger = Logger.getLogger(MyTest.class); //LogDemo为相关的类 @Test public void test() { SqlSession sqlSession= MybatisUtils.getSqlSession(); BlogMapper blogMapper=sqlSession.getMapper(BlogMapper.class); Blog blog=new Blog(); blog.setId(IdUtils.getId()); blog.setCreateTime(new Date()); blog.setAuthor("mkz3"); blog.setTitle("mkz3"); blog.setViews(0); int flag=blogMapper.addBlog(blog); sqlSession.commit(); sqlSession.close(); } @Test public void queryBlogIF() { SqlSession sqlSession= MybatisUtils.getSqlSession(); BlogMapper blogMapper=sqlSession.getMapper(BlogMapper.class); HashMap map = new HashMap(); map.put("title","md"); //logger.info("测试"); map.put("author","mkz1"); List<Blog> blogList=blogMapper.queryBlogIF(map); for (Blog blog : blogList) { System.out.println(blog); } sqlSession.close(); } @Test public void queryBlogCHOOSE() { SqlSession sqlSession= MybatisUtils.getSqlSession(); BlogMapper blogMapper=sqlSession.getMapper(BlogMapper.class); HashMap map = new HashMap(); //map.put("title","md"); //logger.info("测试"); //map.put("author","mkz1"); map.put("views","0"); List<Blog> blogList=blogMapper.queryBlogChoose(map); for (Blog blog : blogList) { System.out.println(blog); } sqlSession.close(); } @Test public void updateBlog() { SqlSession sqlSession= MybatisUtils.getSqlSession(); BlogMapper blogMapper=sqlSession.getMapper(BlogMapper.class); HashMap map = new HashMap(); map.put("id","ad5bc0db43b343cca7971a0eaa981129"); //logger.info("测试"); map.put("title","我是苗可卓"); //map.put("views","0"); blogMapper.updateBlog(map); sqlSession.commit(); sqlSession.close(); } @Test public void queryBlogForeach() { SqlSession sqlSession=MybatisUtils.getSqlSession(); BlogMapper blogMapper=sqlSession.getMapper(BlogMapper.class); HashMap map=new HashMap(); ArrayList<Integer> ids= new ArrayList<Integer>(); ids.add(1); ids.add(2); map.put("ids",ids); List<Blog> blogList=blogMapper.queryBlogForeach(map); for (Blog blog : blogList) { System.out.println(blog); } sqlSession.close(); } }
mybatis官网动态SQL官方文档连接:https://mybatis.org/mybatis-3/zh/dynamic-sql.html
这篇关于mybatis-动态SQL,SQL标签的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-29RocketMQ底层原理资料详解:新手入门教程
- 2024-11-29RocketMQ源码资料解析与入门教程
- 2024-11-29[开源]6.1K star!这款电视直播源神器真的太赞啦!
- 2024-11-29HTTP压缩入门教程:轻松提升网页加载速度
- 2024-11-29JWT开发入门指南
- 2024-11-28知识管理革命:文档软件的新玩法了解一下!
- 2024-11-28低代码应用课程:新手入门全攻略
- 2024-11-28哪些办公软件适合团队协作,且能够清晰记录每个阶段的工作进展?
- 2024-11-28全栈低代码开发课程:零基础入门到初级实战
- 2024-11-28拖动排序课程:轻松掌握课程拖动排序功能