MyBatis 的XML实现方法(JAVA)

数据库表的结构如下:

DROP DATABASE IF EXISTS test;
CREATE DATABASE test DEFAULT CHARACTER SET utf8mb4;
-- 使⽤数据数据
USE test;
-- 创建表[⽤⼾表]
DROP TABLE IF EXISTS userinfo;
CREATE TABLE `userinfo` (
`id` INT ( 11 ) NOT NULL AUTO_INCREMENT,
`username` VARCHAR ( 127 ) NOT NULL,
`gender` TINYINT ( 4 ) DEFAULT '0' COMMENT '1-男 2-⼥ 0-默认',
`delete_flag` TINYINT ( 4 ) DEFAULT 0 COMMENT '0-正常, 1-删除',
`create_time` DATETIME DEFAULT now(),
`update_time` DATETIME DEFAULT now(),
PRIMARY KEY ( `id` )
) ENGINE = INNODB DEFAULT CHARSET = utf8mb4;

在JAVA中的定义:

@Data
public class User {
    private Integer id;
    private String userName;
    private Integer gender;
    private Integer delete_flag;
    private Date create_time;
    private Date update_time;
}

先在配置文件中连接数据库

# .yml 连接数据库
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/(要连接的数据库的名称)?characterEncoding=utf8&useSSL=false
    username: root
    password: 2002
    driver-class-name: com.mysql.cj.jdbc.Driver

定义接口:

 

@Mapper
public interface UserXMLMapper {
    //增
    Integer add(User user);
}

XML实现接口:

创建一个xml文件,然后在配置文件中配置xml文件的地址。

 

#配置xml文件的路径,resources/mapper包中所有以Mapper.xml结尾的文件
mybatis:
  mapper-locations: classpath:mapper/**Mapper.xml
#配置 打印mybatis的日志
  configuration: 
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

在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="填写要实现的接口的全限定名称">

</mapper>

在mapper标签中插入以下代码

<insert id="add">
    insert into userinfo(username) values (#{userName});
</insert>

id:后面跟的是要实现的接口里面的具体的方法名称

#{}:中直接使用User对象的属性名来获取参数

进行单元测试

@SpringBootTest
class UserXMLMapperTest {
    @Autowired
    private UserXMLMapper userXMLMapper;
    @Test
    void add() {
        User user = new User();
        user.setUserName("zhangsan");
        userXMLMapper.add(user);
    }
}

因为数据库中数据太少了,所以在删除之前先添加一些数据

xml的实现代码如下

<delete id="delete">
    delete from userinfo where id = #{id};
</delete>

进行单元测试

@SpringBootTest
class UserXMLMapperTest {
    @Autowired
    private UserXMLMapper userXMLMapper;

    @Test
    void delete() {
        userXMLMapper.delete(3);
    }
}

查询的xml代码如下

<select id="find" resultType="com.example.Spring_demo.mySQL.User">
    select * from userinfo;
</select>

因为数据库返回的数据需要和JAVA进行映射所以resultType后面就是要映射的类的全限定名称。

单元测试

@SpringBootTest
class UserXMLMapperTest {
    @Autowired
    private UserXMLMapper userXMLMapper;

    @Test
    void find() {
        List<User> list = userXMLMapper.find();
        System.out.println(list.toString());
    }
}

大多数情况下数据库中的参数名和JAVA中的参数名是不相同的,因为数据库一般使用_分隔单词,而JAVA中是使用驼峰命名。 

所以在大多数情况下数据库中的参数名和JAVA中的参数名并不是和我上面的例子一样是相同的,它们的对应关系应该是这样:

 而代码执行的结果: 

后面的三个变量都无法获取返回值。

结果映射

解决办法有三种

1. 起别名

就是利用sql语句将返回结果的列名改的和类中的属性名一致。

<select id="find" resultType="com.example.Spring_demo.mySQL.User">
    select id,username,gender,delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from userinfo;
</select>

2. 结果映射
<mapper namespace="com.example.Spring_demo.mySQL.UserXMLMapper">
    <resultMap id="map" type="com.example.Spring_demo.mySQL.User">
<!--        <id></id>标签只能应用于主键-->
        <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>

    <select id="find" resultMap="map">
        select * from userinfo;
    </select>
</mapper>

 

3. 开启驼峰命名

在配置文件中加入以下代码:

#yml文件
mybatis:
  configuration:
    map-underscore-to-camel-case: true #配置驼峰⾃动转换

修改前

修改的xml代码为

<update id="update">
    update userinfo set username=#{name} where id = #{id};
</update>

单元测试

@SpringBootTest
class UserXMLMapperTest {
    @Autowired
    private UserXMLMapper userXMLMapper;

    @Test
    void update() {
        userXMLMapper.update(1,"wangwu");
    }
}

版权声明:本文为博主作者:《嗯》原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/2302_76339343/article/details/135741867

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2024年2月19日
下一篇 2024年2月19日

相关推荐