zoukankan      html  css  js  c++  java
  • 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);
        }  
    }
  • 相关阅读:
    day08
    day07
    day06
    day06
    day05
    第三次作业
    第三次作业
    第二次作业
    java 数字和日期处理
    jsp文件导包
  • 原文地址:https://www.cnblogs.com/30go/p/8439688.html
Copyright © 2011-2022 走看看