springboot-mybatis Demo

@SpringBootApplication
@MapperScan("halo.*")//扫描:该包下相应的class,主要是MyBatis的持久化类.
public class App {
   public static void main(String[] args) {
      SpringApplication.run(App.class, args);
   }
}
public interface DemoMappper {
   
   //#{name}:参数占位符
   @Select("select *from Demo where name=#{name}")
   public List<Demo> likeName(String name);
   
   
   @Select("select *from Demo where id = #{id}")
   public Demo getById(long id);
   
   @Select("select name from Demo where id = #{id}")
   public String getNameById(long id);

   
   /**
    * 保存数据.
    * useGeneratedKeys 自增key
    * keyProperty class 主键属性
    * keyColumn db 主键列
    */
   @Insert("insert into Demo(name) values(#{name})")
   
   @Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id")
   public void save(Demo demo);
   
}
@Getter
@Setter
public class Demo {
   private long id;
   private String name;
   
}
// mybatis 分页工具
@Configuration
public class MyBatisConfiguration {
   
   @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        p.setProperty("offsetAsPageNum", "true");
        p.setProperty("rowBoundsWithCount", "true");
        p.setProperty("reasonable", "true");
        pageHelper.setProperties(p);
        return pageHelper;
    }
}
@RestController
public class DemoController {
   
   @Autowired
   private DemoService demoService;
   
   @RequestMapping("/likeName")
   public List<Demo> likeName(String name){
      /*
       * 第一个参数:第几页;
       * 第二个参数:每页获取的条数.
       */
      PageHelper.startPage(1, 2);
      return demoService.likeName(name);
   }

猜你喜欢

转载自my.oschina.net/haloooooo/blog/1824851