自动装配的规则说明:
1.如果对象在进行实例化.如果对象中的属性被 @Autowired注解修饰,则说明应该先注入属性.
2.先根据属性的类型,查找Map集合中是否有该类型的对象.
3.如果根据类型查找没有找到,则根据属性的名称按照name查找对象.
4.如果上述的方式都没有找到,则报错实例化对象失败.
#整合Mybatis
==========
##SpringBoot整合Mybatis说明
1.SpringBoot整合Mybatis流程
1.导入依赖jar包,数据库驱动/JDBC包/Spring整合Mybatis包
2.编辑application.yml文件 配置数据源/配置Spring整合Mybatis
3.编辑Mybtis 接口文件/编辑xxx.xml映射文件
4.通过@MapperScan为接口创建代理对象
下面是需要复制的内容
1.pom.xml依赖
org.springframework.boot
spring-boot-starter-parent
2.5.2
<java.version>1.8</java.version>
true
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-devtools
org.projectlombok
lombok
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-jdbc
com.baomidou
mybatis-plus-boot-starter
3.4.3
org.springframework.boot
spring-boot-maven-plugin
2.5.2
2.application.yml文件
需要改的地方:数据库名,账号密码,"com.jt.pojo"这个路径,8版和5版的mysql前者有cj后者无
server:
port: 8090
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jt?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root
#SpringBoot整合MP
mybatis-plus:
type-aliases-package: com.jt.pojo
mapper-locations: classpath:/mybatis/*.xml
configuration:
map-underscore-to-camel-case: true
Mapper接口执行 打印Sql日志
logging:
level:
com.jt.mapper: debug
3.xml文件
这是写sql语句的地方
<?xml version="1.0" encoding="UTF-8" ?>2.@SpringBootTest注解
说白了就是在测试类上添加这个注解就可以直接测试代码,而不需把整套框架代码写完再测
package com.jt;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Service;
import java.util.List;
/**
-
要求:
-
1.测试包路径必须在主启动类的同包及子包中.
-
2.从Spring容器中可以获取对象进行调用.
*/
@SpringBootTest
public class TestSpringBoot {
/**
- 调用mapper service controller!!单元测试~~
*/
@Autowired
private UserMapper userMapper; //代理对象
@Autowired
private UserService userService; //实现类对象
@Test
public void testGetAll(){
System.out.println(userMapper.getClass());
List userList = userMapper.getAll();
System.out.println(userList);
}
@Test
public void testService(){
List userList = userService.getAll();
System.out.println(userList);
}
}
#MybatisPlus
1.什么是MybatisPlus
说白了,就是Mybatis的升级版,简化了xml文件中的sql语句 直接用jar包里接口的方法可以实现简单的单表的增删改查
注意:使用MybatisPlus添加新jar包是要把旧的Mybatis jar包删除
MybatisPlus jar包依赖
com.baomidou
mybatis-plus-boot-starter
3.4.3
2.配置对象关系映射(就把后端和数据库关联起来)
注解报错的话 添加jar依赖时,记住要刷新下载。还报红看是不是没导包,还报红就百度
3.继承公共API接口
package com.jt.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jt.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
//注意事项: 继承接口时必须添加泛型对象 否则映射表报错.
public interface UserMapper extends BaseMapper {
//MP规则: 对象的方式操作数据库 curd操作
}
为什么要继承公共接口?因为BaseMapper接口下有很多方法,你不继承别人你怎么用?偷?
4.配置YML文件 (不能忘!!)
##MP用法(MybatisPlue)
注意:条件构造器必须加范型负责没办法添加两个条件
1.特殊字符查询
1.特殊字符(xml):由于xml识别不了 >(大于) <(小于)等符号,所以java将这些符号用了另一种方式来表达如: > gt, < lt, = eq, >= ge, <= le, != ne,
2.常用的查询方式
1.添加数据
@Test
public void testInsert(){
User user = new User();
user.setName(“吴亦凡”).setAge(30).setSex(“男”);
//单表操作几乎不写Sql
userMapper.insert(user);
}
2.根据id查询数据
/**
-
MP: 用对象操作数据库.
-
学习: 代码结构
-
案例1: 根据Id=1查询数据. ID=主键
-
Sql: SELECT id,name,age,sex FROM demo_user WHERE id=?
*/
@Test
public void getUserById(){
User user = userMapper.selectById(1);
System.out.println(user);
}
3.条件构造器-根据年龄和性别查询
/**
-
案例2: 根据age=18 sex=“女” 查询数据
-
Sql: select * from demo_user where age=18 and sex=“女”
-
休息15分钟 15上课
*/
@Test
public void getUserByAS(){
//构建条件构造器 动态的拼接where条件的
User user = new User();
user.setAge(18).setSex(“女”);
//根据对象中不为null的属性生成where条件 默认都是and 连接
QueryWrapper queryWrapper = new QueryWrapper(user);
List userList = userMapper.selectList(queryWrapper);
System.out.println(userList);
}
4.条件构造器-特殊字符查询
/**
-
案例3: 根据age>18 sex=“女” 查询数据
-
Sql: select * from demo_user where age>18 and sex=“女”
-
特殊字符(xml文件): > gt, < lt , = eq
-
>= ge, <=le, != ne
*/
@Test
public void getUserGT(){
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.gt(“age”, 18)
.eq(“sex”,“女”);
List userList = userMapper.selectList(queryWrapper);
System.out.println(userList);
}