简单使用
依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<!-- 引入阿里数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
数据库表
CREATE TABLE `student_info` (
`student_id` BIGINT(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`student_name` VARCHAR(32) DEFAULT NULL COMMENT '姓名',
`student_age` INT(11) DEFAULT NULL COMMENT '年龄',
PRIMARY KEY (`student_id`)
)
实体类StudentInfo
public class StudentInfo {
private int studentId;
private String studentName;
private int studentAge;
}
mybatis-plus
:有自动映射实体类和表之间的关系,驼峰命名规则- 表名:
student_info
对应的实体类StudentInfo
- 字段名:
student_id
:对应属性名:studentId
- 如果你的命名规则是这样,就可以不用在写映射关系了
dao
层
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mp.demo.entity.StudentInfo;
import org.springframework.stereotype.Component;
@Component
public interface StudentInfoDao extends BaseMapper<StudentInfo> {
}
- dao层的接口,必须继承
BaseMapper
类,这样才算使用了mybatis-plus
,而泛型对应的就是你需要传参和返回的类
BaseMapper<T>
接口的具体代码
public interface BaseMapper<T> {
int insert(T entity);
int deleteById(Serializable id);
int deleteByMap(@Param("cm") Map<String, Object> columnMap);
int delete(@Param("ew") Wrapper<T> wrapper);
int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);
int updateById(@Param("et") T entity);
int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
T selectById(Serializable id);
List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);
List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
T selectOne(@Param("ew") Wrapper<T> queryWrapper);
Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);
List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);
List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);
List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);
IPage<T> selectPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper);
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper);
}
BaseMapper
:帮你实现了一些简单的操作表的方法int insert(T entity);
:就是往表插入一条数据List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);
:查询该表的所有数据
最重要的是,如果你只是单纯使用改接口已有的方法,就可以不用在写xml
文件了
- 接下来展示
selectList
:查询该表的所有数据
service
层
import com.mp.demo.entity.StudentInfo;
import java.util.List;
public interface StudentInfoService {
public List<StudentInfo> selectAll();
}
@Service
public class StudentInfoServiceImpl implements StudentInfoService {
@Autowired
private StudentInfoDao studentInfoDao;
@Override
public List<StudentInfo> selectAll() {
return studentInfoDao.selectList(null);
}
}
Controller
层
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentInfoService studentInfoService;
@GetMapping("/getList")
public List<StudentInfo> getStudentAllList(){
return studentInfoService.selectAll();
}
}
application.yml
server:
port: 8080
spring:
# 配置数据源
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
# mybatis-plus相关配置
mybatis-plus:
# xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置)
mapper-locations: classpath:mapper/*.xml
# 以下配置均有默认值,可以不设置
global-config:
db-config:
#主键类型 AUTO:"数据库ID自增" INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
id-type: auto
#字段策略 IGNORED:"忽略判断" NOT_NULL:"非 NULL 判断") NOT_EMPTY:"非空判断"
field-strategy: NOT_EMPTY
#数据库类型
db-type: MYSQL
configuration:
# 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
map-underscore-to-camel-case: true
# 返回map时true:当查询数据为空时字段返回为null,false:不加这个查询数据为空时,字段将被隐藏
call-setters-on-nulls: true
# 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
在入口类配置扫描dao层
@SpringBootApplication
@MapperScan(basePackages = {"com.mp.demo.dao"}) //扫描DAO
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
测试
- 自己先往数据库插一条数据
http://localhost:8080/student/getList
自定义sql语句
dao层
@Component
public interface StudentInfoDao extends BaseMapper<StudentInfo> {
public List<StudentInfo> fuzzySelectByName(String name);
}
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mp.demo.dao.StudentInfoDao">
<resultMap id="BaseResultMap" type="com.mp.demo.entity.StudentInfo">
<id column="student_id" property="studentId"/>
<result column="student_name" property="studentName"/>
<result column="student_age" property="studentAge"/>
</resultMap>
<select id="fuzzySelectByName" parameterType="String" resultMap="BaseResultMap">
select student_id,student_name,student_age
from student_info
where student_name like concat('%',#{name},'%')
</select>
</mapper>
service
public interface StudentInfoService {
public List<StudentInfo> selectAll();
public List<StudentInfo> fuzzySelectByName(String name);
}
@Service
public class StudentInfoServiceImpl implements StudentInfoService {
@Autowired
private StudentInfoDao studentInfoDao;
@Override
public List<StudentInfo> selectAll() {
return studentInfoDao.selectList(null);
}
@Override
public List<StudentInfo> fuzzySelectByName(String name) {
return studentInfoDao.fuzzySelectByName(name);
}
}
controler
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentInfoService studentInfoService;
@GetMapping("/getList")
public List<StudentInfo> getStudentAllList(){
return studentInfoService.selectAll();
}
@GetMapping("/fuzzyQueryStudent")
public List<StudentInfo> getStudentByName(String name){
return studentInfoService.fuzzySelectByName(name);
}
}
github代码
https://github.com/tangpengyi/mybatis-plus-demo