Mybatis:
MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及结果集的检索。
1.简化JDBC的开发
2.能够更好的完成ORM(对象关系映射)
Mybatis-Plus:
mybatis-plus 是在 mybatis 上的增强,减少了 xml 的配置,几乎不用编写xml就可以做到单表的 CURD,很是方便,极大提供了开发的效率。
4.0.0
org.example
ssr
1.0-SNAPSHOT
1.8
UTF-8
UTF-8
2.4.1
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
com.baomidou
mybatis-plus-boot-starter
3.4.3
com.baomidou
mybatis-plus-generator
3.5.1
mysql
mysql-connector-java
org.projectlombok
lombok
junit
junit
org.springframework.boot
spring-boot-test
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
1.8
UTF-8
org.springframework.boot
spring-boot-maven-plugin
2.4.1
com.jt.SsrApplication
repackage
repackage
package com.jt;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.jt.mapper")
public class SsrApplication {
public static void main(String[] args) {
SpringApplication.run(SsrApplication.class,args);
}
}
package com.jt.pojo;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.sql.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@TableName("User")
public class User implements Serializable {
private Integer runoobId;
private String runoobTitle;
private String runoobAuthor;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date submissionDate;
}
package com.jt.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class SysResult implements Serializable {
private Integer status; //200成功 201失败
private String msg; //提示信息
private Object data; //服务器返回值数据
public static SysResult fail(){
return new SysResult(201,"业务调用失败!!",null);
}
public static SysResult success(){
return new SysResult(200,"业务调用成功!!",null);
}
public static SysResult success(Object data){
return new SysResult(200,"业务调用成功!!",data);
}
public static SysResult success(String msg,Object data){
return new SysResult(200,msg,data);
}
}
package com.jt.controller;
import com.jt.pojo.SysResult;
import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/findAll")
public List findAll(){
return userService.findAll();
}
@DeleteMapping("/deleteUserById/{id}")
public SysResult deleteUserById(@PathVariable Integer id){
userService.deleteUserById(id);
return SysResult.success();
}
@GetMapping("/findUserById/{id}")
public SysResult findUserById(@PathVariable Integer id){
return SysResult.success(userService.findUserById(id));
}
@PostMapping("/add")
public SysResult addUser(@RequestBody() User user){
userService.addUser(user);
return SysResult.success();
//return SysResult.success(userService.addUser(user));
}
@PutMapping("/updateUser")
public SysResult updateUser(@RequestBody User user){
userService.updateUser(user);
return SysResult.success();
//return SysResult.success(userService.updateUser(user));
}
@PutMapping("/update")
public SysResult update(@RequestBody User user){
userService.update(user);
return SysResult.success();
}
}
package com.jt.mapper;
import com.jt.pojo.User;
import java.util.List;
public interface UserMapper {
List findAll();
void deleteUserById(Integer id);
void updateUser(User user);
void addUser(User user);
User findUserById(Integer id);
void update(User user);
}
package com.jt.mapper;
import com.jt.pojo.User;
import java.util.List;
public interface UserMapper {
List findAll();
void deleteUserById(Integer id);
void updateUser(User user);
void addUser(User user);
User findUserById(Integer id);
void update(User user);
}
package com.jt.service;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public List findAll() {
return userMapper.findAll();
}
@Override
@Transactional
public void deleteUserById(Integer id) {
userMapper.deleteUserById(id);
}
@Override
public User findUserById(Integer id) {
return userMapper.findUserById(id);
}
@Override
public void updateUser(User user) {
userMapper.updateUser(user);
}
@Override
public void addUser(User user) {
userMapper.addUser(user);
}
@Override
public void update(User user){
userMapper.update(user);
}
}
delete from runoob_tbl where runoob_id=#{runoob_id}
INSERT INTO runoob_tbl VALUES (20,'zzz','xxx','2022-05-18');
UPDATE runoob_tbl SET runoob_id=8 WHERE runoob_id=10;
update runoob_tbl set submission_date = "2022-10-01" where submission_date = "2022-10-20";