SpringBoot学习笔记:配置Mybatis

Posted 子烁爱学习

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot学习笔记:配置Mybatis相关的知识,希望对你有一定的参考价值。

SpringBoot学习笔记(1):配置Mybatis

参考资料:

  1.AndyLizh的博客

  2.xiaolyuh123的博客

快速开始

添加Mybatis依赖(其他依赖已省去)

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>  

配置application.properties文件

server.port=80
# 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/ssb_test
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
#连接池配置
#spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource

#mybatis
#entity扫描的包名
mybatis.type-aliases-package=com.xiaolyuh.domain.model
#Mapper.xml所在的位置
mybatis.mapper-locations=classpath*:/mybaits/*Mapper.xml

#pagehelper分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

#日志配置
logging.level.com.xiaolyuh=debug
logging.level.org.springframework.web=debug
logging.level.org.springframework.transaction=debug
logging.level.org.mybatis=debug

debug=false

mybatis除了以上两种常见配置外,还有:

mybatis.config-location=mybatis-config.xml配置文件的路径
mybatis.type-handlers-package=扫描typeHandlers的包
mybatis.check-config-location=检查配置文件是否存在
mybatis.executor-type=设置执行模式(SIMPLE, REUSE, BATCH),默认为SIMPLE

开发Mapper

@Mapper
public interface UserMapper { @Select("SELECT * FROM users") @Results({ @Result(property = "userSex", column = "user_sex", javaType = UserSexEnum.class), @Result(property = "nickName", column = "nick_name") }) List<UserEntity> getAll(); @Select("SELECT * FROM users WHERE id = #{id}") @Results({ @Result(property = "userSex", column = "user_sex", javaType = UserSexEnum.class), @Result(property = "nickName", column = "nick_name") }) UserEntity getOne(Long id); @Insert("INSERT INTO users(userName,passWord,user_sex) VALUES(#{userName}, #{passWord}, #{userSex})") void insert(UserEntity user); @Update("UPDATE users SET userName=#{userName},nick_name=#{nickName} WHERE id =#{id}") void update(UserEntity user); @Delete("DELETE FROM users WHERE id =#{id}") void delete(Long id); }

开始使用

  上面三步就基本完成了相关dao层开发,使用的时候当作普通的类注入即可。

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {

    @Autowired
    private UserMapper UserMapper;

    @Test
    public void testInsert() throws Exception {
        UserMapper.insert(new UserEntity("aa", "a123456", UserSexEnum.MAN));
        UserMapper.insert(new UserEntity("bb", "b123456", UserSexEnum.WOMAN));
        UserMapper.insert(new UserEntity("cc", "b123456", UserSexEnum.WOMAN));

        Assert.assertEquals(3, UserMapper.getAll().size());
    }

    @Test
    public void testQuery() throws Exception {
        List<UserEntity> users = UserMapper.getAll();
        System.out.println(users.toString());
    }

    @Test
    public void testUpdate() throws Exception {
        UserEntity user = UserMapper.getOne(3l);
        System.out.println(user.toString());
        user.setNickName("neo");
        UserMapper.update(user);
        Assert.assertTrue(("neo".equals(UserMapper.getOne(3l).getNickName())));
    }
}

Mapper的注册方式

启动类添加注解

@SpringBootApplication  
@MapperScan("com.kerry.mapper")  
public class Application {  
  
    public static void main(String[] args) {  
        SpringApplication.run(Application.class, args);  
    }  
}  

每个接口类单独添加注解

package com.kerry.mapper;  
  
import java.util.List;  
  
import org.apache.ibatis.annotations.Mapper;  
  
import com.kerry.model.User;  
  
@Mapper  
public interface UserMapper {  
      
    List<User> getAll();  
      
    User getOne(Integer id);  
  
    void insert(User user);  
  
    void update(User user);  
  
    void delete(Integer id);  
  
}  

以上是关于SpringBoot学习笔记:配置Mybatis的主要内容,如果未能解决你的问题,请参考以下文章

Springboot学习笔记7:整合Mybatis

SpringBoot学习笔记整合Mybatis

SpringBoot学习笔记配置druid

SpringBoot学习笔记:整合mybatis

Springboot+Mybatis 学习笔记

MyBatisPlus学习笔记