1.向一个数据表中插入数据,以对象的形式对数据进行封装
1.1 pojo ----RoleAuthList
@Setter
@Getter
public class RoleAuthList {private Integer id;private Integer userId;private Integer metaDataId;private Integer metaDataFieldId;
}
1.2 controller--- RoleAuthListController
@RestController
@RequestMapping("/auth")
public class RoleAuthListController {
@ResponseBody
@RequestMapping( value = "/add_role_auth",method = RequestMethod.POST)
public int addRoleAuth(@RequestBody RoleAuthList roleAuthList){
return roleAuthListService.addRoleAuth(roleAuthList);
}
}
1.3 service --- RoleAuthListService
public interface RoleAuthListService {// int addRoleAuth(Integer userId, Integer metaDataId,Integer metaDataFieldId);int addRoleAuth(RoleAuthList roleAuthList);
}
1.4 service-imple----RoleAuthListServiceImpl
@Service(value = "roleAuthListService")
public class RoleAuthListServiceImpl implements RoleAuthListService {@Autowiredprivate RoleAuthListMapper roleAuthListMapper;
public int addRoleAuth(RoleAuthList roleAuthList){// return roleAuthListMapper.insertRoleAuth(userId, metaDataId, metaDataFieldId);
return roleAuthListMapper.insertRoleAuth(roleAuthList);}
}
1.5 mapper --- RoleAuthListMapper
@Repository
public interface RoleAuthListMapper {
int insertRoleAuth(RoleAuthList roleAuthList);
}
1.6 resources文件夹下 mapper 下 RoleAuthListMapper.xml
<mapper namespace="com.yl.demo.mapper.RoleAuthListMapper" ><resultMap id="BaseResultMap" type="com.yl.demo.pojo.RoleAuthList" ><id column="id" property="id" jdbcType="INTEGER" /><result column="user_id" property="userId" jdbcType="INTEGER" /><result column="meta_data_id" property="metaDataId" jdbcType="INTEGER" /><result column="meta_data_field_id" property="metaDataFieldId" jdbcType="INTEGER" /><!--前面的列对应的是数据表中的列,后面的属性对应的是 pojo--></resultMap><sql id="Base_Column_List" >id,user_id, meta_data_id, meta_data_field_id</sql><insert id="insertRoleAuth" parameterType="com.yl.demo.pojo.RoleAuthList">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">SELECT LAST_INSERT_ID()
</selectKey>insert into role_auth_list (user_id, meta_data_id,meta_data_field_id)values(#{userId,jdbcType=INTEGER},#{metaDataId,jdbcType=INTEGER},#{metaDataFieldId,jdbcType=INTEGER})
</insert>
</mapper>
注意在 application.properties中的配置
mybatis.mapper-locations=classpath:mapper/*.xml