三、MyBatis
概述
-  MyBatis是一个持久层框架,用于简化JDBC 


Mapper代理开发

-  在resources配置文件包中创建多级目录用 / 
MyBatis核心配置文件

-  enviroments:配置数据库连接环境信息。 -  可以配置多个enviroment,通过default属性切换不同的enviroment 
 
-  
-  mappers:加载sql映射文件、mapper代理方式 -  可以使用包扫描方式 
 
-  
-  typeAliases:类型别名  
配置文件完成增删改查
查询

查询所有数据
<!--数据库字段名和实体类属性不一样*起别名*用sql片段*resultMap:1.定义<resultMap>标签2.在<select>标签中,使用resultMap属性替换resultType属性--><!--id:唯一标识type:映射的类型,支持别名--><!--resultMap--><resultMap id="brandResultMap" type="brand"><!--id:完成主键字段的映射column:表的列名property:实体类的属性名result:完成一般字段的映射--><result column="brand_name" property="brandName"/><result column="company_name" property="companyName"/></resultMap><select id="selectAll" resultMap="brandResultMap">select*from tb_brand;</select><!--起别名--><select id="selectAll" resultType="brand">selectid,brand_name as brandName, company_name as companyName, ordered, description, statusfrom tb_brand;</select><!--用sql片段--><sql id="brand_column">id,brand_name as brandName, company_name as companyName, ordered, description, status</sql><select id="selectAll" resultType="Brand">select<include refid="brand_column" />from tb_brand;</select>查询详情
参数占位符
-  #{}:会替换成?,防止SQL注入 
-  ${}:直接拼接数据,会造成SQL注入 
<!--*参数占位符1. #{}:会替换成?,防止SQL注入2. ${}:直接拼接数据,会造成SQL注入*参数类型parameterType可以省略,一般不写*特殊字符处理<:  1.转义字符:<2.CDATA区:   <![CDATA[<]]>--><select id="selectById" parameterType="int" resultMap="brandResultMap">select * from tb_brand where id = #{id};</select>条件查询
多条件查询
   /**   参数接收*       1.散装参数:如果方法中有多个参数,需要用 @Param("SQL参数占位符名称")*       2.对象参数:保证SQL中的参数名和实体类的属性名保持一致*       3.map集合参数* */List<Brand> selectByCondition(@Param("status")int status,@Param("companyName")String comparyName,@Param("brandName")String brandName);List<Brand> selectByCondition(Brand brand);List<Brand> selectByCondition(Map map);动态SQL
-  SQl语句中用户输入的变化 


    <select id="selectByCondition" resultMap="brandResultMap">select *from tb_brand<where><if test="status != null">and status = #{status}</if><if test="companyName != null and companyName != '' ">and company_name like #{companyName}</if><if test="brandName != null and brandName != '' ">and brand_name like #{brandName}</if></where></select>单条件动态查询

添加

主键返回

修改
-  动态修改 

删除





注解完成增删改查
