MyBatis
能力不足,只能写到这种程度了,等我能力够了,我会继续更新,
#MyBatis由来
#MyBatis功能
#MyBatis编写流程:配置开发环境(jar包),写mapper接口(抽象方法),写mapper映射文件(Mapper标签)
mybati使用介绍
jar包
#SSM框架:mybatis、mybatis-spring
#SpringBoot框架:mybatis-spring-boot-starter
#额外所需jar包:数据库驱动(mysql-connector-java)、数据库操作(spring-jdbc)、连接池(dbcp|druid)
数据库驱动:Java连接MySQL软件,使得Java能够对数据库数据进行CRUD操作(前提:Java能够访问MySQL)
数据库操作:spring支持JDBC操作,使得CRUD操作更加简易,提升开发效率
连接池:保存已经创建好的连接,省略了创建连接和销毁连接的过程,提高访问性能(数百万的访问量)
抽象方法
#建议:新建泛型接口,编写抽象方法,继承泛型接口,编写子类特有的抽象方法
List<T> selectAll();
T selectById(int id);
void insert(T t);
void update(T t);
void delete(int id)
mapper标签
<mapper namespace="接口名"> #文件
<resultMap id="唯一标识名" type="实体类名"> #映射(结果集字段|实体类属性)
<id column="表字段名" jdbcType="属性类型" property="实体类属性名"/> #主键字段映射
<result column="表字段名" jdbcType="属性类型" property="实体类属性名"/> #非主键字段映射
<collection property="实体属性名" javaType="属性类型" ofType="实体类名" column="表字段名"> #关联表映射
<id column="表字段名" jdbcType="属性类型" property="实体类属性名"/> #主键字段映射
<select id="接口方法名" parameterType="参数类型名" resultMap="指定标识符名"> #查询
<insert id="接口方法名" parameterType="参数类型名"> #新增
<update id="接口方法名" parameterType="参数类型名"> #更新
<delete id="接口方法名" parameterType="参数类型名"> #删除
.... .....
mybatis使用实例
pom文件
内容以后补上
entity类
内容以后补上
mapper接口
内容以后补上
mapper映射文件
MyBatis–SQL
新增
新增所有字段,包括id
<!--患者新增:新增 patientAdd-->
<insert id="patientAdd" parameterType="Patient">
insert into patient values(#{patientId},#{createTime})
</insert>
删除
<!--患者删除:删除 patientDelete-->
<delete id="patientDelete" parameterType="Integer">
delete from patient where patient_id = #{patientId}
</delete>
修改
后期修改单个字段的值
<!--患者更改:更新 patientUpdate-->
<update id="patientUpdate" parameterType="Patient">
update patient set create_time=#{createTime}
where patient_id = #{patientId}
</update>
<!--给患者绑定所属测评站-->
<update id="patientStation" parameterType="INTEGER">
update patient set
station_id = #{stationId}
</update>
单表查询
<!--患者详情:查询 patientDetail-->
<select id="patientDetail" parameterType="Integer" resultMap="BaseResultMap">
select * from patient where patient_id = #{patientId}
</select>
关联查询
查询条件使用逗号分割,使用left join,使用模糊查询
<!--患者一览:查询 patientList-->
<select id="patientList" parameterType="Patient" resultMap="BaseResultMap">
select p.*,d.*,o.*,s.* from patient p
left join doctor d on p.doctor_id = d.doctor_id
left join occupation o on p.occupation_id = o.occupation_id
left join station s on p.station_id = s.station_id
where 1=1
<if test="name != null">
and p.name like concat ('%',#{name},'%')
</if>
<if test="sex != null">
and p.sex like concat ('%',#{sex},'%')
</if>
<if test="age>0">
and p.age = #{age}
</if>
<if test="stationId>0">
and p.station_id like concat ('%',#{stationId},'%')
</if>
<if test="doctorId > 0">
and p.doctor_id like concat ('%',#{doctorId},'%')
</if>
<if test="occupationId > 0">
and p.occupation_id like concat ('%',#{occupationId},'%')
</if>
<if test="dictId != null">
and p.dict_id like concat ('%',#{dictId},'%')
</if>
order by patient_id desc
</select>
批处理
批量删除
Integer batchDel(Integer[] patientIds);
<delete id="batchDel" parameterType="int[]">
delete from tb_patient where patientId in
<foreach collection="array" item="arr" index="no" open="(" separator="," close=")">
#{arr}
</foreach>
</delete>
Integer batchDel(List patientIds);
Integer batchDel(List patientIds); // 或者
<delete id="getMemberListById" parameterType="java.util.List" >
delete from tb_patient where patientId in
<foreach collection="array" item="arr" index="no" open="(" separator="," close=")">
#{arr}
</foreach>
</delete>