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);
    }  
}

 

posted @ 2018-02-10 20:37  硅谷工具人  阅读(209)  评论(0编辑  收藏  举报
成功之道,在于每个人生阶段都要有不同的目标,并且通过努力实现自己的目标,毕竟人生不过百年! 所有奋斗的意义在于为个人目标实现和提升家庭幸福,同时能推进社会进步和国家目标! 正如古人讲的正心诚意格物致知,修身齐家治国平天下。