操作步驟
(1)新建maven project;
(2)在pom.xml文件中引入相關(guān)依賴;
(3)創(chuàng)建啟動(dòng)類App.java
(4)在application.properties添加配置文件;
(5)編寫Demo測(cè)試類;
(6)編寫DemoMapper赏廓;
(7)編寫DemoService
(8)編寫DemoController;
(9)加入PageHelper
(10)獲取自增長ID;
(1)新建maven project;
新建一個(gè)maven project,取名為:spring-boot-mybatis
(2)在pom.xml文件中引入相關(guān)依賴
(1)基本依賴狠鸳,jdk版本號(hào);
(2)mysql驅(qū)動(dòng)留瞳,mybatis依賴包拒迅,mysql分頁P(yáng)ageHelper:
<!-- mysql 數(shù)據(jù)庫驅(qū)動(dòng). -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--
spring-boot mybatis依賴:
請(qǐng)不要使用1.0.0版本,因?yàn)檫€不支持?jǐn)r截器插件她倘,
1.1.1 是博主寫帖子時(shí)候的版本璧微,大家使用最新版本即可
-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--
MyBatis提供了攔截器接口,我們可以實(shí)現(xiàn)自己的攔截器硬梁,
將其作為一個(gè)plugin裝入到SqlSessionFactory中前硫。
Github上有位開發(fā)者寫了一個(gè)分頁插件,我覺得使用起來還可以荧止,挺方便的屹电。
Github項(xiàng)目地址: https://github.com/pagehelper/Mybatis-PageHelper
-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
(3)創(chuàng)建啟動(dòng)類App.java
@SpringBootApplication
@MapperScan("com.kfit.*.mapper")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
//這里和以往不一樣的地方就是MapperScan的注解,這個(gè)是會(huì)掃描該包下的接口
(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測(cè)試類
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<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);
}
(7)編寫DemoService
@Service
public class DemoService {
@Autowired
private DemoMappper demoMappper;
public List<Demo> likeName(String name){
return demoMappper.likeName(name);
}
}
(8)編寫DemoController
@RestController
public class DemoController {
@Autowired
private DemoService demoService;
@RequestMapping("/likeName")
public List<Demo> likeName(String name){
return demoService.likeName(name);
}
}
//運(yùn)行訪問:http://127.0.0.1:8080/likeName?name=張三 就可以看到返回的數(shù)據(jù)了
(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<Demo> 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")