1.工程结构
2.创建数据库mybatisplus、表user,导入表数据
3.引入相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.31version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>3.0.2version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
dependencies>
4.创建domain包,编写实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
5.创建mapper包,编写mapper接口
@Mapper
public interface UserMapper {
public List<User> selectAll();
}
6.编写映射文件
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hhb.mp01.mapper.UserMapper">
<select id="selectAll" resultType="com.hhb.mp01.domain.User">
select *
from user
select>
mapper>
7.创建service包,编写Service
public interface UserService {
List<User> selectAll();
}
8.编写ServiceImpl
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> selectAll() {
return userMapper.selectAll();
}
}
9.编写controller
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/selectAll")
private List<User> selectAll() {
List<User> users = userService.selectAll();
return users;
}
}
10.编写配置文件
#配置数据源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=030522
11.测试结果
1.引入相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.3.1version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.8version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.31version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
2.创建实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
3.集成MybatisPlus
编写Mapper接口
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
编写Service接口
public interface UserService {
List<User> selectList();
}
编写ServiceImpl
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> selectList() {
return userMapper.selectList(null);
}
}
编写Controller
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/selectList")
public List<User> selectList() {
List<User> users = userService.selectList();
return users;
}
}
编写配置文件
#配置数据源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=030522
4.测试效果
5.单元测试
@SpringBootTest
class Mp02ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void selectList() {
List<User> users = userMapper.selectList(null);
System.out.println(users);
}
}
6.日志
#去除mybatisplus图形
mybatis-plus.global-config.banner=false
#去除spring图形
spring.main.banner-mode=off
#配置日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
简单插入数据
@Test
void insert() {
User user = new User();
user.setAge(22);
user.setId(6L);
user.setEmail("[email protected]");
user.setName("jerry");
userMapper.insert(user);
}
简单删除数据
@Test
void deleteOne() {
userMapper.deleteById(6L);
}
简单修改数据
@Test
void updateById() {
User user = new User();
user.setId(6L);
user.setAge(35);
user.setName("杰瑞");
user.setEmail("[email protected]");
userMapper.updateById(user);
}
简单查询数据
@Test
void selectById() {
User user = userMapper.selectById(6L);
System.out.println(user);
}
@Test
void selectList() {
List<User> users = userMapper.selectList(null);
System.out.println(users);
}
IService接口
ServiceImpl实现类
public interface UserService extends IService<User>
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService
简单插入数据
@Test
void insertService() {
User user = new User();
user.setId(7L);
user.setName("张三");
user.setAge(25);
user.setEmail("[email protected]");
userService.save(user);
}
简单删除数据
@Test
void deleteService() {
userService.removeById(7L);
}
简单修改数据
@Test
void updateService() {
User user = new User();
user.setId(6L);
user.setAge(36);
userService.updateById(user);
}
简单查询数据
@Test
void selectService() {
List<User> users = userService.selectList();
System.out.println(users);
}
1.自定义Mapper接口方法
@Mapper
public interface UserMapper extends BaseMapper<User> {
User selectByName(String name);
}
2.提供映射配置文件
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hhb.mp02.mapper.UserMapper">
<select id="selectByName" resultType="com.hhb.mp02.domain.User" parameterType="String">
select * from user where name=#{name}
select>
mapper>
3.测试自己的Mapper接口方法
//自定义方法
@Test
void selectByName() {
User user = userMapper.selectByName("Tom");
System.out.println(user);
}