MyBatis-Plus的简单使用案例
gradle
dependencies {
implementation ('org.springframework.boot:spring-boot-starter-web')
runtimeOnly 'mysql:mysql-connector-java'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
compile 'com.baomidou:mybatis-plus-boot-starter:3.4.1'
}
配置
server.port=8081
spring.datasource.password=xxxxxx
spring.datasource.url=jdbc:mysql://ip:3306/mybatis_demo?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
logging.level.com.study.mybatisplusdemo.user.mapper=debug
配置启动项
@SpringBootApplication
@MapperScan("com.study.mybatisplusdemo")
public class MybatisPlusDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusDemoApplication.class, args);
}
@Bean//分页插件
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
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 com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import java.util.Arrays;
@TableName("m_user")
public class User {
@TableId(value = "id",type= IdType.AUTO)
private Integer id;
@TableField("name")
private String name;
@TableField("age")
private Integer age;
//类型处理器,用于类型转换使用
@TableField(value = "json",typeHandler = JacksonTypeHandler.class)
private String[] json;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String[] getJson() {
return json;
}
public void setJson(String[] json) {
this.json = json;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", json=" + Arrays.toString(json) +
'}';
}
}
public interface UserMapper extends BaseMapper<User> {
@Select("select * from m_user where id=#{id}")
public User findUser(int id);
}
@Service
public class UserService {
@Autowired(required = false)
private UserMapper userMapper;
//使用Wrapper 进行查询
public User findUser(int id){
// userMapper.findUser(id);
//userMapper.selectById(id);
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("id",id);
User user = userMapper.selectOne(wrapper);
// System.out.println(wrapper.getSqlSegment());
return user;
}
public IPage<User> pageUser(int pageNum,int size){
Page<User> page = new Page<>();
page.setSize(size);
page.setCurrent(pageNum);
IPage<User> userIPage = this.userMapper.selectPage(page, null);
System.out.println(userIPage);
return userIPage;
}
//使用Wrapper 进行更新
public User update(User user) {
UpdateWrapper<User> wrapper = new UpdateWrapper<>();
wrapper.set(!StringUtils.isEmpty(user.getName()),"name",user.getName());
wrapper.set("age",user.getAge());
wrapper.eq("id",user.getId());
String sqlSet = wrapper.getSqlSet();
// System.out.println(sqlSet);
userMapper.update(null,wrapper);
return null;
}
}
@RequestMapping("/user")
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/user")
public User getUser(int id){
return userService.findUser(id);
}
@RequestMapping("/pageUser")
public IPage<User> pageUser(int page,int size){
return userService.pageUser(page,size);
}
@RequestMapping("update")
public User updateUser(@RequestBody User user){
return userService.update(user);
}
}
版权声明:本文为weixin_36927395原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。