如果查询出的数据只有 "一条",可以通过:
1-实体类对象接收
2-List集合接收
3-Map集合接收,结果{password=123456, sex=男, id=1, age=23, username=admin}
2-如果查询出的数据有 "多条",一定不能用"实体类对象"接收,会抛异常 "TooManyResultsException",可以通过
1-实体类类型的"LIst集合"接收
2-Map类型的"LIst集合"接收
3-在mapper接口的方法上添加@MapKey注解
/**
@Test
public void testGetUserById(){
SqlSession sqlSession = SqlSessionUtils.getSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
User userById = mapper.getUserById(3);
System.err.println(userById);
sqlSession.close();
}
/**
@Test
public void testGetAllUser(){
SqlSession sqlSession = SqlSessionUtils.getSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
List
for (User u:userById) {
System.err.println(u);
}
sqlSession.close();
}
/**
@Test
public void testCount() {
SqlSession sqlSession = SqlSessionUtils.getSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Integer count = mapper.getCount();
System.err.println(count);
}
/**
* 根据用户id查询用户信息为map集合
* @param id
* @return
*/
Map<String, Object> getUserToMap(@Param("id") int id);
<!--Map<String, Object> getUserToMap(@Param("id") int id);-->
<select id="getUserToMap" resultType="map">
select * from t_user where id = #{id}
</select>
<!--结果:{password=123456, sex=男, id=1, age=23, username=admin}-->
@Test
public void testGetUserToMap() {
SqlSession sqlSession = SqlSessionUtils.getSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Map<String, Object> userToMap = mapper.getUserToMap(3);
System.err.println(userToMap);
}
/**
@Test
public void testGetAllUserToMap() {
SqlSession sqlSession = SqlSessionUtils.getSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
List
/**
@Test
public void testGetAllUserToMap2() {
SqlSession sqlSession = SqlSessionUtils.getSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Map
System.err.println(allUserToMap);
}
/**
根据用户名进行模糊查询
@param username
@return java.util.List
@date 2022/2/26 21:56
*/
List
@Test
public void testGetAllUserToMap2() {
SqlSession sqlSession = SqlSessionUtils.getSession();
SqlMapper mapper = sqlSession.getMapper(SqlMapper.class);
List
System.err.println(a);
}
细节点:
*只能使用${},不能使用#{}
因为:
${}:delete from t_user where id in (1,2,3)
#{}:delete from t_user where id in ('1,2,3') //不行
原因:#{}在MyBatis会自动生成 ' ' 号。
/**
//测试类
@Test
public void deleteMore() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SQLMapper mapper = sqlSession.getMapper(SQLMapper.class);
int result = mapper.deleteMore("1,2,3,8");
System.out.println(result);
}
连接了"数据库",但是有很多"表"需要查询,就可以 "动态设置表名"
细节:
只能使用${},因为 "表名" 不能加单引号
/**
* 查询指定表中的数据
* @param tableName
* @return java.util.List<com.atguigu.mybatis.pojo.User>
* @date 2022/2/27 14:41
*/
List<User> getUserByTable(@Param("tableName") String tableName);
<!--List<User> getUserByTable(@Param("tableName") String tableName);-->
<select id="getUserByTable" resultType="User">
select * from ${tableName}
</select>
@Test
public void getUserByTable() {
SqlSession sqlSession = SqlSessionUtils.getSession();
SqlMapper mapper = sqlSession.getMapper(SqlMapper.class);
List<User> userByTable = mapper.getUserByTable("t_user");
System.out.println(userByTable);
}
理解:在 "添加一条信息后,可以返回这个信息在表中的id值"
使用场景:
t_clazz(clazz_id,clazz_name)
t_student(student_id,student_name,clazz_id)
添加班级信息
获取新添加的班级的id
为班级分配学生,即将某学的班级id修改为新添加的班级的id
/**
* 添加用户信息
* @param user
* @date 2022/2/27 15:04
*/
void insertUser(User user);
注意点:
1-useGeneratedKeys:设置使用自增的主键
2-keyProperty:因为增删改有统一的返回值是受影响的行数,
因此只能将获取的自增的主键放在传输的参数user对象的某个属性中
<!--void insertUser(User user);
useGeneratedKeys:设置使用自增的主键
keyProperty:因为增删改有统一的返回值是受影响的行数,因此只能将获取的自增的主键放在传输的参数user对象的某个属性中
-->
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
insert into t_user values (null,#{username},#{password},#{age},#{sex},#{email})
</insert>
//测试类
@Test
public void insertUser() {
SqlSession sqlSession = SqlSessionUtils.getSession();
SqlMapper mapper = sqlSession.getMapper(SqlMapper.class);
User user = new User(null, "ton", "123", 23, "男", "123@321.com");
mapper.insertUser(user);
System.out.println(user);
//输出:user{id=10, username='ton', password='123', age=23, sex='男', email='123@321.com'},自增主键存放到了user的id属性中
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章