1.ORM介绍
2.MyBatis-Plus介绍
添加依赖:(pom.xml)
<!--MyBatisPlus依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> <!--mysql驱动依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--数据连接池druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.20</version> </dependency>
配置数据库相关信息(application.properties)
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/ruoyi?userSSL=false spring.datasource.username=root spring.datasource.password=123456 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
在mysql中创建表
添加@MapperScan注解
修改mpDemo.java为:
package com.example.mpdemo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.mpdemo.mapper") public class MpdemoApplication { public static void main(String[] args) { SpringApplication.run(MpdemoApplication.class, args); } }
3.Mybatis
在com.example.mpdemo里新建mapper存放数据库有关文件
UserController:
package com.example.mpdemo.controller; import com.example.mpdemo.entity.User; import com.example.mpdemo.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class UsrController { @Autowired private UserMapper userMapper; @GetMapping("/user") public String query(){ List<User> list=userMapper.find(); System.out.println(list); return "查询用户"; } }
UserMapper:
package com.example.mpdemo.mapper; import com.example.mpdemo.entity.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface UserMapper { //查询所有用户 @Select("select * from user") public List<User> find(); }
User:
package com.example.mpdemo.entity; public class User { private int id; private String username; private String password; private String birthday; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", birthday='" + birthday + '\'' + '}'; } }
测试
下图可以看到,成功连接了数据库并返回了User的信息
若要转化为json格式供前端使用,则修改String 为List并返回list
public List query(){ List<User> list=userMapper.find(); System.out.println(list); return list; }
一些sql语句:
插入操作:
UserMapper.java:
package com.example.mpdemo.mapper; import com.example.mpdemo.entity.User; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface UserMapper { //查询所有用户 @Select("select * from user") public List<User> find(); @Insert("insert into user values (#{id},#{username},#{password},#{birthday})") public int insert(User user);//如果插入失败,返回0 }
UserController.java:
package com.example.mpdemo.controller; import com.alibaba.druid.pool.ha.selector.StickyDataSourceHolder; import com.example.mpdemo.entity.User; import com.example.mpdemo.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class UsrController { @Autowired private UserMapper userMapper; @GetMapping("/user") public List query(){ List<User> list=userMapper.find(); System.out.println(list); return list; } @PostMapping("/user") public String save(User user){ int i= userMapper.insert(user); if(i>0){ return "插入成功"; }else{ return "插入失败"; } } }
测试:
没写id,默认为0
刷新数据库,可以看到已经成功插入了
删除:
@PostMapping("/user") public String d(int id){ int i= userMapper.delete(id); if(i>0){ return "删除成功"; }else{ return "删除失败"; } }
4.MyBatis-Plus
Mybatis Plus:简化了Mybatis
UserMapper里:继承BaseMapper
public interface UserMapper extends BaseMapper<User> { }
BaseMapper:
插入
UserController:
@RestController public class UsrController { @Autowired private UserMapper userMapper; @GetMapping("/user") public List query(){ List<User> list=userMapper.selectList(null);//查询一个列表(按条件查询) System.out.println(list); return list; } @PostMapping("/user") public String save(User user){ int i= userMapper.insert(user); if(i>0){ return "插入成功"; }else{ return "插入失败"; } } }
其余可以在mybatis官网上找到其他操作代码