详解MyBatis篇三

目录

MyBatis XML配置文件

配置数据库连接字符串和MyBatis

写持久层代码

添加mapper接口

添加xml格式文件

在resources路径下创建xml格式文件

增删改查操作

增加(Insert)

参数重命名 

返回自增id

删除(Delete)

修改(Update)

查询(Select)

属性名称不同的解决方法

多表查询


MyBatis XML配置文件

MyBatis的开发有两种方式:注解;XML。

上一篇文章详细讲解了注解的方式,接下来讲解XML的方式。

使用MyBatis的注解方式主要是来完成一些简单的增删改查功能,如果需要实现复杂的SQL功能,建议使用XML来配置映射语句,也就是将SQL语句写在XML配置文件中。

MyBatis XML的方式需要以下两步:

1.配置数据库连接字符串和MyBatis

2.写持久层代码

配置数据库连接字符串和MyBatis

此步骤需要进⾏两项设置,数据库连接字符串设置和 MyBatis 的 XML ⽂件配置。
如果是application.yml⽂件, 配置内容如下:

# 数据库连接配置
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=false
    username: root
    password: hxwhly2.
    driver-class-name: com.mysql.cj.jdbc.Driver


mybatis:
  # 配置 mybatis xml 的⽂件路径,在 resources/mapper 创建所有表的 xml ⽂件
  mapper-locations: classpath:mybatis/**Mapper.xml

如果是application.properties⽂件, 配置内容如下:

#驱动类名称
  spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  #数据库连接的url
  spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis_test?
  characterEncoding=utf8&useSSL=false
  #连接数据库的⽤⼾名
  spring.datasource.username=root
  #连接数据库的密码
  spring.datasource.password=root
  # 配置 mybatis xml 的⽂件路径,在 resources/mapper 创建所有表的 xml ⽂件
  mybatis.mapper-locations=classpath:mapper/**Mapper.xml
写持久层代码

持久层代码分为两部分:

1.方法定义 interface

2.方法实现 XXX.xml 

添加mapper接口
@Mapper
public interface UserInfoXmlMapper {
    Integer insert(UserInfo userInfo);
}
添加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.wmh.mybatisdemo.mapper.UserInfoXmlMapper">

    <select id="selectAllUser"  resultType="com.wmh.mybatisdemo.model.UserInfo">
         select * from userinfo
    </select>
</mapper>

<mapper>标签:需要指定namespace属性,表示命名空间,值为mapper接口的全限定名。

<select>查询标签:使用率执行数据库的查询操作的

<id>:是和 interface接口中定义的方法名称是一样的,表示对接口的具体方法实现

<resultType>:是返回的数据类型。

在resources路径下创建xml格式文件

注意:

1.配置文件中的classpath指的是resources;

2.配置文件中的mybatis指的是resources目录下的mybatis目录;

3.配置文件中的**Mapper.xml相当于resources的mybatis下的所有以Mapper.xml格式结尾的文件。

增删改查操作
增加(Insert)

mapper接口

@Mapper
public interface UserInfoXmlMapper {
    Integer insert(UserInfo userInfo);
}

xml实现

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

 Test代码

@SpringBootTest
class UserInfoXmlMapperTest {
    @Autowired
    private UserInfoXmlMapper userInfoXmlMapper;

    @Test
    void insert() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("zhaoliu");
        userInfo.setPassword("zhaoliu");
        userInfo.setAge(12);
        userInfo.setGender(1);
        userInfoXmlMapper.insert(userInfo);
    }
}

运行结果:

参数重命名 

如果使⽤@Param设置参数名称的话, 使⽤⽅法和注解类似
UserInfoMapper接⼝:

Integer insert(@Param("userinfo") UserInfo userInfo);
​
<insert id="insert">
    insert into userinfo (username, password, age, gender)
    values (#{userinfo.username}, #{userinfo.password}, #{userinfo.age}, #{userinfo.gender})
</insert>
返回自增id

mapper接口不变,只需xml文件增加useGeneratedKeys和keyProperty即可。

<insert id="insert" useGeneratedKeys="true" keyProperty="id">
    insert into userinfo (username, password, age, gender)
    values (#{username}, #{password}, #{age}, #{gender})
</insert>
删除(Delete)

mapper接口

@Mapper
public interface UserInfoXmlMapper {
    Integer delete(Integer id);
}

xml实现 

<delete id="delete">
    delete from userinfo where id = #{id}
</delete>
修改(Update)

mapper接口

@Mapper
public interface UserInfoXmlMapper {
    Integer update(UserInfo userInfo);
}

xml实现 

<update id="update">
    update userinfo set password = #{password} where id =#{id}
</update>
查询(Select)

mapper接口

@Mapper
public interface UserInfoXmlMapper {
    List<UserInfo> selectAllUser();
}

xml实现

<select id="selectAllUser"  resultType="com.wmh.mybatisdemo.model.UserInfo">
    select * from userinfo
</select>

Test代码

运行结果:

我们注意到,delete_flag, create_time, update_time这几个属性是没有被赋值的,但是其它项都有被赋值,为什么呢?我们看一下数据库表结构和UserInfo属性:

此时,我们不难想到,大概是因为数据库表结构和UserInfo属性名称不一致的原因,当数据库表结构和UserInfo属性名称一致时,才能被成功赋值;否则,就会赋值失败。

属性名称不同的解决方法

1.起别名

2.结果映射

3.开启驼峰命名

其中1和3的解决方法和注解一样,下面讲解xml如何进行结果映射。

<resultMap id="BaseMap" type="com.wmh.mybatisdemo.model.UserInfo">
    <id column="id" property="id"></id>
    <result column="delete_flag" property="deleteFlag"></result>
    <result column="create_time" property="createTime"></result>
    <result column="update_time" property="updateTime"></result>
</resultMap>

解释:

多表查询

ArticleInfo

@Data
public class ArticleInfo {
    private Integer id;
    private String title;
    private String content;
    private Integer uid;
    private Integer deleteFlag;;
    private Date createTime;
    private Date updateTime;
}

mapper接口

@Mapper
public interface ArticleInfoMapper {
    ArticleInfo selectArticleAndUserById(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.wmh.mybatisdemo.mapper.ArticleInfoMapper">

    <select id="selectArticleAndUserById" resultType="com.wmh.mybatisdemo.model.ArticleInfo">
        select ta.*, tb.username, tb.gender from articleinfo ta
        left join userinfo tb
        on ta.uid = tb.id
        where ta.id = #{id}
    </select>

</mapper>

数据库中userinfo表和 articleinfo表信息:

articleinfo表:

userinfo表: 

运行结果:

一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论 37
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新绿MEHO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值