spring-boot5代码
App.java
package com.kfit.spring_boot_mybatis; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Spring Boot启动类. */ @SpringBootApplication @MapperScan("com.kfit.*")//扫描:该包下相应的class,主要是MyBatis的持久化类. public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
Demo.java
package com.kfit.spring_boot_mybatis; //这里先要建表demo public class Demo { private long id; private String name; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
DemoController.java
package com.kfit.spring_boot_mybatis; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.github.pagehelper.PageHelper; @RestController public class DemoController { @Autowired private DemoService demoService; @RequestMapping("/likeName") public List<Demo> likeName(String name){ /* * 第一个参数:第几页; * 第二个参数:每页获取的条数. */ PageHelper.startPage(1, 2);//请求进行拦截,只返回前2条数据 return demoService.likeName(name); } @RequestMapping("/save") public Demo save(){ Demo demo = new Demo(); demo.setName("张三55"); demoService.save(demo); return demo; } }
DemoMappper.java
package com.kfit.spring_boot_mybatis; import java.util.List; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Select; 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); /** * 保存数据. */ @Insert("insert into Demo(name) values(#{name})") @Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id") //是否开启自增涨主键key,属性名字和对应的数据库的名字 public void save(Demo demo); }
DemoService.java
package com.kfit.spring_boot_mybatis; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service public class DemoService { @Autowired private DemoMappper demoMappper; public List<Demo> likeName(String name){ return demoMappper.likeName(name); } @Transactional//添加事务. public void save(Demo demo){ demoMappper.save(demo); } }
MyBatisConfiguration.java
package com.kfit.spring_boot_mybatis.config; import java.util.Properties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.github.pagehelper.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; } }
总结.txt
Spring Boot 集成Mybatis: --------------------------------------- 1、新建一个新的Maven Project; 2、需要在pom.xml文件添加相应的依赖, 比如:mysql驱动; PageHelper分页插件,需要版本号; 3、编写启动类,大部分和之前的代码是一样的,需要的注意的是: 需要添加一个注解@MapperScan --指定MyBatis持久类的位置; 4、编写一个测试的实体类Demo; 5、编写一个DemoMapper,使用@Select和@Save进行数据库操作; 使用@Options配置返回的主键信息; 6、编写DemoService; 7、编写DemoController; 8、添加分页配置信息,需要添加一个MyBatisConfiguration; 9、使用PageHelper.startPage(pageNum,pageSize)进行分页;
application.properties
spring.datasource.url = jdbc:mysql://localhost:3306/spboot1 spring.datasource.username = root spring.datasource.password = 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
pom.xml
<!-- 添加依赖 --> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kfit</groupId> <artifactId>spring-boot-mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-mybatis</name> <url>http://maven.apache.org</url> <!-- spring boot parent节点,引入这个之后,在下面和spring boot相关的就不需要引入版本了; --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 --> <java.version>1.8</java.version> </properties> <dependencies> <!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 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> </dependencies> <build> <defaultGoal>compile</defaultGoal> </build> </project>