目录

一、使用IDEA创建工程

1.1新建

 ​编辑

1.2选择Maven 

 1.3创建包

二、添加相关依赖

2.1添加依赖

2.2更新Maven

 三、配置application.yaml文件

四、相关注解的使用和理解

4.1@Component

4.2@Autowired

4.3@springBootApplication

4.4@Mapper

4.5@MapperScan

4.6@PostConstruct

4.7@Select

4.8@Insert

4.9@Delete

4.10@Update

五、编写java代码

5.1DO类

5.2Mapper接口

5.3启动类

5.4测试类

六、执行流程分析


一、使用IDEA创建工程

1.1新建

 西南交通大学智能监测 培训课程练习2-LMLPHP

1.2选择Maven 

 西南交通大学智能监测 培训课程练习2-LMLPHP

 1.3创建包

西南交通大学智能监测 培训课程练习2-LMLPHP

二、添加相关依赖

2.1添加依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.7.RELEASE</version>
</parent>
	
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Mysql Connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <!-- Druid 数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.4</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3.4</version>
        </dependency>
</dependencies>

 

2.2更新Maven

 三、配置application.yaml文件

server:
  # 标记本项目运行在此电脑的 8000 端口
  port: 8000

spring:
  datasource:
    # 数据库地址, mysql 端口默认 3306
    url: jdbc:mysql://127.0.0.1:3306/summerytempdb?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&allowMultiQueries=true&useSSL=false&serverTimezone=UTC
    # 驱动类,复制
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: ykxykx

四、相关注解的使用和理解

4.1@Component

4.2@Autowired

4.3@springBootApplication

4.4@Mapper

4.5@MapperScan

@SpringBootApplication
@MapperScan("ykx.summertrain.system")
public class YKXSpringBootApplication {

}

4.6@PostConstruct

4.7@Select

4.8@Insert

4.9@Delete

4.10@Update

五、编写java代码

5.1DO类

@Component
public class ProductDO {

    private String product_id;
    private String name;
    private String type;


    public String getProduct_id() {
        return product_id;
    }

    public void setProduct_id(String product_id) {
        this.product_id = product_id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "ProductDO{" +
                "product_id='" + product_id + '\'' +
                ", name='" + name + '\'' +
                ", type='" + type + '\'' +
                '}';
    }
}

5.2Mapper接口

@Mapper
public interface ProductMapper {

        @Select("select *from t_ykx_product;")
        ProductDO[] selectProduct();

        @Insert("insert into t_ykx_product value(#{e.product_id},#{e.name},#{e.type});")
        int insertNewProduct(@Param("e") ProductDO param);

        @Delete("delete from t_ykx_product where product_id = #{id};")
        int deleteProduct(@Param("id") String id);

        @Update("update t_ykx_product set type = #{type} where type = 'BOOK';")
        int updateProduct(@Param("type") String type);
}

 

5.3启动类

@SpringBootApplication
@MapperScan("ykx.summertrain.system")
public class YKXSpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(YKXSpringBootApplication.class,args);
    }

}

 

5.4测试类

@Component
public class TestClass {

    @Autowired
    private ProductMapper productMapper ;

    @PostConstruct
    private void init(){

        ProductDO[] productDOS = productMapper.selectProduct();
        System.out.println("=======查询数据如下=======");
        for(int i = 0; i < productDOS.length; i++){
            System.out.println(productDOS[i]);
        }

        ProductDO productDO = new ProductDO();
        productDO.setProduct_id("99");
        productDO.setName("iPhone14 pro max");
        productDO.setType("phone");
        System.out.println("插入的数据:" + productDO);
        int i1 = productMapper.insertNewProduct(productDO);
        System.out.println("插入的行数为:" + i1);
        int i2 = productMapper.deleteProduct("10");
        System.out.println("删除的行数为:" + i2);
        int i3 = productMapper.updateProduct("book");
        System.out.println("修改的行数为:" + i3);

    }

}

西南交通大学智能监测 培训课程练习2-LMLPHP

 

六、执行流程分析

05-28 12:22