MyBatis XML配置文件(下)

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

        MyBatis XML开发的方式需要以下两步:1、配置数据库连接字符串和MyBatis。2、写持久层代码。

        准备代码:UserInfo实体类        

1. 配置连接字符串和MyBatis

        我们需要进行设置两个步骤,数据库连接字符串设置MyBatis的XML文件配置。如果是application.yml,配置内容如下:

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/mybaties_test_0822?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: 111111
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #配置打印 MyBatis⽇志
    map-underscore-to-camel-case: true  #自动驼峰转换
  mapper-locations: classpath:mapper/**Mapper.xml
    # 配置 mybatis xml 的⽂件路径,在 resources/mapper 创建所有表的 xml ⽂件

        此处注意congratulations和mapper-locations是同一级标题; 

2. 写持久层代码

      持久层代码分两部分:1、方法定义 Interface

                                          2、方法实现:XXX.xml

其逻辑运行如下所示:

2.1 添加 mapper 接口

        创建一个mapper包,在这个包下创建USerInfoMapper接口,如图:

,其接口代码如下:

package com.example.zxslzw_mybaties.mapper;

import com.example.zxslzw_mybaties.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {
    List<UserInfo> queryAllUsers();
}

2.2 添加 USerInfoMapper.xml

        在result包下创建mybatis包,在mybatis包下创建 USerInfoMapper.xml 文件,如图:

        USerInfoMapper.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.example.zxslzw_mybaties.mapper.UserInfoMapper">
    <select id="queryAllUsers" resultType="com.example.zxslzw_mybaties.model.UserInfo">
        select * from userinfo
    </select>
</mapper>

        以下是对上面标签的说明:

        1、<mapper>标签:需要指定 namespace 属性,表示命名空间,值为 mapper 接口的全限定名,包括全包名.类名

        2、<select>查询标签:是用来执行数据库的查询操作的。

        3、id:是和 Interface (接口) 中定义的方法名称一样的,表示对接口的具体实现方法

        4、resultType:是返回的数据类型(使用全限定名),也就是开头我们定义的实体类。

         各标签对应关系:

2.3 测试类代码 

package com.example.zxslzw_mybaties.mapper;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class UserInfoMapperTest {

   @Autowired
    private UserInfoMapper userInfoMapper;

   @Test
    void query(){
       System.out.println(userInfoMapper.queryAllUsers());
   }
}

        对应的数据库中表的数据:

        运行结果如下:

3. 增删改查操作 

3.1 Insert

        UserInfoMapper接口代码:

 Integer insert(UserInfo userInfo);

        xml配置文件内容:

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

         测试类代码:

 @Test
    void insert() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("xuyangyuzhuo");
        userInfo.setPassword("888888");
        userInfo.setAge(20);
        userInfo.setGender(2);
        System.out.println(userInfoMapper.insert(userInfo));
    }

        控制台如下所示:

        数据库中的表信息如下:

3.1.1 使⽤@Param 

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

UserInfoMapper接⼝:

Integer insertUser(@Param("userinfo") UserInfo userInfo);

UserInfoMapper.xml实现:

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

3.1.2 返回⾃增 id

        接⼝定义不变, Mapper.xml 实现 设置useGeneratedKeys 和keyProperty属性:

<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
 insert into userinfo (username, `password`, age, gender, phone) values
 (#{userinfo.username},#{userinfo.password},#{userinfo.age},#
{userinfo.gender},#{userinfo.phone})
</insert>

3.2 Delete

        接口代码如下:

 Integer delete(Integer id);

         xml内容如下:

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

        测试类代码如下:

 @Test
    void delete() {
       System.out.println(userInfoMapper.delete(5));
    }

        运行测试类前表的内容:

        运行测试类后,结果如下:

3.3 update

        接口类代码如下:

 Integer update(UserInfo userInfo);

         xml内容如下:

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

        测试类代码:

 @Test
    void update() {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(7);
        userInfo.setUsername("wangyi");
        userInfoMapper.update(userInfo);
    }

 运行测试类后,结果如下:

3.4 select

        xml文件配置了开启驼峰命名;

        接口如下:

List<UserInfo> select2();

        xml文件如下:

<select id="select2" resultType="com.example.mybatisxmldemo2.model.UserInfo">
        select * from userinfo
    </select>

        测试类代码如下:

@Test
    void select2() {
        System.out.println(uSerInfoXmlMapper.select2());
    }

        运行结果如下:

ps:本文的内容就到这里了,如果 对你有所帮助的话就请一键三连哦!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值