1、创建一个空的Spring Boot工程
2、引入Spring Boot Starter 父工程(pom.xml)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.13</version>
</parent>
3、引入spring-boot-starter
、spring-boot-starter-test
、mybatis-plus-boot-starter等依赖(pom.xml)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.18</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
4. 配置application.yml文件(在resources中的config文件夹中)
server:
port: 8080
spring:
application:
name: book-mall
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
druid:
url: jdbc:mysql://localhost:3306/book_db?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
username: root
password: 1234
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
5. 创建启动类
package com.dzy.bm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BookMallApp {
public static void main(String[] args) {
SpringApplication.run(BookMallApp.class);
}
}
6. 编写实体类
package com.dzy.bm.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("btype_tab")
public class BookType {
@TableId(value = "btype_id",type = IdType.AUTO)
private Integer id;
@TableField("btype_name")
private String name;
@TableField("btype_createtime")
private Date createTime;
@TableField("btype_updatetime")
private Date updateTime;
@TableField("btype_creator")
private String creator;
}
TableField与TableId中的value写入数据库列字段名(TableField默认第一个变量是value),TableId的type设置主键是自增长(与数据库中的btype_id相匹配)
7.编写mapper
package com.dzy.bm.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dzy.bm.entity.BookType;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface IBookTypeDao extends BaseMapper<BookType> {
}
8. 测试
package com.dzy.bm.dao;
import com.dzy.bm.entity.BookType;
import org.junit.Assert;
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.SpringJUnit4ClassRunner;
import java.util.Date;
import java.util.List;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class IBookTypeDaoTest {
@Autowired
private IBookTypeDao dao;
@Test
public void save() {
dao.insert(new BookType(null, "言情", new Date(), new Date(), "tom"));
}
@Test
public void findAll() {
List<BookType> bookTypes = dao.selectList(null);
bookTypes.forEach(System.out::println);
}
}