Java spring cloud b2b2c social e-commerce spring cloud distributed micro service spring boot integration mybatis

Introducing dependency

E-commerce social platform source code Please add Penguin: 103874626. The dependency of introducing mybatis spring boot starter in pom file:

<dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter<artifactId>
     <version>1.3.0</version>
 </dependency>

Import database connection dependency:

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.29</version>
        </dependency>

Import data source

Introduce data source in application.properties configuration file:

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

In this way, springboot can access the data.

Create database table

Table creation statement:

-- create table `account`
# DROP TABLE `account` IF EXISTS
CREATE TABLE `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `money` double DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `account` VALUES ('1', 'aaa', '1000');
INSERT INTO `account` VALUES ('2', 'bbb', '1000');
INSERT INTO `account` VALUES ('3', 'ccc', '1000');

Concrete realization

This article is realized by annotation.

Create entity:

public class Account { 
private int id ; 
private String name ; 
private double money;

setter... 
getter... 
}

dao level

@Mapper
public interface AccountMapper {
 
    @Insert("insert into account(name, money) values(#{name}, #{money})")
    int add(@Param("name") String name, @Param("money") double money);
 
    @Update("update account set name = #{name}, money = #{money} where id = #{id}")
    int update(@Param("name") String name, @Param("money") double money, @Param("id") int  id);
 
    @Delete("delete from account where id = #{id}")
    int delete(int id);
 
    @Select("select id, name as name, money as money from account where id = #{id}")
    Account findAccount(@Param("id") int id);
 
    @Select("select id, name as name, money as money from account")
    List<Account> findAccountList();
}

service level

@Service
public class AccountService {
    @Autowired
    private AccountMapper accountMapper;
 
    public int add(String name, double money) {
        return accountMapper.add(name, money);
    }
    public int update(String name, double money, int id) {
        return accountMapper.update(name, money, id);
    }
    public int delete(int id) {
        return accountMapper.delete(id);
    }
    public Account findAccount(int id) {
        return accountMapper.findAccount(id);
    }
    public List<Account> findAccountList() {
        return accountMapper.findAccountList();
    }
}

The source code of B2B2C e-commerce platform built by JAVASpring Cloud distributed microservice cloud for large enterprises: 103874626

Tags: Spring MySQL Mybatis Database

Posted on Sat, 09 Nov 2019 14:25:18 -0500 by stig1