目录
一.无参查询
1.新增
<!--int insertUser();--> //接口方法
<insert id="insertUser">
insert into t_user values(null,'小米','男','10','smoking')
</insert>
2.删除
<!--int deleteUser();-->//接口方法
<delete id="deleteUser">
delete from t_user where username='小米'
</delete>
3.修改
<!--int updateUser();-->//接口方法
<update id="updateUser">
update t_user set username='小布' , age=11 where username='小米'
</update>
4.查询一个实体类
<!--User getUserById();-->//接口方法
<select id="getUserById" resultType="com.atguigu.mybatis.bean.User"> // resultType实体类的类型
select * from t_user where username='小米'
</select>
5.查询一个list集合
<!--List<User> getUserList();--> //接口方法
<select id="getUserList" resultType="com.atguigu.mybatis.bean.User"> //对象
select * from t_user
</select>
二.MyBatis获取参数值的两种方式
方式一:#{}
#{}这种方式表示的意思是占位符号来替换 ?
方式二:${}
${}这种方式是字符串拼接的方式进行拼接sql语句
注意:
此时可以使用${}和#{}以任意的名称获取参数的值,注意${}需要手动加单引号
通过@Param注解标识mapper接口中的方法参数 此时,会将这些参数放在map集合中,以@Param注解的value属性值为键,以参数为值;以 param1,param2...为键,以参数为值;只需要通过${}和#{}访问map集合的键就可以获取相对应 的值
三.有参数查询,
1.通过Id查询一个实体类
<!--User getUserById(@Param("id") int id);-->//这里接口方法,并且用@param规定了键的属性
<select id="getUserById" resultType="User">//resultType:返回一个User实体类
select * from t_user where id=#{id}
</select>
2.查询一个list集合
<!--List<User> getUserList();-->//接口方法
<select id="getUserList" resultType="User">
select * from t_user
</select>
3.查询单个数据的昵称
* 在MyBatis中,对于Java中常用的类型都设置了类型别名
* 例如: java.lang.Integer-->int|integer
* 例如: int-->_int|_integer
* 例如: Map-->map,List-->list
4.查询数量
<!--int getCount();-->//接口方法
<select id="getCount" resultType="_integer"> //返回的类型
select count(*) from t_user
</select>
5. 查询一条数据为map集(一)
<!--Map<String, Object> getUserToMap(@Param("id") int id);-->//接口方法
<select id="getUserToMap" resultType="map">//返回类型
select * from t_user where id = #{} //使用占位符的方式
or select * from t_user where id = '${}' //使用字符串拼接的方式
</select>
map和list有一定的区别,list是一个集合,而map是一个键值对的存储过程
6. 查询一条数据为map集(二)
@MapKey("id")
Map<String, Object> getAllUserToMap();
<select id="getAllUserToMap" resultType="map">
select * from t_user
</select>
//蚕蛹@Mapkey的方式进行查询
三.特殊的Sql执行
1.模糊查询
<!--List<User> testMohu(@Param("mohu") String mohu);-->//接口方法
<select id="testMohu" resultType="User">
方式一
select * from t_user where username like '%${mohu}%'
方式二
select * from t_user where username like concat('%',#{mohu},'%')
方式三
select * from t_user where username like "%"#{mohu}"%"
</select>
#{mohu}里面是需要写入参数的
2.批量删除
<!--int deleteMore(@Param("ids") String ids);-->//接口方法
<delete id="deleteMore">
delete from t_user where id in (${ids})
</delete>
3.设置表名
<!--List<User> getAllUser(@Param("tableName") String tableName);-->
<select id="getAllUser" resultType="User">
select * from ${tableName}
</select>
4.业务主键和自然主键
1.业务主键比较浪费空间
2.业务主键无书顺序,再插入的时候,有可能会导致表的重新排列
3.字符和数字不同意,取模运算比较复杂
5.获取自增功能的主键
//添加用户信息
//useGeneratedKeys="true" 是否开始获取主键
//keyProperty="id" 绑定主键
<!--int insertUser(User user);-->
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
insert into t_user values(null,#{username},#{password},#{age},#{sex})
</insert>
四.自定义映射resultMap
1.若数据库中字段名和实体类中的属性名不一致,则可以通过resultMap来经行优化
resultMap:自定义映射
<resultMap id="userMap" type="User">
<id property="id" column="id"></id>
<result property="userName" column="user_name"></result>
<result property="password" column="password"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
</resultMap>
<!--List<User> testMohu(@Param("mohu") String mohu);-->//接口的方法
<select id="testMohu" resultMap="userMap">
select * from t_user where username like '%${mohu}%'
</select>
对应关系:resultMap---userMap
id:自增主键id
result property:pojo类 column=:数据库的类
type:什么实体类
2.若数据库中字段名和实体类中的属性名不一致,也可以再mybatis-config.xml中导入驼峰配置文件也可以
<settings>
<!--数据库自增驼峰的自动转换-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--懒加载-->
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
3.处理映射1对多和多对1的映射关系的两种方法
association:设置多对一的映射关系
collection:设置一对多的映射关系
4.多对一映射处理
1.两表查询用链表的方式
//如果使用驼峰就没有必要再使用resultMap了
<resultMap id="empDeptMap" type="Emp">
<id column="eid" property="eid"></id>
<result column="ename" property="ename"></result>
<result column="age" property="age"></result>
<result column="sex" property="sex"></result>
<result column="did" property="dept.did"></result>
<result column="dname" property="dept.dname"></result>
</resultMap>
<!--Emp getEmpAndDeptByEid(@Param("eid") int eid);-->//接口方法
<select id="getEmpAndDeptByEid" resultMap="empDeptMap">
select * from t_emp emp left join t_dept dept on emp.did =#{eid}
dept.did where emp.eid = #{eid}
</select>
2.使用association处理映射关系
<resultMap id="empDeptMap" type="Emp">
<id column="eid" property="eid"></id>
<result column="ename" property="ename"></result>
<result column="age" property="age"></result>
<result column="sex" property="sex"></result>
<association property="dept" javaType="Dept">
<id column="did" property="did"></id>
<result column="dname" property="dname"></result>
</association>
</resultMap>
<!--Emp getEmpAndDeptByEid(@Param("eid") int eid);-->
<select id="getEmpAndDeptByEid" resultMap="empDeptMap">
select emp.*,dept.* from t_emp emp left join t_dept dept on emp.did =
dept.did where emp.eid = #{eid}
</select>
3.分布查询
<resultMap id="empDeptStepMap" type="Emp">
<id column="eid" property="eid"></id>
<result column="ename" property="ename"></result>
<result column="age" property="age"></result>
<result column="sex" property="sex"></result>
<!--
select:设置分步查询,查询某个属性的值的sql的标识(namespace.sqlId)
column:将sql以及查询结果中的某个字段设置为分步查询的条件
-->
<association
property="dept"
select="com.atguigu.MyBatis.mapper.DeptMapper.getEmpDeptByStep"
column="did">
</association>
</resultMap>
<!--Emp getEmpByStep(@Param("eid") int eid);-->
<select id="getEmpByStep" resultMap="empDeptStepMap">
select * from t_emp where eid = #{eid}
</select>
另一个表
<!--Dept getEmpDeptByStep(@Param("did") int did);-->
<select id="getEmpDeptByStep" resultType="Dept">
select * from t_dept where did = #{did}
</select>
5.一对多映射处理
1.表连接的方式
<resultMap id="deptEmpMap" type="Dept">
<id property="did" column="did"></id>
<result property="dname" column="dname"></result>
<!--
ofType:设置collection标签所处理的集合属性中存储数据的类型
-->
<collection property="emps" ofType="Emp">
<id property="eid" column="eid"></id>
<result property="ename" column="ename"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
</collection>
</resultMap>
<!--Dept getDeptEmpByDid(@Param("did") int did);-->
<select id="getDeptEmpByDid" resultMap="deptEmpMap">
select dept.*,emp.* from t_dept dept left join t_emp emp on dept.did =
emp.did where dept.did = #{did}
</select>
2.分布查询
<resultMap id="deptEmpStep" type="Dept">
<id property="did" column="did"></id>
<result property="dname" column="dname"></result>
<collection property="emps" fetchType="eager"
select="com.atguigu.MyBatis.mapper.EmpMapper.getEmpListByDid"
column="did">
</collection>
</resultMap>
<!--Dept getDeptByStep(@Param("did") int did);-->
<select id="getDeptByStep" resultMap="deptEmpStep">
select * from t_dept where did = #{did}
</select>
另一张表
<!--List<Emp> getEmpListByDid(@Param("did") int did);-->
<select id="getEmpListByDid" resultType="Emp">
select * from t_emp where did = #{did}
</select>
五.动态的sql
常用的标签:
if:如果通过了if的判断就能执行if 里面的语句
where:1.能自动删除最前面的and 2.where中如果没有标签起作用,那么where就不起作用 3.where标签不能去掉条件最后多余的and
trim:有几个标签1.prefix:可以再最前面添加内容 2.prefixOverrides:可以再最前面去掉内容 3. suffix:在trim标签中的内容的后面添加某些内容 4.suffixOverrides:在trim标签中的内容的后面去掉某些内容
choose
when 和otherwise相当于if..else..
1.添加多条数据
<!--int insertMoreEmp(List<Emp> emps);-->
<insert id="insertMoreEmp">
insert into t_emp values
<foreach collection="emps" item="emp" separator=",">
(null,#{emp.ename},#{emp.age},#{emp.sex},#{emp.email},null)
</foreach>
</insert>
2.删除多条数据
<delete id="deleteMoreByArray">
delete from t_emp where
<foreach collection="eids" item="eid" separator="or">
eid = #{eid}
</foreach>
</delete>
总结:
- sql语句的使用,后期可以利用逆向编译的方法来生成
- 但是要掌握多表操作
- 1对多和多对一应该搞清楚关系
- Mybatis的缓存我就不多说了,Sqlsession一级缓存和SqlsessionFactory二级缓存,还有三级缓存
- mybatis对数据库实行半自动化,方便我们使用