Day01酷鲨商城第一天

1\csmall-product springboot  2.5.x版本

2\导入sql文件 mall_pms,链接

3\添加依赖

<!-- Mybatis整合Spring Boot的依赖项 -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.2</version>
</dependency>

<!-- MySQL的依赖项 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

4\ properties文件导入配置

spring:
    datasource:
        url=jdbc:mysql://localhost:3306/mall-pms?useUnicode-true&characterEncoding=utf-8&serverTimeZone=Asia/Shanghai
        username=root:
        password=root:

5\  在test包下,测试代码能否运行

@SpringbootTest
class CsmallProductApplicationTests{
    @Test
    void contextLoad(){
    }
    
    @Autowrite
    DataSource datasource;
    
    @Test
    void getConnection() throws Throwable{
        datasource.getConnection();
    }
}

6\
MyBatis 的基础配置
Mapper 接口上添加@Mapper注解
【推荐】 配置类上添加@MapperScan注解指定Mapper接口的根包

// 这个在项目的configuration包下做配置类

@Configuration
@MapperScan("cn.tedu.csmall.product.mapper")
public class MybatisConfiguration{
}


8\ 在application.properties中添加配置,指定xml文件的位置
mybatis.mapper-location=classpath:mapper/*.xml

mybatis.mapper-location= classpath:mapper/*.xml


9\关于Lombok框架

<!-- Lombok的依赖项,主要用于简化POJO类的编写 -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.20</version>
    <scope>provided</scope>
</dependency>


10\ 在pojo类上添加@Data注解  Setter / Getter / hashCode() /equals / toString() 方法
@Setter
@Getter
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstrutor
@Slf4j


11\
插入相册数据案例


准备entity.Album类

@Data
public class Album implement Serializable{

    private Long id;
    private String name;
    private String description;
    private Integer sort;
    private LocalDateTime gmtCreate;
    private LocalDataTime gmtModified;
}


在mapper包下创建AlbumMapper接口,并声明“插入相册数据”的抽象方法

public interface AlbumMapper{
    int insert(Album album);
}

resources/mapper下创建Albummapper.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="cn.tedu.csmalll.product.mapper.AlbumMapper">
    <insert id="insert">
        INSERT INTO pms_album(
        name,description,sort
        ) VALUES(
        #{name},#{description},#{sort}
        )
    </insert>
</mapper>


测试src/test/java的根包下创建mapper.AlbumMapperTests测试类。在类上添加@SpringbootTest注解,在类中自动装配AlbumMapper接口属性

@SpringbootTest
public class AlbumMapperTest{
    @Autowrite
    AlbumMapper mapper;
    
    @Test
    void insert(){
        Album album = new Album();
        album.setName("测试数据001");
        album.setDescription("测试数据的简介001");
        album.setSort(255);
        
        mapper.insert(album);
        System.out.println("插入数据完成!");
    }
}

常见异常

一、
@MapperScan指定的Mapper接口的根包,与实际Mapper接口所在的根包不相符,就会报
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'cn.tedu.csmall.product.mapper.AlbumMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)

二、
如果存在以下错误,执行测试时会出现错误提示:

在XML文件中的<mapper>标签的namespace属性值配置有误
在XML文件中的<insert>等标签的id属性值配置有误
在application.properties中配置的XML文件的位置,与实际使用的XML文件的位置不相符
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): cn.tedu.csmall.product.mapper.AlbumMapper.insert

关于持久层的xml中的 useGeratedKeys 和 keyProperty

如果插入数据的表中的ID是自动编号的,应该在<insert>标签上配置useGeneratedKeys="true"keyProperty="属性名"这2个属性,以获取自动编号的ID值!

<insert id="insert" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO pms_album(
        name,description,sort
    )VALUES{
        #{name},#{description},#{sort}
    }
</insert>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值