本文主要对比一下传统MyBatis和MyBatisPlus实现基本增删改查的区别,可以发现MybatisPlus相比MyBatis会简洁很多,但是MyBatis的处理方式也要掌握哦,毕竟也有公司会用到MyBatis
代码如下(示例):
@Autowired(required = false)
private UserDAO dao;
代码如下(示例):
/**
* 测试向数据库中插入数据
* @return
*/
@RequestMapping("/t2")
public int insertUser(){
int count = dao.insert(new User(null, "tom", "123"));
return count;
}
/**
* 测试查询全部数据库中的数据
* @return
*/
@RequestMapping("/t3")
public List<User> t3(){
//查询全部
List<User> users = dao.selectList(null);
return users;
}
/**
* 测试根据ID查询数据
* @return
*/
@RequestMapping("/t4")
public User t4(){
//根据id查询
User u = dao.selectById(1001l);
return u;
}
/**
* 测试根据id修改数据
* @return
*/
@RequestMapping("/t5")
public int t5(){
//根据id修改
int count = dao.updateById(new User(1592405843226775554L, "mike", "321"));
return count;
}
/**
* 测试根据id删除数据
* @return
*/
@RequestMapping("/t6")
public int t6(){
int count = dao.deleteById(1001l);
return count;
}
/**
* 测试根据id批量删除数据
* @return
*/
@RequestMapping("/t7")
public int t7(){
int count = dao.deleteBatchIds(Arrays.asList(1592419001718865921L, 1592419001144246274L));
return count;
}
可以看出MyBatisPlus实现增删改查操作是可以不用自己创建xml文件来写sql语句的
<insert id="insertCar" >
insert into t_car(id,u_name,car_id,cost,valid_date) values (#{id},#{uName},#{carId},#{cost},#{validDate})
</insert>
@RequestMapping("/car1")
public int InsertCar(){
int count = mapper.insert(new Car(2l, "可莉", "苏A101", 320f, "2022-11-07"));
return count;
}
<select id="login" resultType="demo.entity.User">
select * from t_user where user_name=#{userName} and passwd=#{passwd}
select>
@RequestMapping("/login")
public String login(String userName, String passwd){
User u = dao.login(userName, passwd);
if(u != null){
return "登录成功";
} else {
return "登录失败";
}
}
<update id="updateUser">
update t_user set user_name=#{name} , passwd=#{passwd} where id=#{id}
update>
@RequestMapping("/t9")
public int t9(){
int count = dao.updateUser(new User(1001L, "tom10", "123"));
return count;
}
<delete id="deleteUser">
delete from t_user where id=#{id}
delete>
<delete id="deleteUser">
delete from t_user where id=#{id}
delete>
@RequestMapping("/t10")
public int t10(){
int count = dao.deleteUser(1001L);
return count;
}
package demo.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import demo.entity.User;
public interface UserDAO extends BaseMapper<User> {
public User login(String userName, String passwd);
public int addUser(User user);
public int updateUser(User user);
public int deleteUser(Long id);
}
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="demo.dao.UserDAO">
<select id="login" resultType="demo.entity.User">
select * from t_user where user_name=#{userName} and passwd=#{passwd}
select>
<insert id="addUser">
insert into t_user (id, user_name, passwd) values (#{id}, #{name}, #{passwd})
insert>
<update id="updateUser">
update t_user set user_name=#{name} , passwd=#{passwd} where id=#{id}
update>
<delete id="deleteUser">
delete from t_user where id=#{id}
delete>
mapper>
package demo.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data //生成Get和Set方法,重写toString
@AllArgsConstructor //生成有参构造器
@NoArgsConstructor //生成无参构造器
@TableName("t_user") //设置表名
public class User {
@TableId //指定主键id
private Long id;
@TableField("user_name")
private String name;
private String passwd;
}