org.projectlombok lombok 1.18.12
package demo.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data //生成Get和Set方法,重写toString @AllArgsConstructor //生成有参构造器 @NoArgsConstructor //生成无参构造器 public class User { private Integer id; private String userName; private String passwd; }
#mybatisPlus相关配置
mybatis-plus:
mapper-locations: classpath:mappers/*.xml
复制代码
通过namespace指定DAO接口的路径
复制代码
复制代码
@RequestMapping("/login")
public String login(String userName, String passwd){
User u = dao.login(userName, passwd);
if(u != null){
return "登录成功";
} else {
return "登录失败";
}
}
com.baomidou
mybatis-plus-boot-starter
3.5.1
org.projectlombok
lombok true
mysql
mysql-connector-java
runtime
复制代码
spring:
# 配置数据源信息
datasource:
# 配置数据源类型
type: com.zaxxer.hikari.HikariDataSource
# 配置连接数据库信息
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false
username: root
password: 123456
复制代码
package demo.entity;
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;
private String userName;
private String passwd;
}
复制代码
package demo.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import demo.entity.User;
public interface UserDAO extends BaseMapper {
}
复制代码
@RequestMapping("/t2")
public int insertUser(){
int count = dao.insert(new User(null, "tom", "123"));
return count;
}
复制代码
@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 t3(){
//查询全部
List 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;
}