15.springboot:集成MyBatis

15.springboot:集成MyBatis_第1张图片
Paste_Image.png

操作步骤

(1)新建maven project;
(2)在pom.xml文件中引入相关依赖;
(3)创建启动类App.java
(4)在application.properties添加配置文件;
(5)编写Demo测试类;
(6)编写DemoMapper;
(7)编写DemoService
(8)编写DemoController;
(9)加入PageHelper
(10)获取自增长ID;

(1)新建maven project;

新建一个maven project,取名为:spring-boot-mybatis

(2)在pom.xml文件中引入相关依赖

(1)基本依赖,jdk版本号;
(2)mysql驱动,mybatis依赖包,mysql分页PageHelper:


    
        mysql
        mysql-connector-java
   

    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        1.1.1
    
        
    
        com.github.pagehelper
        pagehelper
        4.1.0
       

(3)创建启动类App.java

@SpringBootApplication
@MapperScan("com.kfit.*.mapper")
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

//这里和以往不一样的地方就是MapperScan的注解,这个是会扫描该包下的接口

(4)在application.properties添加配置文件

########################################################
###datasource
########################################################
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10

(5)编写Demo测试类

public class Demo {
    private long id;
    private String name;
      //省略getter and setter….
}

(6)编写DemoMapper

public interface DemoMappper {
    
    @Select("select *from Demo where name = #{name}")
    public List 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);
}

(7)编写DemoService

@Service
public class DemoService {
    @Autowired
    private DemoMappper demoMappper;
    
    public List likeName(String name){
        return demoMappper.likeName(name);
    }
}

(8)编写DemoController

@RestController
public class DemoController {
    @Autowired
    private DemoService demoService;
    
    @RequestMapping("/likeName")
    public List likeName(String name){
        return demoService.likeName(name);
    }
    
}

//运行访问:http://127.0.0.1:8080/likeName?name=张三  就可以看到返回的数据了

(9)加入PageHelper

@Configuration
public class MyBatisConfiguration {
    
    @Bean
    public PageHelper pageHelper() {
        System.out.println("MyBatisConfiguration.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;
    }
}
@RequestMapping("/likeName")
public List likeName(String name){
         PageHelper.startPage(1,1);
         return demoService.likeName(name);
}

(10)获取自增长ID

@Insert("insert into Demo(name,password) values(#{name},#{password})")
public long save(Demo name);
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") 

你可能感兴趣的:(15.springboot:集成MyBatis)