springboot Mybatis 一些写法

转载:http://www.skyz.cn/posts/Spring-Boot-Mybatis-common-annotation.html

 

有三种传参方式

@Param

@Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})")
int insert(@Param("name") String name, @Param("age") Integer age);

Map

Map<String, Object> map = new HashMap<>();
map.put("name", "CCC");
map.put("age", 40);
userMapper.insertByMap(map);
@Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER})")
int insertByMap(Map<String, Object> map);

Object

@Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})")
int insertByUser(User user);

返回结果的mapping-Result

@Results({
@Result(property = "name", column = "name"),
@Result(property = "age", column = "age")
})
@Select("SELECT name, age FROM user")
List<User> findAll();

增删改查

public interface UserMapper {
@Select("SELECT * FROM user WHERE name = #{name}")
User findByName(@Param("name") String name);
@Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})")
int insert(@Param("name") String name, @Param("age") Integer age);
@Update("UPDATE user SET age=#{age} WHERE name=#{name}")
void update(User user);
@Delete("DELETE FROM user WHERE id =#{id}")
void delete(Long id);
}
 
 

 

 

Map<String, Object> paramMap = new HashMap<String, Object>();

       paramMap.put("a", 2);

       paramMap.put("b", 3);

       List<City> citys = (List<City>) getTemplate().selectList(getNameSpaceAndMethod("testByProc"), paramMap);

       return citys;

 

String brc_XXXXXXXX="{#{errorcode,mode=OUT,jdbcType=INTEGER}=call brc_XXXXXXXX(@id=#{reportId,mode=IN,jdbcType=INTEGER}," +
"@operateUserIp=#{operateUseIp,mode=IN,jdbcType=VARCHAR}," +
"@operateUserName=#{operateUserName,mode=IN,jdbcType=VARCHAR}," +
"@operateResult=#{operateResult,mode=IN,jdbcType=INTEGER}," +
"@APIResult =#{apiresult,mode=IN,jdbcType=VARCHAR})}";

 

 

 

是存储过程的参数,而参数。 需要注意的事项:

  存储过程的参数和名称无关,只和顺序有关系

  存储过程的output参数,只能通过传入的map获取

  存储过程返回的结果集可直接用返回的map接收

  存储过程的return结果需要使用?=call procName(?,?)的第一个参数接收,需要指定对应的modeOUT类型

  存储过程对应的数据类型为枚举类型,需要使用大写,如VARCHAR

 

 

posted on 2018-03-12 17:44  凤鸣岐山  阅读(175)  评论(0编辑  收藏  举报

导航