1.maven配置mybatis依赖
2.mybatis-config.xml
3.UserMapper.xml
4.UserMapper接口
5.编写代码
从xml中构建SqlSessionFactory,通过SqlSessionFactory.openSession()获取SqlSession
sqlSession可以直接执行mapper.xml中的sql语句,比方说:
sqlSession.selectOne("#namesapce.#id",参数),这种方式比较由于要输入namespace.id,所以容易出错,改进的方式是使用Mapper:
BlogMapper mapper = session.getMapper(BolgMapper.class);
直接调用BlogMapper中的方法。mapper代理对象会执行底层的sqlSession方法,要想做到这一点,得将Mapper与Mapper.xml相关联
6.如何将Mapper接口与mapper.xml映射在一起?
配置mapper.xml中的namespace
<mapper namespace="com.itheima.mapper.UserMapper">
7.加载sql映射文件
在mybaits-configs.xml文件中添加如下代码
<mappers>
<!--<mapper resource="com/itheima/mapper/UserMapper.xml"/>-->
<package name="com.itheima.mapper"/>
</mappers>
第一行注释只能加载一个sql映射文件
第二行代码可以加载包下所有映射文件
8.idea配置数据库
9.mybatis-config.xml核心配置文件
environments:配置数据库连接环境信息,可以配置多个environment,通过default来切换不同的environment
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///mybatis?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
<environment id="test">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///mybatis?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
mapper.xml中的resultType中要写带包名的类,很麻烦,为了只写不带包名的类名,比如只写User类名(注意是小写形式),可以在mybatis-config.xml中配置typeAliases
<configuration>
<typeAliases>
<package name="com.itheima.pojo"/>
</typeAliases>
要注意这个标签的位置,只能处于configuration下的第一行
10.mybatis插件
安装MyBatisX插件,其功能包括:
XML和接口方法 相互跳转
根据接口方法生成statement
11.将数据库查询到的数据映射到对应的类中保存
如果数据库字段名与类中的属性名不一样,比方说company_name与companyName不一样,可以通过定义resultMap来解决
<resultMap id="brandResultMap" type="brand">
<!--<id></id>id完成主键字段的映射,result完成一般字段的映射-->
<result column="brand_name" property="brandName"></result>
<result column="company_name" property="companyName"></result>
</resultMap>
同时查询到映射类型,要改为resultMap,而不是resultType
12.传递参数
pojo、map、Collection、List、Array、其他类型,建议将来使用@Param注解来修改Map集合中默认的键名,并使用修改后的昵称来获取值,这样可读性更高
参数都会被封装成Map来处理
接收对应参数,用#{id}
${id}存在sql注入问题,适用于表名或者列名不固定的情况
mybatis如何知道参数传递给sql语句中的哪个变量
通过@Param("status")标识,这个status与#{status}中的status得一致
List<Brand> selectByCondition(@Param("status") int status,
@Param("companyName")String companyName,
@Param("brandName") String brandName);
<select id="selectByCondition" resultMap="brandResultMap">
select * from tb_brand where
status = #{status}
and company_name like #{companyName}
and brand_name like #{brandName}
</select>
pojo中的实体类的可以直接映射传递参数(要求类中的属性与#{}中的变量名一致)
map也可以直接映射传递参数(要求map中的键名与#{}中的变量名一致)
13.多条件模糊查询
模糊查询用like关键字,多条件的连接通过and关键字
14.动态sql
多条件查询存在缺陷,如果其中一个参数为空,则语法出现问题,比方说status为null,则status = null,这个是sql语法的bug,可以利用动态sql中的<where>来解决,通过添加
<if test="status != null and status !=''"></if>来解决
<choose><when test=""></when><othewise></otherwise></choose>类似与switch语句
主键返回 (要把自动提交事务开启)openSession(true)
<insert useGeneratedKeys="true" keyProperty="id"></insert>
动态修改
<set>
<if test="brandName != null and brandName !='' ">
brand_name = #{brandName}
</if>
</set>
批量删除
mapper中:void deleteByIds(@Param("ids") int[] ids);
<delete>
delete from tb_brand where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>