Spring Boot集成mybatis
2018-02-17 15:56
316 查看
1.新建maven project
2.在pom.xml文件中引入相关依赖
创建启动类App.java
在application.properties添加配置文件
编写Demo测试类
编写DemoMapper
编写DemoService
编写DemoController
加入PageHelper
加入PageHelper
获取自增长ID
2.在pom.xml文件中引入相关依赖
(1)基本依赖,jdk版本号; (2)mysql驱动,mybatis依赖包,mysql分页PageHelper: <!-- mysql 数据库驱动. --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- spring-boot mybatis依赖: 请不要使用1.0.0版本,因为还不支持拦截器插件, 1.1.1 是博主写帖子时候的版本,大家使用最新版本即可 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- MyBatis提供了拦截器接口,我们可以实现自己的拦截器, 将其作为一个plugin装入到SqlSessionFactory中。 Github上有位开发者写了一个分页插件 Github项目地址: https://github.com/pagehelper/Mybatis-PageHelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.0</version> </dependency>
创建启动类App.java
@SpringBootApplication @MapperScan("com.*.mapper") public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } } //这里和以往不一样的地方就是MapperScan的注解,这个是会扫描该包下的接口
在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
编写Demo测试类
public class Demo { private long id; private String name; //省略getter and setter…. }
编写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); }
编写DemoService
@Service public class DemoService { @Autowired private DemoMappper demoMappper; public List<Demo> likeName(String name){ return demoMappper.likeName(name); } }
编写DemoController
@RestController public class DemoController { @Autowired private DemoService demoService; @RequestMapping("/likeName") public List<Demo> likeName(String name){ return demoService.likeName(name); } }
加入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; } }
加入PageHelper
@RequestMapping("/likeName") public List<Demo> likeName(String name){ PageHelper.startPage(1,1); return demoService.likeName(name); }
获取自增长ID
@Insert("insert into Demo(name,password) values(#{name},#{password})") public long save(Demo name); @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
相关文章推荐
- spring boot集成mybatis,启动报无法创建dataSource问题
- SpringBoot集成MyBatis的分页插件PageHelper(回头草)
- SpringBoot学习笔记(4) Spring Boot 集成 Mybatis
- spring boot +Thymeleaf+mybatis 集成通用PageHelper,做分页
- SpringBoot集成Mybatis
- SpringBoot:spring boot集成mybatis
- 从零学spring boot--集成mybatis--项目--使用注解方式
- SpringBoot入门-17(springboot集成mybatis注解形式增删查改properties配置)
- Springboot 集成mybatis并用Logback并打印sql
- Spring Boot 集成 MyBatis
- springboot 集成mybatis Invalid bound statement (not found)异常
- Spring Boot集成MyBatis开发Web项目
- Spring Boot 数据访问集成 MyBatis 与事物配置
- SpringBoot集成Mybatis(基于注解式开发)
- SpringBoot集成MyBatis开启事务
- SpringBoot入门-20(springboot集成mybatis注解形式properties配置,利用@Provider实现动态SQL)
- springboot 集成mybatis
- Spring Boot集成MyBatis
- Spring Boot教程 - Spring Boot集成Mybatis(注解配置)