MyBatis入门——MyBatis XML配置文件(3)

目录

一、配置连接字符串和MyBatis

二、写持久层代码

1、添加 mapper 接口

2、添加 USerInfoXmlMapper.xml

3、测试类代码

三、增删改查操作

1、增(Insert)

返回自增 id

2、删(Delete)

3、改(update)

4、查(select)

(1)起别名

(2)结果映射

(3)开启驼峰命名


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

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

        准备代码:实体类:

@Data
public class UserInfo {
    private Integer id;
    private String username;
    private String password;
    private Integer age;
    private Integer gender;
    private String phone;
    private Integer deleteFlag;
    private Date createTime;
    private Date updateTime;
}

一、配置连接字符串和MyBatis

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

#数据库配置
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=false
    username: root
    password: 1234
    driver-class-name: com.mysql.cj.jdbc.Driver
# 设置 Mybatis 的 xml 保存路径
mybatis:
  # 配置 mybatis xml 的⽂件路径,在 resources/mybatis 创建所有表的 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

1、添加 mapper 接口

        创建一个mapper包,再这个包下创建USerInfoXmlMapper接口,如图:

        USerInfoXmlMapper接口代码如下:

import com.example.mybatisxmldemo2.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;

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

2、添加 USerInfoXmlMapper.xml

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

        其中,创建 USerInfoXmlMapper.xml 的路径参考 yml中的配置,如图:

        USerInfoXmlMapper.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.mybatisxmldemo2.mapper.USerInfoXmlMapper">
    <select id="select" resultType="com.example.mybatisxmldemo2.model.UserInfo">
        select * from userinfo
    </select>
</mapper>

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

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

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

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

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

        各标签的对应关系,如图:

3、测试类代码

@SpringBootTest
class USerInfoXmlMapperTest {
    @Autowired
    private USerInfoXmlMapper uSerInfoXmlMapper;

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

        userinfo表如图:

        运行结果如下:

        能对应得上


三、增删改查操作

1、增(Insert)

        UserInfoXmlMapper接口代码:

@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.example.mybatisxmldemo2.mapper.USerInfoXmlMapper">
    <insert id="insert">
        insert into userinfo (username, password, age, gender)values (#{username}, #{password}, #{age}, #{gender})
    </insert>
</mapper>

        测试类代码:

@SpringBootTest
class USerInfoXmlMapperTest {
    @Autowired
    private USerInfoXmlMapper uSerInfoXmlMapper;
    @Test
    void insert() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("zhaoliu");
        userInfo.setPassword("zhaoliu");
        userInfo.setAge(20);
        userInfo.setGender(1);
        System.out.println(uSerInfoXmlMapper.insert(userInfo));
    }
}

        测试类运行结果如下:

