1. 程式人生 > >springboot集成mybatis

springboot集成mybatis

ati ret com mys can quest from star map

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