当前位置: 首页 > news >正文

MyBatis XML操作

目录👑

配置

写持久层代码

mapper接口

数据持久层的实现,.xml的形式。

select

字段映射

MyBatis配置驼峰命名⭐

起别名

结果映射

含参数查询

insert

delete

update


配置

需要配置MySQL驱动类、登录名、密码、数据库连接字符串。

这里是在application.yml中配置

spring:datasource:url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=trueusername: rootpassword: **** # 如果密码password是纯数字的,需要加上" "否则会报错。driver-class-name: com.mysql.cj.jdbc.Drivermybatis:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 配置打印 MyBatis⽇志map-underscore-to-camel-case: true #配置驼峰⾃动转换mapper-locations: classpath:mapper/*.xml 

 mapper-locations: classpath:mapper/*.xml 
  这个是设置创建mybatis XML文件的路径, 在resources下的mapper的所有.xml文件

写持久层代码

mapper接口

import com.bit.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;import java.util.List;
@Mapper
public interface UserInfoXmlMapper {List<UserInfo> selectAll();//alt+enter
}

数据持久层的实现,.xml的形式。

这是MyBatis xml的固定格式

<?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 namespace="com.bit.mybatis.mapper.UserInfoXmlMapper"> <!-- 路径+类名,表示要实现的是谁--></mapper>

namespace的内容要和实现的mapper所在路径+接口名。

不一样可能就会出现绑定异常

MyBatisX插件可以将对应的xml和mapper接口关联起来,

点击小鸟就能互相跳转。

select

查询所有用户的具体实现

<?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 namespace="com.bit.mybatis.mapper.UserInfoXmlMapper"> <!-- 路径+类名,表示要实现的是谁--><select id="selectAll" resultType="com.bit.mybatis.model.UserInfo">select * from user_info
-- select id, username, password, age, gender, phone, delete_flag as deleteFlag ,create_time as createTime, update_time as updateTime from user_info;</select>
</mapper>

这里用到的数据库表和实体类都和上篇文章中用到的一样。

我们下载了MyBatisX插件后,在mapper接口的方法上按alt+enter,就会在对应xml的<mapper></mapper>标签之中生成

<select id="selectAll" resultType="com.bit.mybatis.model.UserInfo"></select>

select表示用来执行数据库查询操作,id对应接口中具体的方法,resultType对应返回的数据类型 也就是实体类的路径。

我们可以直接在select标签中写sql语句。

字段映射

MyBatis配置驼峰命名⭐
mybatis:configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 配置打印 MyBatis⽇志map-underscore-to-camel-case: true #配置驼峰⾃动转换

这和MyBatis注解开发中一样,配置了之后,mybatis会自动将数据库表中含下划线的字段名转换成小驼峰的形式,这样就能映射成功。

这是最推荐的方式。

起别名

在写sql语句时给含下划线的字段取一个别名

  <select id="selectAll" resultType="com.bit.mybatis.model.UserInfo">
--         select * from user_infoselect id, username, password, age, gender, phone,delete_flag as deleteFlag ,
create_time as createTime, 
update_time as updateTime 
from user_info;</select>

结果映射

<?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 namespace="com.bit.mybatis.mapper.UserInfoXmlMapper"> <!-- 路径+类名,表示要实现的是谁--><resultMap id="BaseMap" type="com.bit.mybatis.model.UserInfo">--><result column="delete_flag" property="deleteFlag"></result><result column="create_time" property="createTime"></result><result column="update_time" property="updateTime"></result></resultMap><select id="selectAll" resultMap="BaseMap">select * from user_info</select> </mapper>

<resultMap>里column表示表中的字段,property中表示字段对应的名字。

select标签中resultMap的内容和要对应的resultMap中的id一样。

resultMap中的type对应返回的数据类型,也就是实体类的路径。

含参数查询

import com.bit.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;import java.util.List;
@Mapper
public interface UserInfoXmlMapper {UserInfo selectById(Integer id);
}
  <select id="selectById" resultType="com.bit.mybatis.model.UserInfo">select * from user_info where id = #{id};</select>
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.*;@SpringBootTest
class UserInfoXmlMapperTest {@Autowiredprivate UserInfoXmlMapper userInfoXmlMapper;@Testvoid selectById() {System.out.println(userInfoXmlMapper.selectById(1));}
}

insert

import com.bit.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;import java.util.List;
@Mapper
public interface UserInfoXmlMapper {Integer insertUser(UserInfo userInfo);
}

如果insertUser的参数用@param重命名,比如insertUser(@Param("user")UserInfo userInfo),

sql语句中values后面就是(#{user.username},#{user.password},#{user.age})

<insert id="insertUser">insert into user_info(username, password,age) values (#{username},#{password},#{age})</insert>

测试

package com.bit.mybatis.mapper;import com.bit.mybatis.model.UserInfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.*;@SpringBootTest
class UserInfoXmlMapperTest {@Autowiredprivate UserInfoXmlMapper userInfoXmlMapper;@Testvoid insertUser() {UserInfo userInfo = new UserInfo();userInfo.setAge(99);userInfo.setUsername("kdfj");userInfo.setPassword("809889");Integer result =  userInfoXmlMapper.insertUser(userInfo);System.out.println("受影响的条数:" + result);}
}

insert在xml中和用注释一样还是能获取到递增主键的值。

<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">insert into user_info(username, password,age) values (#{username},#{password},#{age})
</insert>

在insert中添加useGeneratedKeys="true" keyProperty="id",

@Test
void insertUser() {UserInfo userInfo = new UserInfo();userInfo.setAge(35);userInfo.setUsername("喜羊羊");userInfo.setPassword("eefd9");Integer result =  userInfoXmlMapper.insertUser(userInfo);System.out.println("受影响的条数:" + result + ",id:" + userInfo.getId());
}

尽管userInfo没有通过set方法来设置id,还是可以获取到id的值。

delete

import com.bit.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;import java.util.List;
@Mapper
public interface UserInfoXmlMapper {Integer deleteById(Integer id);
}
<?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 namespace="com.bit.mybatis.mapper.UserInfoXmlMapper"><!-- 路径+类名,表示要实现的是谁--><delete id="deleteById">delete from user_info where id = #{id}</delete>
</mapper>

测试

package com.bit.mybatis.mapper;import com.bit.mybatis.model.UserInfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.*;@SpringBootTest
class UserInfoXmlMapperTest {@Autowiredprivate UserInfoXmlMapper userInfoXmlMapper;@Testvoid deleteById() {Integer result =  userInfoXmlMapper.deleteById(2);System.out.println("受影响的条数:" + result);}
}

update

import com.bit.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;import java.util.List;
@Mapper
public interface UserInfoXmlMapper {Integer updateById(String username,Integer id);
}
<update id="updateById">update user_info set username=#{username} where id = #{id}</update>
    @Testvoid updateById() {Integer result =  userInfoXmlMapper.updateById("小美",8);System.out.println("受影响的条数:" + result);


文章转载自:

http://KrZeolVv.stbhn.cn
http://Guo1m9zM.stbhn.cn
http://uP8Qwp3U.stbhn.cn
http://biaXPBqX.stbhn.cn
http://HjF8ZZ0c.stbhn.cn
http://orB0ioiY.stbhn.cn
http://nteV9FaF.stbhn.cn
http://PH3An5NK.stbhn.cn
http://NRZjwjpJ.stbhn.cn
http://pvu344gH.stbhn.cn
http://MocAZvsu.stbhn.cn
http://LDFBF1l5.stbhn.cn
http://a5zIGxY3.stbhn.cn
http://AxuslsXU.stbhn.cn
http://rRLPyOAX.stbhn.cn
http://8KCuZPrs.stbhn.cn
http://xriCurQw.stbhn.cn
http://6XfVnclb.stbhn.cn
http://ylRcxhGR.stbhn.cn
http://VYmMYCtV.stbhn.cn
http://jTlIAs3Y.stbhn.cn
http://eb7vzmVv.stbhn.cn
http://nmc2qzPm.stbhn.cn
http://0Jwj91f7.stbhn.cn
http://8et1GXmp.stbhn.cn
http://Eyz6Dr6v.stbhn.cn
http://5fLGm9ad.stbhn.cn
http://ouAiWWWQ.stbhn.cn
http://E7qKAD3F.stbhn.cn
http://OJMyJs6i.stbhn.cn
http://www.dtcms.com/a/386708.html

相关文章:

  • 3DGS压缩-Knowledge Distillation for 3DGS
  • 宇视设备视频平台EasyCVR视频设备轨迹回放平台监控摄像头故障根因剖析
  • Mysql 主从复制操作
  • 2.Boost工作原理分析
  • 专题一递归算法
  • 精准选中对象
  • 制作uniapp需要的storyboard全屏ios启动图
  • 嵌入式硬件工程师的每日提问(2)
  • 清华最新发布114页大型推理模型的强化学习综述
  • 软件质量保证(SQA)和测试的关系
  • 22.1 突破单卡显存极限!DeepSpeed ZeRO实战:用1块GPU训练百亿参数大模型
  • 框架-SpringCloud-1
  • Redis 与微服务架构结合:高并发场景下的架构艺术
  • g4f 0.6.2.9版本安装以及服务不太稳定的问题探究
  • I2C通信
  • 经典算法题之x 的平方根
  • 【精品资料鉴赏】RPA财务机器人应用(基于UiPath)教材配套课件
  • 融合A*与蚁群算法的室内送餐机器人多目标路径规划方法研究
  • RustDesk:免费开源的跨平台远程桌面控制软件
  • 超越NAT:如何构建高效、安全的内网穿透隧道
  • RabbitMQ理解
  • 【闪电科创】边缘计算深度学习辅导
  • Linux服务器中Mysql定时备份(清理)数据库
  • 物联网智能网关配置教程:实现注塑机数据经基恩士PLC上传至云平台
  • 搭建第一个Spring Boot项目
  • MyBatis 注解操作
  • InternVL3.5 开源:革新多模态架构,重塑感知与推理的边界​
  • 新手教程—LabelImg标注工具使用与YOLO格式转换及数据集划分教程
  • C++奇异递归模板模式(CRTP)
  • 国产数据库地区分布,北京一骑绝尘