SpringBoot使用Mybatis

发布时间:2023年12月17日

1.注解开发

1.1mapp.java

public interface UserDao {
    // 测试
    @Select("select * from user")
    List<User> findAll();

1.2实体类

package com.study.entity;


public class User {
    private Integer id;
    private Integer villageId;
    private String realName;
    private String nickName;
    private String pwd;
    private String sex;
    private String phone;
    private String idCard;
    private String nation;
    private String address;
    ;
    private String tx;
    private String registeTime;
    private Community community;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getVillageId() {
        return villageId;
    }

    public void setVillageId(Integer villageId) {
        this.villageId = villageId;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public String getNation() {
        return nation;
    }

    public void setNation(String nation) {
        this.nation = nation;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTx() {
        return tx;
    }

    public void setTx(String tx) {
        this.tx = tx;
    }

    public String getRegisteTime() {
        return registeTime;
    }

    public void setRegisteTime(String registeTime) {
        this.registeTime = registeTime;
    }

    public Community getCommunity() {
        return community;
    }

    public void setCommunity(Community community) {
        this.community = community;
    }
}

1.3数据表

1.4测试类

@SpringBootTest
class StudyApplicationTests {
    @Autowired
    private UserDao userDao;
    @Test
    void findAll(){
        System.out.println(userDao.findAll());

    }

}

1.5常见问题

问题1-默认对象字符串输出

说明:Java 中默认的对象字符串输出形式;解决方法实体类写toSring方法

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", villageId=" + villageId +
                ", realName='" + realName + '\'' +
                ", nickName='" + nickName + '\'' +
                ", pwd='" + pwd + '\'' +
                ", sex='" + sex + '\'' +
                ", phone='" + phone + '\'' +
                ", idCard='" + idCard + '\'' +
                ", nation='" + nation + '\'' +
                ", address='" + address + '\'' +
                ", tx='" + tx + '\'' +
                ", registeTime='" + registeTime + '\'' +
                ", community=" + community +
                '}';
    }

??问题2-mybatis驼峰写法

说明:发现有些没有拿到值;因为没有开启Mybatis的驼峰写法。

#扫描mapper配置
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.study.entity
#  开启驼峰写法
  configuration:
    map-underscore-to-camel-case: true
server:
  port : 8086

打印成功?

2.配置文件开发

2.1UserMapper.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.study.dao.UserDao">
//查询
<select id="findById" parameterType="java.lang.Integer" resultType="com.study.entity.User">
        select * from user where id=#{id}
    </select>
</mapper>

2.2 UserDao

    User findById( @Param("id")Integer id);

2.3测试类

@SpringBootTest
class StudyApplicationTests {
    @Autowired
    private UserDao userDao;
    @Test
    void findById() {
        System.out.println(userDao.findById(6));

    }


}

2.4成功

?

文章来源:https://blog.csdn.net/m0_62785037/article/details/135049973
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。