MyBatis

本文详细介绍了MyBatis的起源、功能及其在SSM和SpringBoot框架中的应用。通过配置开发环境、编写Mapper接口和映射文件,展示了如何进行数据库的CRUD操作。内容涵盖抽象方法的编写、Mapper标签的使用,以及MyBatis在实际项目中的应用,包括新增、删除、修改和查询等操作的SQL示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值