整合 mtbatis plus 这个是 pom 依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.com.jt.jt</groupId>
<artifactId>springboot_demo2_mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_demo2_mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入插件 lombok 主动的 set/get/ 构造方法插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- 引入数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--springBoot 数据库连贯 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--spring 整合 mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
dao 层接口 须要继承 BaseMapper<User> 能力应用 MP 工具
/@Mapper // 将接口交给 Spring 治理
public interface UserDao extends BaseMapper<User> {
// 查问 user 表的所有的记录
@Select("select * from user")
List<User> findAll();}
pojo 层 设置办法
@Data
@Accessors(chain = true)
@TableName //("user") //1. 将对象与表 进行一对一关联
public class User implements Serializable {@TableId(type = IdType.AUTO) // 主键的信息 设定自增
private Integer Id;
//@TableField("name") // 如果字段名称与属性的名称统一 (蕴含驼峰规定), 能够省略不写
private String name;
private Integer age;
private String sex;
}
加这个注解能够间接 不必在 dao 层加 mappr 单注解了
@SpringBootApplication
@MapperScan("com.com.jt.jt.dao") // 次要通知 mapper 的包门路, 会主动的实现包扫描
public class SpringbootDemo2MybatisApplication {public static void main(String[] args) {SpringApplication.run(SpringbootDemo2MybatisApplication.class, args);
}
}
xml 总配置
server:
port: 8090 #定义端口
servlet:
context-path: / #定义我的项目的公布门路
spring:
datasource:
#driver-class-name: com.mysql.cj.jdbc.Driver springboot 程序采纳默认的配置
url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root
mybatis-plus:
#定义别名包
type-aliases-package: com.com.jt.jt.pojo
#加载 user 标签的 mapper 文件
mapper-locations: classpath:/mybatis/mappers/*.xml
#开启驼峰映射
configuration:
map-underscore-to-camel-case: true
#引入日志信息.
logging:
level:
com.jt.dao: debug
根本数据 MP 工具
@SpringBootTest
class SpringbootDemo2MybatisApplicationTests {
@Autowired
private UserDao userDao;
@Test
public void testFind(){List<User> userList = userDao.findAll();
System.out.println(userList);
}
@Test
public void testSelect01(){List<User> userList = userDao.selectList(null);
System.out.println(userList);
}
/**
* 业务: 查问 id=11 的用户信息 主键...
*/
@Test
public void testSelect02(){User user = userDao.selectById(11);
System.out.println(user);
}
/**
* 业务: 查问 name 属性为 "小乔" 的数据
* sql: select * from user where name="小乔";
* 对象的形式 > sql 形式
*/
@Test
public void testSelect03(){
// 条件结构器
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", "小乔");
List<User> userList = userDao.selectList(queryWrapper);
System.out.println(userList);
}
/**
* 业务: 查问 name 属性为 "小乔" 的数据 并且 age >=18 岁
* sql: select * from user where name="小乔" and age>=18;
*
* 大于 > gt| 小于 < lt |
* 大于等于 >= ge | 小于等于 le
*/
@Test
public void testSelect04(){
// 条件结构器
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", "小乔")
.ge("age", 18);
List<User> userList = userDao.selectList(queryWrapper);
System.out.println(userList);
}
/**
* 业务: 查问 name 中蕴含 "精" 的用户, 并且 sex 为女
* 业务: 查问 name 中蕴含 以精结尾的数据, 并且 sex 为女
* sql: select * from user where name like "% 精 %" and sex="女";
*/
@Test
public void testSelect05(){
// 条件结构器
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.likeLeft("name", "精")
.eq("sex", "女");
List<User> userList = userDao.selectList(queryWrapper);
System.out.println(userList);
}
/**
* 需要: 查问 user 表中的数据 要求依照年龄降序排列, 如果年龄雷同依照 id 降序排列
*/
@Test
public void testSelect06(){QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("age","id");
List<User> userList = userDao.selectList(queryWrapper);
System.out.println(userList);
}
/**
* 需要: 查问 name 属性为 null 的数据.
* where name is null
*/
@Test
public void testSelect07(){QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.isNull("name");
List<User> userList = userDao.selectList(queryWrapper);
System.out.println(userList);
}
/**
* 查问 name="小乔" age=17 性别 = 女的用户
* 如果传递的是对象. 会依据对象中不为 null 的属性充当 where 条件.
*/
@Test
public void testSelect08(){User user = new User();
user.setName("小乔").setAge(17).setSex("女");
QueryWrapper<User> queryWrapper = new QueryWrapper<>(user);
List<User> userList = userDao.selectList(queryWrapper);
System.out.println(userList);
}
/**
* 查问 id=1,3,5,10 数据.
* sql: select * from user where id in (1,3,5,10)
*/
@Test
public void testSelect09(){
// 转化时, 留神应用对象的类型
Integer[] ids = {1,3,5,10};
List<Integer> idList = Arrays.asList(ids);
List<User> userList = userDao.selectBatchIds(idList);
System.out.println(userList);
}
@Test
public void testInsert(){User user = new User();
user.setName("特朗普").setAge(70).setSex("男");
userDao.insert(user);
}
/**
* 删除 name=null 的数据, 或者 name="特朗普"
*/
@Test
public void testDelete(){QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.isNull("name")
.or()
.eq("name", "特朗普");
userDao.delete(queryWrapper);
}
/**
* 案例 1: 将 id= 1 的数据的年龄改为 8000 岁.
* sql1: update user set age=8000 where id=1;
* 案例 2: 要求将 name="黑熊精" age=5500.
* sql2: update user set age=5500 where name="黑熊精";
*/
@Test
public void testUpdate(){User user = new User();
user.setId(1); // 主键充当 where 条件
user.setAge(8000);
userDao.updateById(user);
// 参数阐明 1. 实体对象 封装 set 条件的值
// 2. 更新条件结构器
User temp = new User();
temp.setAge(5500);
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("name", "黑熊精");
userDao.update(temp,updateWrapper);
}