首页 > 其他分享 >Mybatis + SpringBoot 构建项目流程总结

Mybatis + SpringBoot 构建项目流程总结

时间:2024-06-05 19:24:02浏览次数:13  
标签:SpringBoot UserEntity 流程 Long public private user Mybatis id

软件版本

SpringBoot:3.0.2

 

引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>

 

添加配置

spring.datasource.url=jdbc:mysql://192.168.1.111:3306/demo
spring.datasource.username=root
spring.datasource.password=abc123

# 指定Mybatis的Mapper目录
mybatis.mapper-locations=classpath:mappers/*.xml

# 指定Mybatis的实体目录
mybatis.type-aliases-package=com.yfeil.test.entity

# 开启Mybatis驼峰命名
mybatis.configuration.map-underscore-to-camel-case=true

# 开启Mybatis日志打印SQL
# logging.level.com.yfeil.test=DEBUG

 

创建Entity

@Data
public class UserEntity {
    private Long id;
    private String name;
    private String password;
    private String address;
    private String phone;
    private List<ItemEntity> item;
}

 

@Data
public class ItemEntity {
    private Long itemId;
    private Long userId;
    private String itemName;
}

 

创建Mapper

@Mapper
public interface UserMapper {

    List<UserEntity> list();

    UserEntity select(Long id);

    Long insert(UserEntity user);

    Integer update(@Param("user") UserEntity user, @Param("id") Long id);

    Integer delete(Long id);
}

 

@Mapper
public interface ItemMapper {
}

 

创建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.yfeil.test.mapper.UserMapper">

    <resultMap type="UserEntity" id="userMap">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="password" column="password"/>
        <result property="address" column="address"/>
        <result property="phone" column="phone"/>
        <collection property="item" resultMap="com.yfeil.test.mapper.ItemMapper.itemMap"/>
    </resultMap>

    <select id="list" resultMap="userMap">
        SELECT *
        FROM user
            LEFT JOIN item ON item.user_id = user.id
    </select>

    <select id="select" resultMap="userMap">
        SELECT *
        FROM user
             LEFT JOIN item ON item.user_id = user.id
        WHERE user.id = #{id}
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO user (name,password,address,phone) VALUE
        (#{name},#{password},#{address},#{phone})
    </insert>

    <update id="update">
        UPDATE user
        <set>
            <if test="user.name != null">
                name = #{user.name},
            </if>
            <if test="user.password != null">
                password = #{user.password},
            </if>
            <if test="user.address != null">
                address = #{user.address},
            </if>
            <if test="user.phone != null">
                phone = #{user.phone},
            </if>
        </set>
        WHERE id = #{id}
    </update>


    <delete id="delete">
        DELETE
        FROM user
        WHERE id = #{id}
    </delete>

</mapper>

 

<?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.yfeil.test.mapper.ItemMapper">

    <resultMap type="ItemEntity" id="itemMap">
        <id property="itemId" column="item_id"/>
        <result property="userId" column="user_id"/>
        <result property="itemName" column="item_name"/>
    </resultMap>

</mapper>

 

创建测试控制器

@RequiredArgsConstructor
@RequestMapping("/test")
@RestController
public class TestController {

    private final UserMapper userMapper;

    @GetMapping("/")
    public List<UserEntity> get(){
        return userMapper.list();
    }

    @GetMapping("/{id}/")
    public UserEntity getById(@PathVariable Long id){
        return userMapper.select(id);
    }

    @PostMapping("/")
    public Long post(@RequestBody UserEntity user){
        return userMapper.insert(user);
    }

    @PutMapping("/{id}/")
    public Integer put(@RequestBody UserEntity user, @PathVariable Long id){
        return userMapper.update(user, id);
    }

    @DeleteMapping("/{id}/")
    public Integer delete(@PathVariable Long id){
        return userMapper.delete(id);
    }
}

 

标签:SpringBoot,UserEntity,流程,Long,public,private,user,Mybatis,id
From: https://www.cnblogs.com/yfeil/p/18233378

相关文章

  • SpringBoot+Nginx大文件传输
    Nginx配置 #公众端的附件上传location/api/visitor/upload{#Passalteredrequestbodytothislocationupload_pass/api/outerPortal/uploadAndSave;#Storefilestothisdirectory#Thedirectory......
  • Java1.8语言+ springboot +mysql + Thymeleaf 全套家政上门服务平台app小程序源码
    Java1.8语言+springboot+mysql +Thymeleaf 全套家政上门服务平台app小程序源码家政系统是一套可以提供上门家政、上门维修、上门洗车、上门搬家等服务为一体的家政平台解决方案。它能够与微信对接、拥有用户端小程序,并提供师傅端app,可以帮助创业者在不需要相关技术人员及......
  • 如何从零开始创建一个完整的SpringBoot项目,逐步构建自己的技术栈。
     有的小伙伴虽然开发很多年,但是有很多连自己都没有动手去创建一个SpringBoot。原因各不相同,有的是开发项目比较忙,没有时间,刚去公司的时候项目的框架就是现成的,就一直用。还有就是自己没有闲下学习的习惯。我其实就是,最起码工作1-2的时候还没有自己创建过,当时自我学习的时间......
  • Android Audio模块 音量增益 framework到HAL 层加载流程
    目录背景概念AndroidAudio调用流程(app-framework-HAL)标记0libaudioclient标记1libaudioclient:aps变量赋值标记1.1binder变量赋值标记2audiopolicy标记4audiopolicy:mAudioPolicyManager变量赋值标记5audiopolicy标记6audiopolicy:mpClientInterface变量赋值标......
  • Java (MyBatis)
    前沿MyBatis是一个开源的持久层框架,它简化了数据库访问的过程,通过将SQL语句和Java方法进行映射1.SQL和Java方法的映射:MyBatis使用XML或注解来配置SQL语句,并将SQL语句映射到Java接口或方法上,使得开发者可以直接调用Java方法来执行数据库操作。2.SQL语句的......
  • mybatis - [07] 模糊查询
    题记部分   (1)mapper类List<User>getUserLike(Stringvalue);(2)mapper.xml<!--写法1--><selectid="getUserLike"resultType="com.harley.pojo.User">select*fromuserwherenamelike#{value}</select><......
  • 基于SpringBoot的秒杀系统源码数据库
    基于SpringBoot的秒杀系统源码数据库社会发展日新月异,用计算机应用实现数据管理功能已经算是很完善的了,但是随着移动互联网的到来,处理信息不再受制于地理位置的限制,处理信息及时高效,备受人们的喜爱。本次开发一套基于SpringBoot的秒杀系统,管理员功能有个人中心,用户管理,商品类......
  • 基于springboot的二手车交易系统源码数据库
    基于springboot的二手车交易系统源码数据库如今社会上各行各业,都喜欢用自己行业的专属软件工作,互联网发展到这个时候,人们已经发现离不开了互联网。新技术的产生,往往能解决一些老技术的弊端问题。因为传统二手车交易信息管理难度大,容错率低,管理人员处理数据费工费时,所以专门为......
  • 基于springboot的纺织品企业财务管理系统源码数据库
    基于springboot的纺织品企业财务管理系统源码数据库在如今社会上,关于信息上面的处理,没有任何一个企业或者个人会忽视,如何让信息急速传递,并且归档储存查询,采用之前的纸张记录模式已经不符合当前使用要求了。所以,对纺织品企业财务信息管理的提升,也为了对纺织品企业财务信息进行......
  • 一个基于 React + SpringBoot 的在线多功能问卷系统(附源码)
    简介:一个基于React+SpringBoot的在线多功能问卷系统前端技术栈:React、React-Router、Webpack、Antd、Zustand、Echarts、DnDKit后端技术栈:SpringBoot、MySQL、MyBatisPlus、Redis项目源码下载链接: https://pan.quark.cn/s/2e32786e0c61部分页面静态预览: 主要前......