内容:ORM介绍 Mybatis-Plus介绍 Mybatis-Plus CRUD操作
- ORM介绍
- Mybatis-Plus介绍
- 任务:查询所有用户
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.3.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.20version>
dependency>
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false
spring.datasource.username=root
spring.datasource.password=root
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`birthday` time(6) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
INSERT INTO `user` VALUES (1, 'ljh', '123', '16:30:54.000000');
INSERT INTO `user` VALUES (2, 'yxy', '222', '16:31:09.000000');
SET FOREIGN_KEY_CHECKS = 1;
package com.example.mdpdemo.entity;
public class User {
private int id;
private String username;
private String password;
private String birthday;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", birthday='" + birthday + '\'' +
'}';
}
public void setId(int id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public int getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getBirthday() {
return birthday;
}
}
@SpringBootApplication
@MapperScan("com.example.mdpdemo.mapper")
public class MdpdemoApplication {
public static void main(String[] args) {
SpringApplication.run(MdpdemoApplication.class, args);
}
}
- 加入swagger:加入方法见本专栏
- 创建mapper接口
@Mapper
public interface UserMapper {
@Select("select * from user")
public List<User> find();
}
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@ApiOperation("查询用户")
@GetMapping("/user")
public String query(){
List<User> users = userMapper.find();
System.out.println(users);
return "查询用户";
}
@GetMapping("/in")
public String in(){
return "in";
}
}
-
用swagger测试接口
-
查询结果
- 以json格式给前端,将返回值转为List就自动装配为json了
@ApiOperation("查询用户")
@GetMapping("/user")
public List query(){
List<User> users = userMapper.find();
System.out.println(users);
return users;
}
- 测试结果:
- 任务:增加一个用户,并展示给前端
- 先在mapper中编写sql语句,注意:增加用户返回值为影响的行数
@Insert("insert into user values (#{id},#{username},#{password},#{birthday})")
public int addUser(User user);
@Select("select * from user where username = #{username}")
public User selectOne(String username);
@PostMapping("/user1")
public User save(User user){
int i = userMapper.addUser(user);
if (i!=0) {
System.out.println("插入成功");
return userMapper.selectOne("lhp");
}
return null;
}
运行结果:
用mybatisplus进行改造
- 官网
- 直接在mapper中继承一个basemapper,并且给他一个泛型,他就可以根据这个泛型找到数据库中相应的表,注意名字一致
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
- 控制层:插入一个用户并通过map的方式查询并展示给前端
@PostMapping("/user1")
public List<User> save(User user){
int i = userMapper.insert(user);
if (i!=0) {
System.out.println("插入成功");
HashMap<String, Object> map = new HashMap<>();
map.put("username","lhp");
return userMapper.selectByMap(map);
}
return null;
}