Mybatis框架下的增删改查
一、什么是Mybatis
1、半自动化的ORM框架(把数据库中的关系数据映射为程序中的对象)
1)编写SQL语句需要手写
2)参数动态映射
2、Mybatis配置
1)依赖的jar包
mysql-connector
mybatis
2)核心配置
##configuration
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> </configuration>
##mapper
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper> </mapper>
配置路径如图:

二、mybatis基本框架
需要满足动静分离的特点(且resources与java路径要一致)
datasources.properties写联系数据库的url

在动静分离的基础上,需要保持resources与java的路径一致,接口和mapper文件名保持一致(大小写区分),否则方法不能映射到mapper中,同时sql语句在mapper中编写 
三、SQL语句的两种编写方式
1、在mapper里面编写
1)查询所有
对于sql语句中的id,关联接口中定义的方法名,resultType关联实体类
<select id="findAll" resultType="VStuScore">
select * from vstuscore
</select>

注意:当出现实体类定义的参数与数据库中不同名时或者数据类型不一致时,可以在mapper里面进行修改,如下:

2)条件查询
条件查询需要用到where与if标签,其中where标签可以去掉第一个 and ,if标签配合 test 使用
<select id="findBy" resultType="VStuScore">
select * from vstuscore
<where>
<if test="null != deptName">
and deptName=#{deptName}
</if>
<if test="null != subName">
and subName like concat(#{subName},'%')
</if>
<if test="null != score">
and score>=#{score}
</if>
</where>
</select>
3)添加
添加分为单个添加和多个添加两种,其中添加多个需要利用到循环foreach,foreach中的collection定义方式为array/list/map,item为别名,seperator以逗号分隔
<!--单个-->
<insert id="add">
insert into scoreinfo(stuId,subId,score) value(#{stuId},#{subId},#{score})
</insert>
<!--多个-->
<insert id="addAll">
insert into scoreinfo(stuId,subId,score) values
<foreach collection="array" item="sco" separator=",">
(#{sco.stuId},#{sco.subId},#{sco.score})
</foreach>
</insert>
4)删除与修改
与查询与增加相似,删除需要循环的是主键,以主键来找到信息进行删除,而修改利用到set标签,在修改时去掉最后一个逗号
<!--where标签可以在删除时去掉第一个and-->
<delete id="remove">
delete from scoreinfo
<where>
id in
<foreach collection="array" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</where>
</delete>
<!--set标签可以在修改时去掉最后一个逗号-->
<update id="modify">
update scoreinfo
<set>
<if test="null != stuId">
stuId=#{stuId},
</if>
<if test="null != subId">
subId=#{subId},
</if>
<if test="null != score">
score=#{score},
</if>
</set>
where
id=#{id}
</update>
5)查询存储过程
由于存储过程有in out inout 三种参数,而java默认mode为in,对于out类型,后面需要加上对应的数据库类型,对于存储过程这里有两种解决方式
第一种是用Map来做,不需要建立实体类,而第二种建实体类进行映射(推荐第二种,对于数据量较少的情况实体类所占内存远远小于用Map接口的方式)
<!--mode默认是in 对于Out的形式 后面需要加上对应的数据库类型-->
<select id="findStuByPage" parameterType="map" resultType="VStudentInfo" statementType="CALLABLE">
{ call proPageStu(#{total,mode=OUT,jdbcType=INTEGER},#{pageNo},#{pageSize}) }
</select>
<select id="findStuByPage2" parameterType="PageParam" resultType="VStudentInfo" statementType="CALLABLE">
{ call proPageStu(#{total,mode=OUT,jdbcType=INTEGER},#{pageNo},#{pageSize}) }
</select>
2、对于简单的sql语句可以直接在接口内定义
对于简单的sql语句 无需用到其他标签,直接在接口的方法上作注解,写sql语句即可(增删查),对于两个以上的参数,需要利用@Param注明
@Insert("insert into subjectinfo(proId,subName,classHours) value(#{proId},#{subName},#{classHours})")
int addSub(@Param("proId") int proId,@Param("subName") String subName, @Param("classHours") int classHours);
四、Mybatis的核心对象
1)配置文件解析
InputStream config = Resources.getResourceAsStream(String path);
2)SQL会话工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(config);
3)SQL会话 open(true:自动提交/false:不自动提交(启动事务))
SqlSession session = factory.openSession(true);
4)创建会话对象
XXX mapper = session.getMapper(XXX.class);
5)对象调用方法
List<entity> list = mapper.findAll();
浙公网安备 33010602011771号