Ⅰ:依赖:只需普通的mysql,数据源,JDBC,数据源连接等依赖即可,不需另外添加依赖
Ⅱ:sql语句:
insert into st_teacher (id, create_by, create_time,
update_by, update_time, version,
name, password, icon,
role_id,logins)
values (#{teacher.id,jdbcType=VARCHAR}, #{teacher.createBy,jdbcType=VARCHAR}, #{teacher.createTime,jdbcType=TIMESTAMP},
#{teacher.updateBy,jdbcType=VARCHAR}, #{teacher.updateTime,jdbcType=TIMESTAMP}, #{teacher.version,jdbcType=INTEGER},
#{teacher.name,jdbcType=VARCHAR}, #{teacher.password,jdbcType=VARCHAR}, #{teacher.icon,jdbcType=VARCHAR},
#{teacher.roleId,jdbcType=VARCHAR},#{teacher.logins,jdbcType=INTEGER})
//批量插入语句
insert into st_teacher (id, create_by, create_time,
update_by, update_time, version,
name, password, icon,
role_id,logins) values
(#{teacher.id,jdbcType=VARCHAR}, #{teacher.createBy,jdbcType=VARCHAR}, #{teacher.createTime,jdbcType=TIMESTAMP},
#{teacher.updateBy,jdbcType=VARCHAR}, #{teacher.updateTime,jdbcType=TIMESTAMP}, #{teacher.version,jdbcType=INTEGER},
#{teacher.name,jdbcType=VARCHAR}, #{teacher.password,jdbcType=VARCHAR}, #{teacher.icon,jdbcType=VARCHAR},
#{teacher.roleId,jdbcType=VARCHAR},#{teacher.logins,jdbcType=INTEGER})
对应关系:
注意:在批量插入中若想调用方法属性必须使用collection对应的值加上点获取,如上图片中,否则报以下异常:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘id’ not found. Available parameters are [collection, list]
Ⅲ:Mapper接口:
package com.luntek.platform.ic_manufacturing_platform.mapper.admin;
import com.luntek.platform.ic_manufacturing_platform.entity.Teacher;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author Czw
* @Description TODO
* @Date 2019/4/12 0012 下午 1:23
*/
@Repository
public interface AdminMapper {
/**
* 单个增加老师
* @param teacher
* @return
*/
Integer addSingleTeacher(@Param("teacher") Teacher teacher);
/**
* 批量添加老师用户
* @param list
* @return
*/
Integer addBatchTeacher(List list);
}
Ⅳ:测试页面
package com.luntek.platform.ic_manufacturing_platform.mapper.admin;
import com.luntek.platform.ic_manufacturing_platform.entity.Teacher;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import static org.junit.Assert.*;
/**
* @author Czw
* @Description TODO
* @Date 2019/4/12 0012 下午 3:59
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class AdminMapperTest {
@Autowired
private AdminMapper adminMapper;
@Test
public void addBatchTeacher(){
List list=new ArrayList<>();
Teacher t1=new Teacher();
t1.setId(UUID.randomUUID().toString());
t1.setRoleId("teacher1");
t1.setIcon("/usr/image");
t1.setCreateBy("root");
t1.setCreateTime(new Date());
t1.setLogins(0);
t1.setName("zs");
t1.setPassword("123456");
t1.setVersion(0);
t1.setUpdateBy("root");
t1.setUpdateTime(new Date());
Teacher t2=new Teacher();
t2.setId(UUID.randomUUID().toString());
t2.setRoleId("teacher2");
t2.setIcon("/usr/image");
t2.setCreateBy("root");
t2.setCreateTime(new Date());
t2.setLogins(0);
t2.setName("zs");
t2.setPassword("123456");
t2.setVersion(0);
t2.setUpdateBy("root");
t2.setUpdateTime(new Date());
list.add(t1);
list.add(t2);
log.info("生效数量:",adminMapper.addBatchTeacher(list));
}
@Test
public void addSingleTeacher() {
Teacher t1=new Teacher();
t1.setId(UUID.randomUUID().toString());
t1.setRoleId("teacher");
t1.setIcon("/usr/image");
t1.setCreateBy("root");
t1.setCreateTime(new Date());
t1.setLogins(0);
t1.setName("zs");
t1.setPassword("123456");
t1.setVersion(0);
t1.setUpdateBy("root");
t1.setUpdateTime(new Date());
log.info("生效数量:{}",adminMapper.addSingleTeacher(t1));
}
}
Ⅴ:实体类Teacher
package com.luntek.platform.ic_manufacturing_platform.entity;
import lombok.Data;
@Data
public class Teacher extends BaseEntity{
private static final long serialVersionUID = 662626208904211382L;
private String id;
private String createBy;
private Date createTime;
private String updateBy;
private Date updateTime;
private Integer version;
private Integer logins;
private String name;
private String password;
private String icon;
private String roleId;
}
余生还长,切勿惆怅;创作不易,点赞再走