官网地址:mybatis – MyBatis 3 | 简介
MyBatis是一款优秀的持久层框架,它支持自定义SQL、存储过程以及高级映射,MyBatis免除了几乎所有的JDBC代码以及设置参数和获取结果的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
总结:1.MyBatis是一款优秀的持久层框架,在内部封装了JDBC,简化了操作数据库的过程
2.它支持自定义SQL、存储过程以及高级映射。
映射:
数据库中的表与POJO中的类——映射
表中的字段与类中的属性——映射
1.2 导入数据库导入后刷新
1.3 入门案例 1.3.1 创建项目 1.3.2 导入项目1.配置pom.xml配置文件
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.0
mysql
mysql-connector-java
org.projectlombok
lombok
2.导入jar包
mybatis-spring-boot-starter
mysql-connector-java
lombok
3.导入src
1.4 lombok插件安装 1.4.1 lombok介绍说明:pojo对象中必须添加jget/set/toString等常规方法,但是该方法写起来繁琐,但是又必须添加
所以可以引入lombok插件动态生成上述的方法。
1.4.2 导入jar包
lombok
上面的
1.4.3 安装lombok插件setting-Plugins-搜索lombok安装,重启IDEA
lombok 版本地址: https://plugins.jetbrains.com/plugin/6317-lombok/versions
1.4.4 lombok注解标识POJO@Data //动态生成get/set/toString/equals/hashCode等方法
@Accessors(chain=true) //开启链式加载 重写set方法
@NoArgsConstructor //无参构造
@AllArgsConstructor //全参构造
链式加载规则:
1.重写set方法
2.要求返回当前对象@Data //动态生成get/set/toString/equals/hashCode等方法
@Accessors(chain = true) //开启链式加载 重写set方法
@NoArgsConstructor //无参构造
@AllArgsConstructor //全参构造
public class User implements Serializable {
private Integer id;
private String name;
private Integer age;
private String sex;
public void xx(){
User user3=new User();
//链式加载规则,重写set方法,要求返回当前对象user
user3.setName("xxx").setSex("女").setAge(18).setId(1);
}
public User setId(Integer id){
this.id=id;
return this; //this 代表当前对象! 运行期有效!!
}
}
1.4.5 编辑Mapper接口
规则:基于面向接口开发的规则,准备一个新的接口
@Mapper //将接口交给Spring容器管理 Map
指定接口方法,查询所有的用户信息
@Mapper //将接口交给Spring容器管理 Map1.4.6 编辑Mapper的映射文件public interface UserMapper { //指定接口方法 查询User的全部数据 //查询所有的用户信息 List findAll(); }
核心作用:编辑操作数据库的sql
1.namespace是mybatis映射文件的唯一标识,与接口对应
调用流程:(1)编辑Mapper接口,编辑接口方法
(2)映射文件与Mapper接口绑定
2.
执行业务操作 id:与接口方法一一对应
resultType:返回POJO对象类型全路径
mybatis自动的将结果集自动的封装为对象
如果返回值是List集合,则自动封装为List,User充当其中的泛型对象
如果返回值是User对象,则直接返回User对象
1.4.7 Spring整合Mybatis的配置
说明:Mybatis原生代码的结构混乱,比较复杂,所以采用Spring整合mybatis的操作简化开发的步骤
其中标识spring整合mybatis的步骤;配置信息了解
#1.配置端口号 注意缩进!!!!!
server:
port: 8090
#2.配置数据源
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
#yml文件 0不解析 如果字母以0开头则引号包裹
#password: "0123456"
password: root
#3.配置Mybatis
mybatis:
#定义别名包
type-aliases-package: com.jt.pojo
#将所有的映射文件全部加载
mapper-locations: classpath:/mappers/*.xml
#开启驼峰映射
configuration:
map-underscore-to-camel-case: true
#4.打印Sql com.jt.mapper下的Sql日志
logging:
level:
com.jt.mapper: debug
1.4.8 Spring的测试类
说明:Spring为了后期测试代码方便,专门针对于测试方法,开发了一个注解@SpringBootTest
@SpringBootTest //该注解的作用启动spring容器中,之后动态的获取对象
public class TestMybatis {
@Autowired //IDEA编译异常 不影响代码的执行
private UserMapper userMapper;
@Test
public void test01(){
List userList = userMapper.findAll();
System.out.println(userList);
}
}
1.@Configuration 标识当前类是配置类
2.@ComponentScan 包扫描注解 扫描注解
3.@Bean 标识该方法的返回值交给Spring容器管理
4.@Scope 控制多例和单例
5.@Lazy 懒加载
6.@PostConstruct 初始化方法
7.@PreDestroy 销毁方法
8.@Component 将当前类未来的对象交给容器管理
9.@Autowired 按照类型进行注入
10.@Qualifier 按照名称进行注入
11.@Repository 标识持久层注解
12.@Service 标识Service层
13.@Controller 标识Controller层
14.@Value 为属性赋值 @Value("${key}")
15.@PropertySource 加载指定路径的配置文件properties
16.@Aspect 标识当前类是一个切面类
17.@Pointcut 用于定义切入点表达式 表达式写法4种
18.@EnableAspectJAutoProxy 让AOP的注解有效果
19.@Before AOP-前置通知
20.@AfterReturning AOP-后置通知
21.@AfterThrowing AOP-异常通知
22.@After AOP-最终通知
23.@Around AOP-环绕通知
24.@Order(1) //可以利用order关键字 实现AOP的排序 数字越小越先执行.
25.@ResponseBody 将返回的数据转化为JSON串, 如果是字符串本身 原数据返回
26.@RequestMapping("/hello") 实现浏览器的请求路径与方法的映射
27.@PathVariable restFul结构,接收参数的注解.
28.@GetMapping("") 只能接收GET请求类型
29.@DeleteMapping("") 只能接收DELETE请求类型
30.@PostMapping("") 只能接收POST请求类型
31.@PutMapping("") 只能接收PUT请求类型
32.@RestController 表示Controller类,同时要求返回值为JSON
33.@CrossOrigin 允许跨域访问
34.@RequestBody 参数接收时,将JSON串转化为java对象 json中的key与对象的属性一致.
35.@Data lombok动态生成get/set/toString/equals/hashcode等方法
36.@Accessors 控制是否开启链式加载结构
37.@NoArgsConstructor 生成无参构造方法
38.@AllArgsConstructor 生成全参构造方法
39.@Mapper mybatis将当前的接口交给Spring容器管理. Map<类名小写,JDK动态代理对象>
40.@SpringBootTest 该注解的作用在进行代码测试时启动spring容器,之后动态的获取对象 注意包路径 主启动类的同包及子包中.



