springboot集成mybatis

Posted 胖子学习天地

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot集成mybatis相关的知识,希望对你有一定的参考价值。

1. pom.xml中添加依赖包

    <!-- jdbc -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

2. 配置文件application.properties添加jdbc连接方式

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3. 添加mapper(注解方式)

public interface UserMapper {
    
    @Select("select id,user_code as code,user_name as username,user_pwd as password,update_tmsp as updateTime "
            + " from m_user "
            + " where user_name = #{name}")
    public User findUser(@Param("name") String name);
    
}

4. controller中注入mapper

    @Autowired
    private UserMapper userMapper;
    
    @RequestMapping("/queryUser")
    public User getUser(String name){
        return userMapper.findUser(name);
    }

5. 启动程序app中添加mapper包扫描

@ComponentScan(basePackages={"com.king.controller"})
@MapperScan(basePackages = "com.king.mapper")
@EnableAutoConfiguration
public class App {
    
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }  
}

 

以上是关于springboot集成mybatis的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot集成mybatis以及自动化测试代码实现

SpringBoot集成mybatis

springboot-简单集成mybatis

01.springboot集成mybatis与jsp

SpringBoot 集成MyBatis-Plus提示反序列化异常:cannot deserialize from Object value (no delegate- or property-bas

Springboot 和 Mybatis集成开发