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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2020-04-23 开发冲刺六