Mybatis + SpringBoot 构建项目流程总结

软件版本

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);
    }
}

 

posted @ 2024-06-05 19:09  Yfeil  阅读(20)  评论(0编辑  收藏  举报