Springboot 在Mybatis中使用HashMap进行增删查改
前言:使用HashMap,我们可以不写resultMap,不写实体类。
OK,我们直接进入实战案例,准备一个springboot+mybatis的项目(如果没搞懂springboot+mybatis的,可以参考我这篇https://blog.csdn.net/qq_35387940/article/details/88048830)。
先准备一张表,
然后新建一个interface,FriendMapper.java:
package com.example.demo.mapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
import java.util.Map;
/**
* @Author : JCccc
* @CreateTime : 2019/9/27
* @Description :
**/
@Mapper
public interface FriendMapper {
int add(Map<String,Object> map);
void deleteById(Map<String,Object> map);
Map<String, Object> queryById(Map<String,Object> map);
int update(Map<String,Object> map);
}
然后是service,FriendService.java:
package com.example.demo.service;
import java.util.List;
import java.util.Map;
/**
* @Author : JCccc
* @CreateTime : 2019/9/27
* @Description :
**/
public interface FriendService {
int add(Map<String,Object> map);
void deleteById(Map<String,Object> map);
Map<String, Object> queryById(Map<String,Object> map);
int update(Map<String,Object> map);
}
接着是FriendServiceImpl.java:
package com.example.demo.service.impl;
import com.example.demo.mapper.FriendMapper;
import com.example.demo.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* @Author : JCccc
* @CreateTime : 2019/9/27
* @Description :
**/
@Service
public class FriendServiceImpl implements FriendService {
@Autowired
FriendMapper friendMapper;
@Override
public int add(Map<String,Object> map) {
return friendMapper.add(map);
}
@Override
public void deleteById(Map<String,Object> map) {
friendMapper.deleteById(map);
}
@Override
public Map<String, Object> queryById(Map<String,Object> map) {
return friendMapper.queryById(map);
}
@Override
public int update(Map<String,Object> map) {
return friendMapper.update(map);
}
}
然后是xml,FriendMapper.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.example.demo.mapper.FriendMapper" >
<insert id="add" useGeneratedKeys="true" keyProperty="id" keyColumn="id"
parameterType="java.util.HashMap">
insert into
friend(id,name,age,nickName)
values (#{id},#{name},#{age},#{nickName})
</insert>
<select id="queryById" parameterType="java.util.HashMap"
resultType="java.util.HashMap">
select *
from friend
where id=#{id}
</select>
<update id="update" useGeneratedKeys="true" keyProperty="id" keyColumn="id"
parameterType="java.util.HashMap">
update friend
set name=#{name},age=#{age},nickName=#{nickName}
where id=#{id}
</update>
<delete id="deleteById" parameterType="java.util.HashMap">
delete
from friend
where id=#{id}
</delete>
</mapper>
最后是写接口进行测试,
添加接口:
@GetMapping("/add")
public String addFriend(){
Map map=new HashMap();
map.put("name","JCccc");
map.put("age",12);
map.put("nickName","Migrant worker");
int result = friendServiceImpl.add(map);
Object id = map.get("id");
System.out.println("id: "+id);
return "影响行数为:"+result+"\n返回主键是:"+id;
}
调用接口:
数据库:
剩下的查询,修改,删除接口,测试:
查询接口:
@GetMapping("/query/{id}")
public String queryFriend(@PathVariable("id") Integer id){
Map map=new HashMap();
map.put("id",id);
map = friendServiceImpl.queryById(map);
return map.toString();
}
调用接口:
修改接口:
@GetMapping("/update")
public String updateFriend(){
Map map=new HashMap();
map.put("id",7);
map.put("name","update");
map.put("age",12);
map.put("nickName","update");
int result = friendServiceImpl.update(map);
Object id = map.get("id");
return "影响行数为:"+result+"\n返回主键是:"+id;
}
调用接口:
数据库:
删除接口:
@GetMapping("/delete/{id}")
public void deleteFriend(@PathVariable("id") Integer id){
Map map=new HashMap();
map.put("id",id);
friendServiceImpl.deleteById(map);
}
调用:
好了,到此。
这个HashMap的使用是需要选择场景的,有些场景使用起来非常舒服方便,有些场景并不然。
学以致用,致用是需要找到合适的场景用才算致用。