MyBatis MyBatis概念
持久层
负责将数据到保存到数据库的那一层代码
avaEE三层架构:表现层、业务层、持久层
框架
框架就是一个半成品软件,是一套可重用的、通用的、软件基础代码模型
在框架的基础之上构建软件编写更加高效、规范、通用、可扩展
为什么简化 简化jDBC :免除JDBC代码以及设置参数和获取结果集的工作。
JDBC存在的缺点:
硬编码
操作繁琐
Mapper代理开发
定义与SQL映射文件同名的Mapper接口,并且将Mapper接口和SQL映射文件放置在同一目录下
设置SQL映射文件的namespace属性为Mapper接口全限定名
在Mapper接口中定义方法,方法名就是SQL映射文件中sql语句的id,并保持参数类型和返回值 类型一致
编码
通过SqlSession的getMapper方法获取Mapper接口的代理对象
调用对应方法完成sql的执行
细节:如果Mapper接口名称和SQL映射文件名称相同,并在同一目录下,则可以使用包扫描的方式简化SQL映射文件的加载
1 2 3 4 5 UserMapper userMapper = sqlSession.getMapper(UserMapper.class);List<User> users = userMapper.selectAll(); System.out.println(users);
UserMapper.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 <mapper namespace ="com.jpc.mapper.UserMapper" > <select id ="selectAll" resultType ="com.jpc.pojo.User" > select * from tb_user; </select > </mapper > <resultMap id ="userResultMap" type ="user" > <result column ="user_name" property ="userName" /> </resultMap > <select id ="selectAll" resultMap ="userResultMap" > select * from tb_user; </select >
配置文件完成增删改查 查询 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <select id ="selectById" parameterType ="int" resultMap ="brandResultMap" > select * from tb_brand where id = #{id}; </select >
多条件查询 mapper接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 List<Brand> selectByCondition (@Param("status") int status, @Param("companyName") String companyName) ; List<Brand> selectByCondition (Brand brand) ; List<Brand> selectByCondition (Map map) ;
调用
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 40 41 42 43 44 45 46 int status = 1 ;String companyName = "华为" ;companyName = "%" + companyName + "%" ; List<Brand> brands = brandMapper.selectByCondition(status, companyName); System.out.println(brands); int status = 1 ;String companyName = "华为" ;companyName = "%" + companyName + "%" ; Brand brand = new Brand ();brand.setStatus(status); brand.setCompanyName(companyName); List<Brand> brands = brandMapper.selectByCondition(brand); System.out.println(brands); int status = 1 ;String companyName = "华为" ;companyName = "%" + companyName + "%" ; Map map = new HashMap ();map.put("status" , status); map.put("companyName" , companyName); List<Brand> brands = brandMapper.selectByCondition(map); System.out.println(brands);
mapper映射文件
1 2 3 4 5 6 7 8 9 10 <select id ="selectByCondition" resultMap ="brandResultMap" > select * from tb_brand where status = #{status} and company_name like #{companyName} </select >
添加 mapper接口
调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 int status = 2 ; String companyName = "小米有限公司" ; String brandNmae = "红米" ; String description = "为烧焦而生" ; int ordered = 1 ; Brand brand = new Brand (); brand.setStatus(status); brand.setCompanyName(companyName); brand.setBrandName(brandNmae); brand.setOrdered(ordered); brand.setDescription(description); brandMapper.add(brand);
mapper映射文件
1 2 3 4 5 6 7 <insert id ="add" useGeneratedKeys ="true" keyProperty ="id" > insert into tb_brand(brand_name, company_name, ordered, description, status) values(#{brandName}, #{companyName}, #{ordered}, #{description}, #{status}); </insert >
修改 mapper接口
1 void update (Brand brand) ;
调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 int id = 6 ; int status = 2 ; String companyName = "小米有限公司" ; String brandNmae = "红米" ; String description = "为发烧而生" ; int ordered = 100 ; Brand brand = new Brand (); brand.setStatus(status); brand.setCompanyName(companyName); brand.setBrandName(brandNmae); brand.setOrdered(ordered); brand.setDescription(description); brand.setId(id); brandMapper.update(brand);
mapper映射文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <update id ="update" > update tb_brand <set > <if test ="brandName != null and brandName != ''" > brand_name = #{brandName}, </if > <if test ="companyName != null and companyName != ''" > company_name = #{companyName}, </if > <if test ="ordered != null and ordered != ''" > ordered = #{ordered}, </if > <if test ="description != null and description != ''" > description = #{description}, </if > <if test ="status != null and status != ''" > status = #{status} </if > </set > where id = #{id}; </update >
删除 mapper接口
1 void deleteById (int id) ;
调用
1 brandMapper.deleteById(id);
mapper映射文件
1 2 3 <delete id ="deleteById" > delete from tb_brand where id = #{id}; </delete >
MyBatis参数传递 MyBatis 接口方法中可以接收各种各样的参数,MyBatis底层对于这些参数进行不同的封装处理方式>
单个参数
POJO类型
Map集合
Collection
List
Array
其他类型
多个参数: 封装为Map集合,可以使用@Param注解
map.put(“arg0”, 参数1)
map.put(“param1”, 参数1)
map.put(“param2”, 参数2)
map.put(“arg1”, 参数2)
替换:@Param(“username”) —- > map.put(“username”, 参数1)