1、在pom文件中引入依赖
2、创建启动类
1 import org.mybatis.spring.annotation.MapperScan; 2 import org.springframework.boot.SpringApplication; 3 import org.springframework.boot.autoconfigure.SpringBootApplication; 4 5 /** 6 * Spring Boot启动类. 7 */ 8 @SpringBootApplication 9 @MapperScan("com.包名.*")//扫描:该包下相应的class,主要是MyBatis的持久化类.Mapper 10 public class App { 11 public static void main(String[] args) { 12 SpringApplication.run(App.class, args); 13 } 14 }
3、在application.properties添加配置文件
4、编写Demo实体类
5、编写DemoMapper接口
1 import java.util.List; 2 3 import org.apache.ibatis.annotations.Insert; 4 import org.apache.ibatis.annotations.Options; 5 import org.apache.ibatis.annotations.Select; 6 7 public interface DemoMappper { 8 9 //#{name}:参数占位符 10 @Select("select *from Demo where name=#{name}") 11 public List<Demo> likeName(String name); 12 13 14 @Select("select *from Demo where id = #{id}") 15 public Demo getById(long id); 16 17 @Select("select name from Demo where id = #{id}") 18 public String getNameById(long id); 19 20 21 /** 22 * 保存数据. 23 */ 24 @Insert("insert into Demo(name) values(#{name})") 25 @Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id") 26 public void save(Demo demo); 27 28 }
6、编写DemoService
1 import java.util.List; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Service; 5 import org.springframework.transaction.annotation.Transactional; 6 7 @Service 8 public class DemoService { 9 10 @Autowired 11 private DemoMappper demoMappper; 12 13 public List<Demo> likeName(String name){ 14 return demoMappper.likeName(name); 15 } 16 17 @Transactional//添加事务. 18 public void save(Demo demo){ 19 demoMappper.save(demo); 20 } 21 22 }
7、编写DemoController
1 import java.util.List; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RestController; 6 7 8 9 @RestController 10 public class DemoController { 11 12 @Autowired 13 private DemoService demoService; 14 15 @RequestMapping("/likeName") 16 public List<Demo> likeName(String name){ 17 // PageHelper.startPage(1, 2); 第一页,每页2条 18 return demoService.likeName(name); 19 } 20 21 @RequestMapping("/save") 22 public Demo save(){ 23 Demo demo = new Demo(); 24 demo.setName("张三"); 25 demoService.save(demo); 26 return demo; 27 } 28 29 }
8、添加PageHelpler--MyBatisConfiguration
1 import java.util.Properties; 2 3 import org.springframework.context.annotation.Bean; 4 import org.springframework.context.annotation.Configuration; 5 6 import com.github.pagehelper.PageHelper; 7 8 @Configuration 9 public class MyBatisConfiguration { 10 11 @Bean 12 public PageHelper pageHelper() { 13 System.out.println("MyBatisConfiguration.pageHelper()"); 14 PageHelper pageHelper = new PageHelper(); 15 Properties p = new Properties(); 16 p.setProperty("offsetAsPageNum", "true"); 17 p.setProperty("rowBoundsWithCount", "true"); 18 p.setProperty("reasonable", "true"); 19 pageHelper.setProperties(p); 20 return pageHelper; 21 } 22 }
在Controller中加入PageHelpler
9、获取自增长ID
在Mapper中使用@Options配置返回的主键信息;
@Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id")
useGeneratedKeys=true 开启主键自增
keyProperty="id" 对应实体类属性
keyColumn="id" 对应数据库字段
----------------------------------------------------------------------------------------------------------------
application.properties配置文件
1 ######################################################## 2 ###datasource -- mysql的数据库配置. 3 ######################################################## 4 spring.datasource.url = jdbc:mysql://localhost:3306/test 5 spring.datasource.username = root 6 spring.datasource.password = root 7 spring.datasource.driverClassName = com.mysql.jdbc.Driver 8 spring.datasource.max-active=20 9 spring.datasource.max-idle=8 10 spring.datasource.min-idle=8 11 spring.datasource.initial-size=10
pom.xml文件
(1)基本依赖,jdk版本号;
(2)mysql驱动,mybatis依赖包,mysql分页PageHelper:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 5 <groupId>com.kfit</groupId> 6 <artifactId>spring-boot-mybatis</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <packaging>jar</packaging> 9 10 <name>spring-boot-mybatis</name> 11 <url>http://maven.apache.org</url> 12 13 <!-- spring boot parent节点,引入这个之后,在下面和spring boot相关的就不需要引入版本了; --> 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>1.4.1.RELEASE</version> 18 </parent> 19 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 --> 24 <java.version>1.8</java.version> 25 </properties> 26 27 <dependencies> 28 29 <!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ --> 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-web</artifactId> 33 </dependency> 34 35 <!-- mysql 数据库驱动. --> 36 <dependency> 37 <groupId>mysql</groupId> 38 <artifactId>mysql-connector-java</artifactId> 39 </dependency> 40 41 <!-- 42 spring-boot mybatis依赖: 43 44 请不要使用1.0.0版本,因为还不支持拦截器插件, 45 1.1.1 是博主写帖子时候的版本,大家使用最新版本即可 46 --> 47 <dependency> 48 <groupId>org.mybatis.spring.boot</groupId> 49 <artifactId>mybatis-spring-boot-starter</artifactId> 50 <version>1.1.1</version> 51 </dependency> 52 53 54 <!-- 55 MyBatis提供了拦截器接口,我们可以实现自己的拦截器, 56 将其作为一个plugin装入到SqlSessionFactory中。 57 Github上有位开发者写了一个分页插件,我觉得使用起来还可以,挺方便的。 58 Github项目地址: https://github.com/pagehelper/Mybatis-PageHelper 59 --> 60 <dependency> 61 <groupId>com.github.pagehelper</groupId> 62 <artifactId>pagehelper</artifactId> 63 <version>4.1.0</version> 64 </dependency> 65 66 67 68 69 </dependencies> 70 </project>
来源:https://www.cnblogs.com/onroad2019/p/11430674.html