SpringBoot学习(四)—— springboot快速整合Mybatis组件

缺乏、安全感 2023-06-16 07:56 58阅读 0赞

SpringBoot学习(一)—— idea 快速搭建 Spring boot 框架

SpringBoot学习(二)—— springboot快速整合spring security组件

SpringBoot学习(三)—— springboot快速整合swagger文档

SpringBoot学习(四)—— springboot快速整合Mybatis组件

SpringBoot学习(五)—— springboot快速整合Druid

SpringBoot学习(六)—— springboot快速整合RabbitMQ

SpringBoot学习(七)—— springboot快速整合Redis

MyBatis

文章目录

  • MyBatis
      • 简介
          • 优点
          • 劣势
      • 引入mybatis组件
      • 代码实战

简介

优点

最大的优点是SQL语句灵活,适合调优情景,业务复杂情景

劣势

最大的劣势是不同数据库之间的迁移

引入mybatis组件

pom.xml中加入

  1. <dependency>
  2. <groupId>mysql</groupId>
  3. <artifactId>mysql-connector-java</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.mybatis.spring.boot</groupId>
  7. <artifactId>mybatis-spring-boot-starter</artifactId>
  8. <version>2.1.1</version>
  9. </dependency>

application.properties中加入

  1. #dateSource
  2. spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
  3. spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
  4. spring.datasource.username=root
  5. spring.datasource.password=root
  6. # mybatis
  7. # 下划线转驼峰开启
  8. mybatis.configuration.map-underscore-to-camel-case=true
  9. # mapper扫描位置
  10. mybatis.mapper-locations=classpath:mapper/*.xml

需要说明的是,我引入的mysql驱动为 com.mysql.cj.jdbc.Driver,而不是 com.mysql.jdbc.Driver。以下是一些参数的说明;

  • serverTimezone:该驱动需要指定某时区。
  • useUnicode:开启指定编码。
  • characterEncoding:指定读取数据库的编码,因为项目采用UTF-8,存取数据库信息时保持一致。
  • useSSL:是否建立SSL连接,我显示选的否,因为这要为服务器证书验证提供信任库,暂时没条件。

代码实战

本地的 mysql 版本为 5.7.20。

建表语句

  1. CREATE TABLE USER_INFO
  2. (
  3. user_id DECIMAL(10) PRIMARY KEY NOT NULL,
  4. user_name VARCHAR(20) DEFAULT "" NOT NULL
  5. );
  6. CREATE UNIQUE INDEX USER_INFO_user_id_uindex ON USER_INFO (user_id);
  7. ALTER TABLE USER_INFO COMMENT = '用户基本信息表';

插入数据

  1. INSERT INTO test.user_info (user_id, user_name) VALUES (1, '特朗普');
  2. INSERT INTO test.user_info (user_id, user_name) VALUES (2, '唐纳德');

现在的项目路径为

在这里插入图片描述

在SpringBoot的入口类中加 @MapperScan 注解,以扫描 DAO 类,不用在每个dao接口加个@Mapper 注解。

  1. package com.example;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @SpringBootApplication
  6. @MapperScan("com.example.dao")
  7. public class DemoApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(DemoApplication.class, args);
  10. }
  11. }

注:如果没有分拆项目成微服务架构,或分布式架构,application主类中的@SpringBootApplication自动会扫描本包中的@Controller,@Service,@Resource等,是不需要浪费另一行@ComponentScan注解,配置路径的。

UserController.java

  1. package com.example.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. import com.example.service.IUserService;
  7. @Controller
  8. @RequestMapping("user")
  9. public class UserController {
  10. @Autowired
  11. private IUserService userService;
  12. @RequestMapping("/queryUser")
  13. @ResponseBody
  14. void queryUser() {
  15. this.userService.queryUser();
  16. }
  17. }

IUserService.java

  1. package com.example.service;
  2. public interface IUserService {
  3. void queryUser();
  4. }

UserServiceIml.java

  1. package com.example.service;
  2. import com.example.dao.UserDao;
  3. import com.example.entity.UserEntity;
  4. import org.springframework.stereotype.Service;
  5. import javax.annotation.Resource;
  6. import java.util.List;
  7. @Service("userService")
  8. public class UserServiceIml implements IUserService {
  9. @Resource
  10. private UserDao userDao;
  11. @Override
  12. public void queryUser() {
  13. List<UserEntity> userList = this.userDao.queryUser();
  14. System.out.println("================");
  15. System.out.println(userList);
  16. System.out.println("================");
  17. }
  18. }

注:如果是SSM架构过来的,这个@Resource注解可能比较陌生,因为我估计会和我之前一样用@Autowired,但是我是没有在dao层用 @Repository 注解的,之前用了在主类用了@MapperScan自动去扫描所有的dao,所以不能再用之前的@Autowired。

UserDao.java

  1. package com.example.dao;
  2. import com.example.entity.UserEntity;
  3. import java.util.List;
  4. public interface UserDao {
  5. List<UserEntity> queryUser();
  6. }

UserEntity.java

  1. package com.example.entity;
  2. public class UserEntity {
  3. private long userId;
  4. private String userName;
  5. public long getUserId() {
  6. return userId;
  7. }
  8. public void setUserId(long userId) {
  9. this.userId = userId;
  10. }
  11. public String getUserName() {
  12. return userName;
  13. }
  14. public void setUserName(String userName) {
  15. this.userName = userName;
  16. }
  17. @Override
  18. public String toString() {
  19. return "UserEntity{" +
  20. "userId=" + userId +
  21. ", userName='" + userName + '\'' +
  22. '}';
  23. }
  24. }

如果你运行不成功,请对比引入的包的版本,和数据库配置是否是根据你本地的,顺便看看注解是否和文中一样。

效果图如下;

在这里插入图片描述

注:如果是跟着本教程来的,因为一开始就引入了spring security,而新增的路径不在刚才的权限中,所以我给zs用户改成了皆可访问。如果没有跟着之前配置spring security,无需理会如下内容

  1. //http.authorizeRequests().antMatchers("/user/addUser").hasRole("AAA")
  2. http.authorizeRequests().antMatchers("/**").hasRole("AAA")

发表评论

表情:
评论列表 (有 0 条评论,58人围观)

还没有评论,来说两句吧...

相关阅读