insert into t_user values(null,'admin','123456',23,'男')
delete from t_user where id = 1
update t_user set username='ybc',password='123' where id = 6
注意: 查询的标签select必须设置属性resultType或resultMap,用于设置实体类和数据库表的映射关系
resultType:自动映射,用于属性名和表中字段名一致的情况
resultMap:自定义映射,用于一对多或多对一或字段名和属性名不一致的情况
MyBatis获取参数值的两种方式:${} 和 #{}
${} 的本质就是字符串拼接,#{} 的本质就是占位符赋值
${} 使用字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单 引号;
#{} 使用占位符赋值的方式拼接sql,此时为字符串类型或日期类型的字段进行赋值时, 可以自动添加单引号
若 mapper 接口中的方法参数为单个的字面量类型
此时可以使用 ${} 和 #{} 以任意的名称获取参数的值,注意 ${} 需要手动加单引号
若mapper接口中的方法参数为多个时
此时MyBatis会自动将这些参数放在一个map集合中,以arg0,arg1...为键,以参数为值、以 param1,param2...为键,以参数为值;
因此只需要通过 ${} 和 #{} 访问map集合的键就可以获取相 对应的值,注意 ${} 需要手动加单引号
若mapper接口中的方法需要的参数为多个时,此时可以手动创建map集合,将这些数据放在 map中
只需要通过 ${} 和 #{} 访问map集合的键就可以获取相对应的值,注意 ${} 需要手动加单引号
若mapper接口中的方法参数为实体类对象时
此时可以使用 ${} 和 #{} ,通过访问实体类对象中的属性名获取属性值,注意 ${} 需要手动加单引号
insert into t_user values(null,#{username},#{password},#{age},#{gender},#{email})
可以通过 @Param 注解标识mapper接口中的方法参数
此时,会将这些参数放在map集合中,以@Param注解的value属性值为键,以参数为值;以 param1,param2...为键,以参数为值;
只需要通过 ${} 和 #{} 访问map集合的键就可以获取相对应 的值, 注意 ${} 需要手动加单引号
package com.ssm.mybatis.mapper;import com.ssm.mybatis.pojo.User;
import org.apache.ibatis.annotations.Param;import java.util.List;/*** Author:wy* Date:2023/3/18*/public interface SelectMapper {/*** 根据用户id查询用户信息* @param id* @return*/User getUserById(@Param("id") int id);
}
@Testpublic void testGetUserById() {SqlSession sqlSession = SqlSessionUtil.getSqlSession();SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);User user = mapper.getUserById(1);System.out.println(user);}
/*** 查询所有用户信息* @return*/List getUserList();
@Testpublic void testGetUserList() {SqlSession sqlSession = SqlSessionUtil.getSqlSession();SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);List userList = mapper.getUserList();for (User user : userList) {System.out.println(user);}}
当查询的数据为多条时,不能使用实体类作为返回值,否则会抛出异常TooManyResultsException;
但是若查询的数据只有一条,可以使用实体类或集合作为返回值
/*** 查询用户的总记录数* @return*/Integer getCount();
@Testpublic void testGetCount() {SqlSession sqlSession = SqlSessionUtil.getSqlSession();SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);Integer count = mapper.getCount();System.out.println("用户总数=" + count);}
/*** 根据用户id查询用户信息为map集合* @param id* @return*/Map getUserByIdToMap(@Param("id") Integer id);
@Testpublic void testGetUserByIdToMap() {SqlSession sqlSession = SqlSessionUtil.getSqlSession();SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);Map map = mapper.getUserByIdToMap(1);System.out.println(map);//结果:{password=123456, gender=男, id=1, age=21, email=123456@qq.com, username=张三}}
方式一
/*** 查询所有用户信息为map集合* @return* 将表中的数据以map集合的方式查询,一条数据对应一个map;若有多条数据,就会产生多个map集合此时可以将这些map放在一个list集合中获取*/@MapKey("id")List
@Testpublic void testGetAllUserToMap() {SqlSession sqlSession = SqlSessionUtil.getSqlSession();SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);List
方式二
/*** 查询所有用户信息为map集合* @return* 可以将每条数据转换的map集合放在一个大的map中,* 但是必须要通过@Mapkey注解将查询的某个字段的值作为大的map的键*/@MapKey("id")Map getAllUserToMap();
@Testpublic void testGetAllUserToMap() {SqlSession sqlSession = SqlSessionUtil.getSqlSession();SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);Map map = mapper.getAllUserToMap();System.out.println(map);//{ // 1={password=123456, gender=男, id=1, age=21, email=123456@qq.com, username=张三}, // 2={password=123456, gender=女, id=2, age=19, email=123456@qq.com, username=老六} // }}