一.增删改查
1.查询数据
<select id="login" resultType="Admin">
select * from admin where account = #{account} and password = #{password}
</select>
2.删除数据
<delete id="deleteAdmin" parameterType="int">
delete from admin where id = #{id}
</delete>
3.增加数据
<insert id="saveAdmin" parameterType="Admin" useGeneratedKeys="true"
keyColumn="id" keyProperty="id">
insert into admin (account,password,gender)
values (#{account},#{password},#{xb})
</insert>
4.修改数据
<update id="updateAdmin" parameterType="Admin">
update admin set account=#{account},
password=#{password},gender=#{xb} where id = #{id}
</update>
补充:
#{}和${}的区别
#{}:占位符,是经过预编译的,编译好SQL语句在取值,#方式能够防止sql注入
${}:拼接符,会传入参数字符串,取值后再去编译SQL语句,$方式无法防止sql注入${}
注意:Mybatis排序时使用order by 动态参数时需要注意,用$而不是#
二.映射
简单类型输出类型
返回简单基本类型
对象映射
如果表中的类名与类中的属性名完全相同,mybatis会自动将查询结果封装到POJO对象中
如果java中使用标准驼峰命名,数据库中使用下划线连接命名,可以开始全局设置实现自动转换
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--从经典的数据命名a_column 转为java的驼峰命名 aColumn-->
特殊处理定义 resultMap
定义resultMap
<resultMap id="studentMap" type="Student">
<!--id标签映射主键-->
<id column="id" property="id"></id>
<!--result 映射普通属性-->
<result column="no" property="no"></result>
<result column="name" property="name"></result>
<result column="gender" property="gender"></result>
<!--association 映射关联对象 会创建关联对象,将数据封装到关联对象中-->
<association property="grade" javaType="Grade">
<result column="gname" property="name"></result>
</association>
<association property="admin" javaType="Admin">
<result column="account" property="account"></result>
</association>
</resultMap>
(1).resultMap的id属性是resultMap的唯一
(2).id属性是映射的POJO类
(3).id标签映射主键,result标签映射非主键
(4).property设置POJO的属性名称,column映射查询结果的列名称
使用resultMap
<select id="findStudentByid" resultMap="studentMap">
<include refid="selectStudent"></include>
WHERE s.id = #{id}
</select>
(1).输出映射使用的是resultMap ,而不是resultType
(2).resultMap引用了userResultMap
多表关联处理结果集
resultMap元素中association ,collection元素
Collection关联元素处理一对多关系
<resultMap id="Studentmap" type="student">
<id column="sid" property="id"></id>
<result column="no" property="no"></result>
<result column="sname" property="name"></result>
<association property="grade" javaType="Grade">
<id column="gid" property="id"></id>
<result column="gname" property="name"></result>
</association>
</resultMap>
<select id="getGradeByid" resultMap="Studentmap">
SELECT
s.id sid,
s.no,
s.name sname,
g.id gid,
g.name gname
FROM
student s
LEFT JOIN grade g
ON s.gradeid = g.id
WHERE s.id = #{id}
</select>
嵌套查询
<resultMap id="Studentmap1" type="Student">
<id column="id" property="id"></id>
<!--result 映射普通属性-->
<result column="no" property="no"></result>
<result column="name" property="name"></result>
<result column="gender" property="gender"></result>
<collection property="grade" javaType="Grade" select="findGradeList" column="gradeid"></collection>
</resultMap>
<select id="findStudentList1" resultMap="Studentmap1">
SELECT
s.id,
s.no,
s.name,
s.gender,
s.gradeid
from student s
</select>
<select id="findGradeList" resultType="Grade">
select * from grade where id = #{gradeid}
</select>
(1).columu是关联查询时将"gradeid"传入到findGradeList中,并将findGradeList查询结果映射到Grade里的student属性中去
(2).collection和association都需要配置select和column属性,两者配置方法相同
三.注解
@Insert : 插入sql,和xml insert sql 语法完全一样
@Insert("insert into grade(name)value(#{name})")
int saveGrade(Grade grade);
@Select : 查询sql,和xml select sql语法完全一样
@Update : 更新sql,和xml update sql语法完全一样
@Delete : 删除sql,和xml delete sql语法完全一样
@Delete("delete from grade where id =#{id}")
int deleteGrade(Grade grade);
@Param : 入参
@Results : 设置结果集合
@Result : 结果
四.Mybatis动态SQL
if元素
<select id="findStudentList" parameterType="Student" resultType="Student">
select * from student
<trim prefix="where" prefixOverrides="and | or">
<if test="no!=null & no!=''">
no=#{no}
</if>
<if test="name!=null & name!=''">
and name=#{name}
</if>
<if test="gender!=null & gender!=''">
and gender=#{gender}
</if>
</trim>
</select>-->
if test="条件"
where 一旦where标签中的if有成立的条件,会动态的添加where关键字
一旦有像and or这样的关键字,可以动态删除
trim元素
trim prefix="自定义的前缀" prefixOverrides="覆盖指定的关键字" suffix="自定义的后缀" suffixOverrides =""
Choose元素
<select id="findStudentList" parameterType="Student" resultType="Student">
select * from student
<trim prefix="where" prefixOverrides="and | or">
<choose>
<when test="name!=null">
name=#{name}
</when>
<otherwise>
name="李四"
</otherwise>
</choose>
</trim>
</select>
otherwise 不成立执行otherwise
set元素
<update id="updateStudent" parameterType="Student">
update student
<set>
<if test="name!=null">
name = #{name},
</if>
<if test="gender!=null">
gender = #{gender},
</if>
<if test="phone!=null">
phone = #{phone}
</if>
</set>
where id = #{id}
</update>
Set元素可以把最后一个逗号去掉
foreach元素
<select id="findStudentList1" resultType="Student">
select * from student where no in
<foreach collection="list" item="no" open="(" separator="," close=")">
#{no}
</foreach>
</select>
foreach可以在sql中进行迭代一个集合
item 表示集合中的每一个元素进行迭代时的别名
index 指定一个名字,用于表示在迭代过程中,每次迭代到的位置
open表示该语句以什么考试
separator表示在每次进行迭代之间以什么符号作为分隔符
close表示以什么结束
collection 如果传入的参数类型时List时,collection属性值为list
如果传入的参数类型为array数组的时候。collection属性值为array
五.特殊符号处理
特殊字符 转义字符
< <
> >
" "
' '
& &
也可以使用<![CDATA[]]>包裹特殊字符
<select id="findStudentList3" resultType="com.ffyc.mybatisdemo.model.Student">
select * from student where no <![CDATA[ < ]]> 1005
</select>
五.缓存
缓存的概念
作用:是为了减去数据库的压力,提高查询性能
原理:是从数据库中查询出来的对象在使用完后不要销毁,而是存储在内存中,当在此需要获取该对象是,直接从内存中直接获取,不在像数据库执行select语句,从而减少了对数据库的查询次数,因此提高了数据库的性能。
Mybatis有一级缓存和二级缓存
一级缓存
@Test
public void cacheDemo(){
SqlSession sqlSession = MyBatisUtil.getsqlSessionFactory();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
Student student = new Student();
student.setId(2);
student.setName("李四");
List<Student> students = studentDao.findStudentList(student);
System.out.println(students);
// studentDao.updateStudent(student);//增删操作后,会清空一级缓存
// sqlSession.clearCache(); //清空一级缓存
System.out.println("===============================");
student.setName("李四");
List<Student> students1 = studentDao.findStudentList(student);
System.out.println(students1);
sqlSession.close(); //关闭sqlsession会清空一级缓存
}
二级缓存
(1).启用缓存
<!--启用全局的二级缓存-->
<setting name="cacheEnabled" value="true"/>
(2).对象序列化
将所有的POJO类实现序列化接口Java.io.Serializable
(3).再Mapper映射文件中添加<cache/>表示此mapper开启二级缓存
当SqlSession关闭时,会将数据存入到二级缓存