xml映射
| <insert id="insertAuthor"> |
| insert into Author (id,username,password,email,bio) |
| values ( |
| </insert> |
| |
| <update id="updateAuthor"> |
| update Author set |
| username = |
| password = |
| email = |
| bio = |
| where id = |
| </update> |
| |
| <delete id="deleteAuthor"> |
| delete from Author where id = |
| </delete> |
| |
结果集配置
| User selectUserById(int id); |
| |
| |
| <resultMap type="user" id="userMap"> |
| <id column="id" property="id"/> |
| <result column="age" property="age"/> |
| <result column="user_name" property="userName"/> |
| <result column="sex" property="sex"/> |
| <result column="birthday" property="birthday"/> |
| </resultMap> |
| |
| <select id="selectUserById" parameterType="java.lang.Integer" resultMap="userMap" > |
| select * from t_user where id = #{id} |
| </select> |
| |
动态sql
| //符合条件则拼接上if标签的sql语句 |
| <if test="title != null"></if> |
| |
| //switch case |
| <choose> |
| |
| <when test="title != null"></when> |
| |
| <otherwise></otherwise> |
| </choose> |
| |
| //where会在符合条件时自动添加上where 并去除子句中多余的 and 或 or |
| <where> |
| <if test="title != null"> |
| AND title like #{title} |
| </if> |
| </where> |
| |
| //set可用于动态包含需要更新的列,忽略其它不更新的列 |
| update Author |
| <set> |
| <if test="username != null">username=#{username},</if> |
| <if test="password != null">password=#{password},</if> |
| <if test="email != null">email=#{email},</if> |
| <if test="bio != null">bio=#{bio}</if> |
| </set> |
| where id=#{id} |
| |
| //该标签自定义 添加的sql语句 过滤的sql语句;如下表示在需要时会自动在sql语句中添加set |
| <trim prefix="SET" suffixOverrides=","> |
| ... |
| </trim> |
| |
| //foreach对集合进行遍历 |
| <insert id="add"> |
| insert into t_user(user_name, sex, age) values |
| |
| <foreach collection="list" item="u" separator=","> |
| (#{u.userName}, #{u.sex}, #{u.age}) |
| </foreach> |
| </insert> |
| |
| //sql可以用来定义可重用的 SQL 代码片段,以便在其它语句中使用。 参数可以静态地(在加载的时候)确定下来,并且可以在不同的 include 元素中定义不同的参数值 |
| <sql id="userColumns"> ${alias}.id,${alias}.username,${alias}.password </sql> |
| //使用时在xml映射sql中添加该id的标签即可重用代码片段 |
| <include refid="userColumns"> |
| |
视图封装
在视图层vo文件夹下编写封装类
- 例如封装用户表和博客表为BlogUserVo


| public class BlogUserVO { |
| private Integer uId; |
| private String phone; |
| private String password; |
| private String nickname; |
| private String headImg; |
| private String personalSign; |
| private Integer levelId; |
| private Integer bId; |
| private String blogType; |
| private String blogTitle; |
| private String blogPic; |
| private String blogContent; |
| private Date createTime; |
| private Date updateTime; |
| private String isDel; |
| private String isTop; |
| private String status; |
| private String blogPreface; |
| |
| |
| |
| |
| } |
| |
| |
| |
| |
| |
| |
| List<BlogUserVO> getUserBlog(BlogUserParam bup) throws MapperException; |
| |
| |
| <select id="getUserBlog" |
| parameterType="top.yfly.blog.vo.BlogUserParam" |
| resultType="top.yfly.blog.vo.BlogUserVO"> |
| select |
| <include refid="selbu"></include> |
| from t_blog b, |
| t_user_info ui, t_user u |
| where b.user_id = ui.user_id |
| and |
| b.user_id = u.id and ui.user_id = u.id |
| and b.user_id = #{userId} |
| <if test="blogType != null"> |
| and b.blog_type = #{blogType} |
| </if> |
| <if test="blogTitle != null"> |
| and b.blog_tile like concat('%',#{blogTitle} ,'%') |
| </if> |
| <if test="inputTime != null"> |
| order by b.create_time |
| </if> |
| <if test="praisedCount != null"> |
| order by ui.praised_count desc |
| </if> |
| <if test="accessCount != null"> |
| order by ui.access_count desc |
| </if> |
| <if test="pageNum !=null and pageSize != null"> |
| limit #{pageNum}, #{pageSize} |
| </if> |
| </select> |
| |
映射注解
| @Insert("insert into t_user(user_name,password, sex, age, birthday, head_url), value(#{userName}, #{password}, #{sex}, #{age}, #{birthday}, #{headUrl})") |
| int add(User user); |
| |
| @UpdateProvider(type = UserProvider.class, method = "update") |
| int update(User user); |
| |
| @Delete("delete from t_user where id=#{id}") |
| int del(int id); |
| |
| @Select("select * from t_user where id=#{id}") |
| User getOne(int id); |
| |
sql语句构建器
| @SelectProvider(value = BlogMapperProvide.class, method = "getPage") |
| List<Blog> getPage(@Param("title")String title, @Param("page")int page, @Param("size")int size); |
| |
| |
| |
| |
| class BlogMapperProvider { |
| |
| public static String getPage(@Param("title")String title, @Param("page")int page, @Param("size")int size) { |
| return new SQL() {{ |
| SELECT("*"); |
| FROM("blog"); |
| if(title != null) { |
| WHERE("title like concat('%', #{title} , '%')"); |
| } |
| if(page > 0 && size > 0) { |
| LIMIT("#{page}, #{size}"); |
| } |
| }}.toString(); |
| } |
| } |
| |
| |
| |
| |
| public class BlogMapperProvide { |
| |
| public String getPage(@Param("title")String title, @Param("page")int page, @Param("size")int size) { |
| return new SQL() {{ |
| SELECT("*"); |
| FROM("blog"); |
| if(title != null) { |
| WHERE("title like concat('%', #{title} , '%')"); |
| } |
| if(page > 0 && size > 0) { |
| LIMIT("#{page}, #{size}"); |
| } |
| }}.toString(); |
| } |
| |
| } |
| |
| public String insertPersonSql() { |
| String sql = new SQL() |
| .INSERT_INTO("PERSON") |
| .VALUES("ID, FIRST_NAME", "#{id}, #{firstName}") |
| .VALUES("LAST_NAME", "#{lastName}") |
| .toString(); |
| return sql; |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术