zoukankan      html  css  js  c++  java
  • springboot整合mybatis

    1、pom.xml引入mybatis依赖

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

    2、指定扫描路径:@MapperScan(value = "com.demo.dao")

    /*
    * 启动类
    */
    @SpringBootApplication
    @ComponentScan("com.demo.*")
    @MapperScan(value = "com.demo.dao")
    public class DemoApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(DemoApplication.class, args);
    	}
    
    }
    

    2.1、dao例子:com.demo.dao.DeptDao

    public interface DeptDao {
    
        @Select("select * from dept")
        public List<Dept> getDeptList();
    
        @SelectProvider(type= DeptProvider.class,method = "getDeptById")
        public Dept getDeptById(@Param("id") String id);
    }
    

    2.2、provider可以写复杂点SQL。下面例子比较简单,单纯为了实现效果。

    public class DeptProvider {
    
        public String getDeptById(Map<String,Object> param){
            StringBuffer sb = new StringBuffer();
            sb.append("select * from dept where id = #{id}");
            return sb.toString();
        }
    
    }
    

    3、完成上面2步骤,dao层就可以提供给service调用了。

    @Service
    public class DeptServiceImpl implements DeptService {
        
        @Autowired
        DeptDao deptDao;
    
        @Override
        public List<Dept> getDeptList() {
            List<Dept> list = deptDao.getDeptList();
            return list;
        }
    }
    

    总结:springboot整合mybatis后配置小而且使用方便。

  • 相关阅读:
    Yum安装Lamp环境
    Cacti系统监控安装
    源码安装Memcache
    Lamp源码编译+SVN安装
    分页数据列表写法
    文件单位转换函数
    Session写入到Memcache,Redis和数据库中
    [LeetCode#30]Substring with Concatenation of All Words
    快速创建php server
    Git skills in reseting files
  • 原文地址:https://www.cnblogs.com/chenweichu/p/10041956.html
Copyright © 2011-2022 走看看