springboot+mybatis项目

1:pom



    4.0.0

    org.example
    item-center
    1.0-SNAPSHOT
    jar

    item-center

    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.7.RELEASE
    

    
        UTF-8
        org.example.AppMain
        1.8
        1.14.8
        1.16
        2.3.3
    

    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            junit
            junit
            4.13.2
            test
        
        
        
            mysql
            mysql-connector-java
            8.0.26
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
            2.6.1
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.0
        

        
            com.alibaba
            druid
            1.1.23
        


        
        
            com.alibaba
            fastjson
            2.0.23
        

    



2:yml配置

spring:
  profiles:
    active: dev

spring:
  application:
    name: item-center
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/item-center
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource

mybatis:
    mapper-locations: classpath:mybatis-mapper/*.xml

3:sql

CREATE TABLE `item-center`.`t_item`  (
  `id` bigint(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'id',
  `name` varchar(50) NULL COMMENT '名称',
  `price` bigint(11) NULL COMMENT '价格',
  PRIMARY KEY (`id`)
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COMMENT = '商品';

4:entity

package org.example.entity;

import java.io.Serializable;

public class ItemDO implements Serializable {

    private long id;
    private String name;
    private long price;

   
}

5:mapper.xml




    
    
    
        insert into t_item(name,price)
        values(#{name},#{price})
    

    
        update t_item set
        name=#{name},
        price=#{price}
        where id=#{id}
    

6:mapper.class

package org.example.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.example.entity.ItemDO;

import java.util.List;

@Mapper
public interface ItemMapper {
    public int insert(ItemDO itemDO);
    public int update(ItemDO itemDO);
    public int get(long id);
    public List listAll();
}

7:test

package org.example;

import com.alibaba.fastjson.JSON;
import org.example.entity.ItemDO;
import org.example.mapper.ItemMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Random;
import java.util.UUID;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {AppMain.class})
public class ItemTest {
    @Autowired
    private ItemMapper itemMapper;
    @Test
    public void itemAllTest(){
        ItemDO itemDO = new ItemDO();
        itemDO.setName("名称"+UUID.randomUUID().toString());
        itemDO.setPrice(new Random().nextInt(1000));
        itemMapper.insert(itemDO);
        System.out.println(JSON.toJSON(itemDO));
        itemDO.setName("改过后的——>"+itemDO.getName());
        itemMapper.update(itemDO);
        List list = itemMapper.listAll();
        System.out.println(JSON.toJSONString(list));
    }
}

你可能感兴趣的:(spring,boot,mybatis,后端)