Mybatis + SpringBoot 构建项目流程总结

8 篇文章 0 订阅
7 篇文章 0 订阅

软件版本

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Yfeil

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

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

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

打赏作者

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

抵扣说明:

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

余额充值