Search code examples
javaspringspring-bootmybatisibatis

How to Perform proper SQL INSERT using MyBatis-SpringBoot-MySQL in XML-style?


Im having a hard time inserting a new data in my SQL database. Can someone check my mapper.xml and mapper.java?

Actor.java POJO

@Component
public class Actor {

    private static final long serialVersionUID = 1L;

    private Integer actor_id;
    private String first_name;
    private String last_name;
    private String last_update;
    //Getters and Setters
}

ActorMapper.xml

 <insert id="insertNewActor" parameterClass="com.helios.mybatissakila.model.Actor" 
         useGeneratedKeys="true" keyColumn="actor_id" keyProperty="actor_id">
     insert into actor (first_name,last_name,last_update) 
     values (#{first_name},#{last_name},#{last_update})
 </insert>

Mapper.java method

List <Actor> insertNewActor(Map<String, Object> actor);

RESTController method

@PostMapping(value="/actors", consumes = "application/json", produces = "application/json")
    public List<Actor> insertNewActor(@RequestBody Map<String, Object> actor) {
        return actorMapper.insertNewActor(actor);
    }

DB table structure:

enter image description here

Also, please notice if I should be using parameterClass or parameterType in mapper.xml?


Solution

  • first remove the @Component Actor which is not needed. then change mapper.java ,like this

    int insertNewActor(Actor actor);
    

    if it fails,show the error