spring boot---mybatis
@mapper 与 .xml 的关系
mapper类:

1 package com.zsc.ticketsys.mapper; 2 3 import com.zsc.ticketsys.domain.Ticket; 4 import org.apache.ibatis.annotations.Delete; 5 import org.apache.ibatis.annotations.Mapper; 6 import org.apache.ibatis.annotations.Param; 7 import org.apache.ibatis.annotations.Select; 8 import com.zsc.ticketsys.vo.TicketUserVo; 9 10 import java.util.List; 11 12 @Mapper 13 public interface TicketMapper { 14 public Ticket get(@Param("id") Long id); 15 //添加票据 16 public int insert(Ticket ticket); 17 18 @Delete("delete from ticket where id=#{id}") 19 public int delete(Long id); 20 21 @Select("select * from user") 22 public List<Ticket> listTicket(); 23 24 //根据主题进行迷糊查询 模糊查询 25 public List<Ticket> listLikeSubject(String subject); 26 27 //根据主题和类型进行查询 动态查询 28 public List<Ticket> listLikeSubjectAndType(String subject,String type); 29 30 //查询票据及其附件 31 public Ticket getTicketDetails(Long id); 32 33 //联表查询 34 @Select("select t.*,u.username from ticket as t,user as u where t.user_id=u.id") 35 public List<TicketUserVo> listTicketUser(); 36 37 }
xml文件:

1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <!-- namespace表示命名空间 --> 6 <mapper namespace="com.zsc.ticketsys.mapper.TicketMapper"> 7 <!--根据票据编号获取票据信息 --> 8 <select id="get" parameterType="Long" resultType="Ticket"> 9 select * from ticket where id=#{id} 10 </select> 11 12 <!-- 添加票据信息 --> 13 <insert id="insert" parameterType="Ticket" useGeneratedKeys="true" keyProperty="id"> 14 insert into ticket(user_id,subject,body,money,ticket_type,create_time,status,company_name) 15 values(#{userId},#{subject},#{body},#{money},#{ticketType},#{createTime},#{status},#{companyName}) 16 </insert> 17 <!-- 模糊查询--> 18 <select id="listLikeSubject" parameterType="String" resultType="Ticket"> 19 select * from ticket where Subject like concat('%' ,#{subject},'%') 20 </select> 21 22 <!--动态查询 --> 23 <select id="listLikeSubjectAndType" parameterType="String" 24 resultType="Ticket"> 25 select * from ticket where 1=1 26 <if test="subject !=null and type!=''">and subject like concat('%',#{subject},'%')</if> 27 <if test="type !=null and type !=''">and ticket_type =#{type}</if> 28 </select> 29 30 <select id="getTicketDetails" parameterType="Long" resultMap="Ticket_User_AttachFile"> 31 select * from ticket where id=#{id} 32 </select> 33 34 <!-- 定义一个ResultMap 封装查询结果--> 35 <resultMap id="Ticket_User_AttachFile" type="Ticket"> 36 <id property="id" column="id"/> 37 <!-- 用户对象1对1关联--> 38 <association property="user" column="user_id" javaType="User" 39 select="com.zsc.ticketsys.mapper.UserMapper.get"/> 40 <!-- 附件对象1对多关联--> 41 <collection property="attachFile" column="id" ofType="AttachFile" 42 select="com.zsc.ticketsys.mapper.AttachFileMapper.listByTicketId"/> 43 </resultMap> 44 45 46 </mapper>