mybatis 增删改查小例子

1.查询操作:

1.1单个参数查询:

<select id="selectPerson1ByIdWithMap" parameterType="java.lang.Integer" resultType="java.util.Map">
	select * from person p where p.person_id = #{id}
</select>

1.2查询结果不是实体类:使用resultType

<select id="selectPersonCount" resultType="java.lang.Integer">
		select count(*) from person 
</select>

说明:

resultMap与resultType区别
resultMap:适合使用返回值是自定义实体类的情况
resultType:适合使用返回值的数据类型是非自定义的,即jdk的提供的类型

1.3多个参数查询:参数使用map

	<select id="selectPerson1ByParams" parameterType="map" resultMap="BaseResultMap">
		select * from person p where p.name = #{name} and p.gender = #{gender}
	</select>


1.4查询list实体:

	<!-- 当查询集合的时候resultMap指的是集合中的泛型 -->
	<select id="selectPerson1All" resultMap="BaseResultMap">
		select * from person
	</select>


说明:resultMap用VO类

1.5模糊查询:

<select id="selectPerson1ByLike" parameterType="java.util.Map" resultMap="BaseResultMap">
		select * from person p where p.name like '%${name}%'
</select>	

说明:

1.模糊查询:参数需要用map  

2.符号为:${}    

2.插入:可以返回主键

通过:selectKey标签查询主键:

<insert id="insert" parameterType="person">
		<selectKey keyProperty="personId" order="AFTER" resultType="java.lang.Integer">
			select LAST_INSERT_ID()
		</selectKey>
		insert into person1 (person_id, name, gender, person_addr, birthday)
		values(#{personId}, #{name}, #{gender}, #{personAddr}, #{birthday})
</insert>

selectKey标签:

		keyProperty:实体类中的id属性名,当返回主键的时候就是返回给keyProperty
		order:生成主键和执行insert语句的顺序,在mysql是AFTER,在oracle中使用BEFORE
		resultType:主键返回的数据类型,和实体类中的主键的类型一致
		select LAST_INSERT_ID():mysql中生成主键的sql,oracle中select xxxx.nextval from dual


3.修改操作:

	<update id="update" parameterType="person">
		update person p set p.name = #{name}
		where p.person_id = #{personId} 
	</update>


4.删除操作:
	<delete id="delete" parameterType="java.lang.Integer">
		delete from person  where person_id = #{personId}
	</delete>
说明:mysql中删除不能使用别名

即:不能用下面的形式

	<delete id="delete" parameterType="java.lang.Integer">
		delete from person p  where p.person_id = #{personId}
	</delete>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 下面是一个使用Spring Boot和MyBatis实现增删改查例子: 首先,我们需要创建一个Book实体类,包含书籍的相关属性。然后,我们创建一个BookMapper接口,使用@Mapper注解标记该接口为MyBatis的Mapper接口,并定义了一些常用的数据库操作方法,如查询全部书籍、添加书籍、删除书籍、修改书籍等。接着,我们创建一个BookMapper.xml文件,配置了具体的SQL语句。最后,我们创建一个BookService接口和一个BookServiceImpl实现类,用于处理业务逻辑。 在Controller层,我们创建一个PublishController类,使用@Autowired注解注入PublishService,并定义了一些接口方法,如查询全部书籍、添加书籍、删除书籍、修改书籍等。 在Spring Boot的启动类中,我们使用@SpringBootApplication注解标记该类为Spring Boot的启动类,并使用@MapperScan注解指定Mapper接口所在的包。 这样,我们就可以通过访问Controller层的接口来实现增删改查的功能了。 以上是一个简单的springboot mybatis实现增删改查例子。希望对你有帮助。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* *3* [SpringBoot中的增删改查案例](https://blog.csdn.net/woshishq1210/article/details/130981372)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值