1. 新建spring boot项目
2. maven导入依赖
<!--连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!--Mybatis-Plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
4.创建表
CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '员工编号',
`name` varchar(10) DEFAULT NULL COMMENT '员工姓名',
`gender` char(4) DEFAULT NULL COMMENT '性别',
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=1945 DEFAULT CHARSET=utf8;
5. 配置数据库连接
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.username=账号
spring.datasource.password=密码
spring.datasource.url=jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
logging.level.root = info
6.配置 MapperScan 注解
@MapperScan(basePackages = {"com.mp.demo.mapper"})
7.编写entry类
@Data
public class Employee{
//主键
private int id;
//姓名
private String name;
//性别
private String gender;
}
8. mapper接口继承BaseMapper
public interface EmployeeMapper extends BaseMapper<Employee> {
}
9. 编写方法
@SpringBootApplication
@MapperScan(basePackages = {"com.mp.demo.mapper"})
class DemoApplication {
@Autowired
private StudentMapper EmployeeMapper;
public void insert(){
Employee employee = new Employee();
employee.setId(57);
employee.setGender("男");
employee.setName("好男人");
employeeMapper.insert(employee);
}
}
10. 输出结果
image.png
网友评论