SpringBoot整合MyBatis 和 MyBatis-Plus

待我称王封你为后i 2024-03-25 23:19 176阅读 0赞

Java知识点总结:想看的可以从这里进入

目录

      • 2.10、整合MyBatis
      • 2.11、MyBatis-Plus

2.10、整合MyBatis

  1. 导入MyBatis的启动器

    1. <!--mybatis的启动器-->
    2. <dependency>
    3. <groupId>org.mybatis.spring.boot</groupId>
    4. <artifactId>mybatis-spring-boot-starter</artifactId>
    5. <version>2.2.0</version>
    6. </dependency>
  2. yml文件中配置Mybatis属性 (可以导入mybatis的XML配置文件)

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <!DOCTYPE configuration
    3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    5. <configuration>
    6. <!--setting设置-->
    7. <settings>
    8. <!--日志STDOUT_LOGGING和LOG4J-->
    9. <setting name="logImpl" value="LOG4J"/>
    10. </settings>
    11. <!--设置别名 -->
    12. <typeAliases>
    13. <package name="com.yu.springbootproject.pojo"/>
    14. </typeAliases>
    15. </configuration>
    16. # mybatis的配置
    17. mybatis:
    18. config-location: classpath:mybatis/mybatis.xml #mybatis配置文件的位置
    19. mapper-locations: classpath:mapping/*.xml #mapper的映射文件xml的位置,如果配置文件中配置了mappers,这里就不能配置了,会产生冲突
    20. #type-aliases-package: ***.mapper 扫描mapper接口,可以在启动类指定
    21. #configuration:
    22. #map-underscore-to-camel-case: true 默认开启驼峰命名法和 config-location 有冲突,只能写一个
  3. 启动类添加包的扫描

    1. @SpringBootApplication
    2. @MapperScan("xxxx.项目名.mapper")
    3. public class SpringbootprojectApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(SpringbootprojectApplication.class, args);
    6. }
    7. }
  4. 添加log4j的日志

    1. <dependency>
    2. <groupId>log4j</groupId>
    3. <artifactId>log4j</artifactId>
    4. <version>1.2.17</version>
    5. </dependency>
    6. #将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码
    7. log4j.rootLogger=DEBUG,console,file
    8. #控制台输出的相关设置
    9. log4j.appender.console=org.apache.log4j.ConsoleAppender
    10. log4j.appender.console.Target=System.out
    11. log4j.appender.console.Threshold=DEBUG
    12. log4j.appender.console.layout=org.apache.log4j.PatternLayout
    13. log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] [%c] [%p] -%m%n
    14. #文件输出的相关设置
    15. #log4j.appender.file = org.apache.log4j.RollingFileAppender
    16. #log4j.appender.file.File=
    17. #log4j.appender.file.MaxFileSize=10mb
    18. #log4j.appender.file.Threshold=DEBUG
    19. #log4j.appender.file.layout=org.apache.log4j.PatternLayout
    20. #log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] [%c] [%p] -%m%n
    21. #日志输出级别
    22. log4j.logger.org.mybatis=DEBUG
    23. log4j.logger.java.sql=DEBUG
    24. log4j.logger.java.sql.Statement=DEBUG
    25. log4j.logger.java.sql.ResultSet=DEBUG
    26. log4j.logger.java.sql.PreparedStatement=DEBUG
  5. 接口的扫描

    1. 在mapper接口中添加注解 @Mapper

      1. @Mapper
      2. public interface UserMapper {
      3. User selectByPrimaryKey(Integer userId);
      4. }
    2. 映射xml文件

      1. <?xml version="1.0" encoding="UTF-8"?>
      2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
      3. <mapper namespace="com.yu.springbootproject.mapper.UserMapper">
      4. <resultMap id="BaseResultMap" type="com.yu.springbootproject.pojo.User">
      5. <id column="user_id" jdbcType="INTEGER" property="userId" />
      6. <result column="username" jdbcType="VARCHAR" property="username" />
      7. <result column="password" jdbcType="VARCHAR" property="password" />
      8. <result column="deleted" jdbcType="BIT" property="deleted" />
      9. </resultMap>
      10. <sql id="Base_Column_List">
      11. user_id, username, `password`, deleted
      12. </sql>
      13. <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
      14. select
      15. <include refid="Base_Column_List" />
      16. from `user`
      17. where user_id = #{userId,jdbcType=INTEGER}
      18. </select>
      19. </mapper>
    3. 编写Service,然后进行测试

      1. public interface UserService {
      2. User selectByPrimaryKey(Integer userId);
      3. }
      4. public class UserServiceImpl implements UserService {
      5. @Autowired
      6. private UserMapper userMapper;
      7. @Override
      8. public User selectByPrimaryKey(Integer userId) {
      9. return userMapper.selectByPrimaryKey(userId);
      10. }
      11. }
      12. @Resource
      13. private UserService userService;
      14. @Test
      15. public void userMapperTest(){
      16. User user = userService.selectByPrimaryKey(1);
      17. System.out.println(user.toString());
      18. }

    image-20220923152906081

2.11、MyBatis-Plus

MyBatis-Plus 是基于Mybatis的一个工具,它是对MyBatis的增强,在其基础上只进行增强,不进行改变,是为了简化MyBatis开发,提高效率而研发的。

整合MyBatis-Plus 需要将MyBatis 的相关依赖移除。

  1. 导入MyBatis-Plus的启动器(移除原来mybatis的)

    1. <!--mybatis-plus -->
    2. <dependency>
    3. <groupId>com.baomidou</groupId>
    4. <artifactId>mybatis-plus-boot-starter</artifactId>
    5. <version>3.5.2</version>
    6. </dependency>
  2. yml文件中配置mybatis-plus

    1. # mybatis-plus的配置
    2. mybatis-plus:
    3. type-aliases-package: com.yu.springboot.entity #实体类路径
    4. mapper-locations: classpath:mapping/*.xml # mappee接口映射的xml文件路径
    5. configuration:
    6. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #开启sql打印日志
    7. map-underscore-to-camel-case: true #将带有下划线的表字段映射为驼峰格式的实体类属性
  3. 启动类添加包的扫描(不变)
  4. 接口的扫描

    1. 在mapper接口继承 BaseMapper<>

      1. public interface UserinfoMapper extends BaseMapper<Userinfo> {
      2. }
    2. 映射xml文件

      1. <mapper namespace="com.yu.springboot2test.mapper.UserinfoMapper">
      2. <resultMap id="BaseResultMap" type="com.yu.springboot2test.entity.Userinfo">
      3. <id column="id" jdbcType="BIGINT" property="id" />
      4. <result column="username" jdbcType="VARCHAR" property="username" />
      5. <result column="password" jdbcType="VARCHAR" property="password" />
      6. <result column="age" jdbcType="INTEGER" property="age" />
      7. <result column="is_deleted" jdbcType="BIT" property="isDeleted" />
      8. <result column="gmt_create" jdbcType="TIMESTAMP" property="gmtCreate" />
      9. <result column="gmt_modified" jdbcType="TIMESTAMP" property="gmtModified" />
      10. </resultMap>
      11. <sql id="Base_Column_List">
      12. id, username, `password`, age, is_deleted, gmt_create, gmt_modified
      13. </sql>
      14. </mapper>
    3. 编写Service,然后进行测试

      service接口继承IService,实现类继承ServiceImpl

      1. public interface UserinfoService extends IService<Userinfo> {
      2. }
      3. @Service
      4. public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo> implements UserinfoService {
      5. }
      6. @Resource
      7. private UserinfoMapper userinfoMapper;
      8. @Test
      9. public void test2(){
      10. Userinfo userinfo = userinfoMapper.selectById(1L);
      11. System.out.println(userinfo);
      12. }

      image-20230308174349798

发表评论

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

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

相关阅读