mybatis-puls

mybatis-plus依赖
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>
application.yml配置
server:
  port: 8003
spring:
  datasource:
    url: jdbc:mysql:/IP地址//数据库?serverTimezone=GMT%2B8&CharacterEncoding=utf8
    username: 账号
    password: 密码
mybatis-plus:
# 扫描xml文件
  mapper-locations: classpath:/mapper/*.xml
  configuration:
# 控制台打印执行SQL语句
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
启动类扫描mapper
@SpringBootApplication
//扫描
@MapperScan("com.jsd.mapper")
public class Springcloud03Application {

    public static void main(String[] args) {
        SpringApplication.run(Springcloud03Application.class, args);
    }

}

pojo

User

@Data
//数据库表名
@TableName("user")
public class User implements Serializable {
    private Integer Id;
    private String Name;
    private Integer Age;
    private String Email;
//对象
    private Message message;
//嵌套集合
    private List<Message> messageList;

}

启用分页配置

@Configuration
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        //定义分页拦截器对象
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MARIADB));
        return interceptor;
    }

}

Message

@Data
@TableName("message")
public class Message implements Serializable {
    private Integer Id;
    private String Message;
}
UserMapper接口
//继承BaseMapper<>
public interface UserMapper extends BaseMapper<User>{

    List<User> selectAll();

    List<User> selectLists();
}
UserMapper.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" >
<mapper namespace="com.jsd.mapper.UserMapper">
    <resultMap id="User" type="com.jsd.pojo.User">
        <id column="id" property="Id"></id>
        <result column="name" property="Name"></result>
        <result column="age" property="Age"></result>
        <result column="email" property="Email"></result>
        <association property="message" javaType="com.jsd.pojo.Message">
            <id column="id" property="Id"></id>
            <result column="message" property="Message"></result>
        </association>
    </resultMap>
    <resultMap id="UserList" type="com.jsd.pojo.User">
        <id column="id" property="Id"></id>
        <result column="name" property="Name"></result>
        <result column="age" property="Age"></result>
        <result column="email" property="Email"></result>
        <collection property="messageList" ofType="com.jsd.pojo.Message">
            <id column="id" property="Id"></id>
            <result column="message" property="Message"></result>
        </collection>
    </resultMap>
    <select id="selectAll" resultMap="User">
        select
        a.id,a.name,a.age,a.email,b.message
        from user a join message b
        on a.id=b.id
    </select>
    <select id="selectLists" resultMap="UserList">
        select
        a.id,a.name,a.age,a.email,b.message
        from user a join message b
        on a.id=b.id
    </select>
</mapper>

添加多条数据

Integer insertMessage(@Param("ListMessage") List<Message> message);
    <insert id="insertMessage">
        insert into message
        (id,message)
        values
        <foreach collection="ListMessage" item="message" separator=",">
            (#{message.id},#{message.message})
        </foreach>
    </insert>

分页

List<ApiHistory> page(@Param("apiHistory") ApiHistory apiHistory, @Param("pageSize") Integer pageSize, @Param("currentPage") Integer currentPage);
    <select id="page" resultType="com.zw.entity.ApiHistory">
        select api_history_id,task_name,url,request,create_time 
            from
        api_history
        <where>
            <if test="apiHistory.taskName != null">
                and task_name like concat(concat('%',#{apiHistory.taskName},'%'))
            </if>
        </where>
        order by api_history_id desc limit #{currentPage},#{pageSize}
    </select>

批量删除

int deleteList(@Param("ids") String[] ids);
<delete id="deleteList">
        delete from api_history
        where api_history_id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>

LLL_
15 声望3 粉丝