        也可以使用@Param注释设置接口参数名称,使用方法和注解类似,UserInfoXmlMapper代码如下:

@Mapper
public interface USerInfoXmlMapper {
    Integer insert2(@Param("userInfo") 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.example.mybatisxmldemo2.mapper.USerInfoXmlMapper">
    <insert id="insert2">
        insert into userinfo (username, password, age, gender)values (#{userInfo.username}, #{userInfo.password}, #{userInfo.age}, #{userInfo.gender})
    </insert>
</mapper>

返回自增 id

        接口代码如下:

@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.example.mybatisxmldemo2.mapper.USerInfoXmlMapper">
    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        insert into userinfo (username, password, age, gender)values (#{username}, #{password}, #{age}, #{gender})
    </insert>
</mapper>

2、删(Delete)

        接口代码如下:

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

        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.mybatisxmldemo2.mapper.USerInfoXmlMapper">
    <delete id="delete">
        delete from userinfo where id = #{id}
    </delete>
</mapper>

        测试类代码如下:

@SpringBootTest
class USerInfoXmlMapperTest {
    @Autowired
    private USerInfoXmlMapper uSerInfoXmlMapper;
    @Test
    void delete() {
        System.out.println(uSerInfoXmlMapper.delete(13));
    }
}

        运行测试类前表的内容:

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

3、改(update)

        接口类代码如下:

@Mapper
public interface USerInfoXmlMapper {
    Integer update(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.example.mybatisxmldemo2.mapper.USerInfoXmlMapper">
    <update id="update">
        update userinfo set username = #{username} where id = {#id}
    </update>
</mapper>

        测试类代码如下:

@SpringBootTest
class USerInfoXmlMapperTest {
    @Autowired
    private USerInfoXmlMapper uSerInfoXmlMapper;
    @Test
    void update() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("xxxxx");
        userInfo.setId(4);
        uSerInfoXmlMapper.update(userInfo);
    }
}

        运行测试类前表的内容如下:

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

4、查(select)

        使用XML的方式进行查询,也会存在数据封装的问题(前面的没有是因为我在xml文件配置了开启驼峰命名),现在进行SQL查询

        接口代码如下:

@Mapper
public interface USerInfoXmlMapper {
    List<UserInfo> select2();
}

        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.mybatisxmldemo2.mapper.USerInfoXmlMapper">
    <select id="select2" resultType="com.example.mybatisxmldemo2.model.UserInfo">
        select * from userinfo
    </select>
</mapper>

        测试类代码如下:

@SpringBootTest
class USerInfoXmlMapperTest {
    @Autowired
    private USerInfoXmlMapper uSerInfoXmlMapper;
    @Test
    void select2() {
        System.out.println(uSerInfoXmlMapper.select2());
    }
}

        运行结果如下:

        这三个属性内容还是null,解决办法和注解类似(1、3和注解一样,2不一样),有三种:1、起别名  2、结果映射  3、开启驼峰命名

(1)起别名

        接口类:

@Mapper
public interface USerInfoXmlMapper {
    List<UserInfo> select3();
}

        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.mybatisxmldemo2.mapper.USerInfoXmlMapper">
    <select id="select3" resultType="com.example.mybatisxmldemo2.model.UserInfo">
        select id, username, password, age, gender, phone, delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from userinfo
    </select>
</mapper>

        测试类代码:

@SpringBootTest
class USerInfoXmlMapperTest {
    @Autowired
    private USerInfoXmlMapper uSerInfoXmlMapper;
    @Test
    void select3() {
        System.out.println(uSerInfoXmlMapper.select3());
    }
}

        运行结果如下:

        上面三个属性不为null了。

(2)结果映射

        接口类代码:

@Mapper
public interface USerInfoXmlMapper {
    List<UserInfo> select4();
}

        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.mybatisxmldemo2.mapper.USerInfoXmlMapper">
    <resultMap id="BaseMap" type="com.example.mybatisxmldemo2.model.UserInfo">
        <id column="id" property="id"></id> <!--主键-->
        <result column="username" property="username"></result>
        <result column="age" property="age"></result>
        <result column="gender" property="gender"></result>
        <result column="phone" property="phone"></result>
        <result column="delete_flag" property="deleteFlag"></result>
        <result column="create_time" property="createTime"></result>
        <result column="update_time" property="updateTime"></result>
    </resultMap>

    <select id="select4" resultMap="BaseMap">
        select * from userinfo
    </select>
</mapper>

        测试类代码如下:

@SpringBootTest
class USerInfoXmlMapperTest {
    @Test
    void select4() {
        System.out.println(uSerInfoXmlMapper.select4());
    }
}

        运行结果如下:

        其中那三个属性不为null了。

(3)开启驼峰命名

        yml文件内容如下:

mybatis:
  # 配置 mybatis xml 的⽂件路径,在 resources/mybatis 创建所有表的 xml ⽂件
  mapper-locations: classpath:mybatis/*Mapper.xml
  configuration: # 配置打印 MyBatis日志
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true #配置驼峰自动转换

        之后就会自动进行驼峰转换,不用额外起别名或者结果映射了。

        开发中使用注解还是XML的方式?关于开发中使用哪种模式,没有明确答案。仁者见仁,智者见智,没有统一的标准,更多是取决于你的团队或者项目经理、项目负责人。


五、常见问题

        yml配置路径错了:

        方法名称不一致:

        xml配置文件路径不正确:

        上面这些错误,都会造成以下报错内容:(绑定失败)

解决方案:

1确认yml配置的路径是否正确

2确认方法名称是佛一致

3确认xml配置的文件路径是否正确

  • 55
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 31
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

tao滔不绝

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

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

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

打赏作者

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

抵扣说明:

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

余额充值