4、MyBatis的增删改查
4.1、新增
1 2 3 4
| <insert id="insertUser"> insert into t_user values(null,'admin','123456',23,'男') </insert>
|
4.2、删除
1 2 3 4
| <delete id="deleteUser"> delete from t_user where id = 7 </delete>
|
4.3、修改
1 2 3 4 5
| <update id="updateUser"> update t_user set username='ybc',password='123' where id = 6 </update>
|
4.4、查询一个实体类对象
1 2 3 4
| <select id="getUserById" resultType="com.atguigu.mybatis.bean.User"> select * from t_user where id = 2 </select>
|
4.5、查询list集合
1 2 3
| <select id="getUserList" resultType="com.atguigu.mybatis.bean.User"> select * from t_user </select>
|
测试
mapper接口中的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| int insertUser(User user);
void updateUser(User user);
void deleteUser(Integer id);
User getUserById();
List<User> getAllUser();
User checkLogin(String username, String password);
User checkLoginByMap(Map<String,Object> map);
User checkLoginByParam(@Param("username") String username, @Param("password")String password); }
|
xml配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| <?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="pers.dhx_.mapper.EmpMapper" >
<select id="getUserById" resultType="pers.dhx_.pojo.User" > select *from t_user where id=3; </select> <select id="getAllUser" resultType="User" > select *from t_user ; </select>
<select id="checkLogin" resultType="User"> select *from t_user where username=#{arg0} and password=#{arg1} ; </select>
<select id="checkLoginByMap" resultType="User"> select *from t_user where username= #{username} and password=#{password} ; </select>
<insert id="insertUser"> insert into t_user values(null,#{username},#{password},#{age},#{gender},#{email}); </insert>
<select id="checkLoginByParam" resultType="User"> select *from t_user where username=#{username} and password=#{password}; </select> </mapper>
|
5、MyBatis获取参数值的两种方式
MyBatis获取参数值的两种方式:${}和#{}
${}的本质就是字符串拼接
#{}的本质就是占位符赋值
${}使用字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单引号;
但是#{}使用占位符赋值的方式拼接sql,此时为字符串类型或日期类型的字段进行赋值时,可以自动添加单引号
5.1、单个字面量类型的参数
若mapper接口中的方法参数为单个的字面量类型
- 此时可以使用${}和#{}以任意的名称获取参数的值,注意${}需要手动加单引号
5.2、多个字面量类型的参数
若mapper接口中的方法参数为多个时
- 此时MyBatis会自动将这些参数放在一个map集合中,
- 以arg0,arg1…为键,以参数为值;以param1,param2…为键,以参数为值;
- 因此只需要通过${}和#{}访问map集合的键就可以获取相对应的值,注意${}需要手动加单引号
5.3、map集合类型的参数
若mapper接口中的方法需要的参数为多个时,此时可以手动创建map集合,将这些数据放在map中
只需要通过${}和#{}访问map集合的键就可以获取相对应的值,注意${}需要手动加单引号
5.4、实体类类型的参数
若mapper接口中的方法参数为实体类对象时此时可以使用${}和#{},通过访问实体类对象中的属性名获取属性值,注意${}需要手动加单引号
5.5、使用@Param标识参数
可以通过@Param注解标识mapper接口中的方法参数此时,
会将这些参数放在map集合中,以@Param注解的value属性值为键,以参数为值;
以param1,param2…为键,以参数为值;只需要通过${}和#{}访问map集合的键就可以获取相对应的值,
注意${}需要手动加单引号
User checkLoginByParam(@Param("username") String username, @Param("password")String password);
对于单个参数的方法,一般都要使用@Param注解
-
使用@Param注解
当以下面的方式进行写SQL语句时:
1 2
| @Select("select column from table where userid = #{userid} ") public int selectColumn(int userid);
|
- 当使用了使用@Param注解来声明参数时,如果使用 #{} 或 ${} 的方式都可以。
1 2
| @Select("select column from table where userid = ${userid} ") public int selectColumn(@Param("userid") int userid);
|
- 当不使用@Param注解来声明参数时,必须使用使用 #{}方式。如果使用 ${} 的方式,会报错。
1 2
| @Select("select column from table where userid = ${userid} ") public int selectColumn(@Param("userid") int userid);
|
- 不使用@Param注解
不使用@Param注解时,参数只能有一个,并且是Javabean。在SQL语句里可以引用JavaBean的属性,而且只能引用JavaBean的属性。
1 2
| @Select("SELECT * from Table where id = ${id}") Enchashment selectUserById(User user);
|
6、MyBatis的各种查询功能
6.1、查询一个实体类对象
1 2 3 4 5 6
|
User getUserById(@Param("id") int id);
|
1 2 3 4
| <select id="getUserById" resultType="User"> select * from t_user where id = #{id} </select>
|
6.2、查询一个list集合
1 2 3 4 5
|
List<User> getUserList();
|
注意这里查询实体类 类型需要使用 resultType属性
1 2 3 4
| <select id="getUserList" resultType="User"> select * from t_user </select>
|
当查询的数据为多条时,不能使用实体类作为返回值,否则会抛出异常TooManyResultsException;
但是若查询的数据只有一条,可以使用实体类或集合作为返回值
6.3、查询单个数据
1 2 3 4
| <select id="getCount" resultType="_integer"> select count(id) from t_user </select>
|
6.4、查询一条数据为map集合
1 2 3 4 5 6
|
Map<String, Object> getUserByIdToMap(@Param("id")Integer id);
|
1 2 3 4
| <select id="getUserByIdToMap" resultType="map"> select *from t_user where id=#{id}; </select>
|
6.5、查询多条数据为map集合
1>方式一 – @MapKey
- @MapKey
- 通过@MapKey()注解 , 可以将每条数据的map集合放在一个大的map集合中
1 2 3 4 5 6 7 8
|
@MapKey("id") Map<Integer,Map<String, Object>> getAllUserToMap();
|
1 2 3
| <select id="getUserByIdToMap" resultType="map"> select *from t_user where id=#{id}; </select>
|
2>方式二 List< Map<Integer,User> >
- 将mapper接口的方法的返回值设置为泛型为map 的List 的集合
1 2
| List<Map<String, Object>> getAllUserToMap();
|
1 2 3
| <select id="getAllUserToMap" resultType="map"> select *from t_user; </select>
|