Spring Boot入门——集成Mybatis
步骤:
1、新建maven项目
2、在pom.xml文件中引入相关依赖
<!-- mysql依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.42</version> </dependency>
<!-- mybatis依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>
3、在application.properties文件中添加配置信息
#mysql数据库配置 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver
4、编写Grade类
package com.wyl.bean; public class Grade { private int id; private String gradeNm; private int teacherId; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getGradeNm() { return gradeNm; } public void setGradeNm(String gradeNm) { this.gradeNm = gradeNm; } public int getTeacherId() { return teacherId; } public void setTeacherId(int teacherId) { this.teacherId = teacherId; } }
5、编写mapper
import java.util.List; import org.apache.ibatis.annotations.Select; import com.wyl.bean.Grade; public interface GradeMapper { @Select("select * from grade where grade_nm=#{name}") public List<Grade> getByGradeNm(String name);
@Insert("insert into grade(grade_nm,teacher_id) values(#{gradeNm},#{teacherId})")
@Options(useGeneratedKeys=true,keyColumn="id",keyProperty="id")//设置id自增长
public void save(Grade grade); }
6、编写service
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.wyl.bean.Grade; import com.wyl.dao.GradeMapper; @Service public class GradeService { @Autowired private GradeMapper gradeMapper; public List<Grade> getByGradeNm(String name){ return gradeMapper.getByGradeNm(name); } }
7、编写Controller
import java.util.List; import javax.annotation.Resource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.wyl.bean.Grade; import com.wyl.service.GradeService; @RestController public class GradeController { @Resource private GradeService gradeService; @RequestMapping("/getByGradeNm") public List<Grade> getByGradeNm(String name){ return gradeService.getByGradeNm(name); } }
8、创建启动类App.java,添加自动扫描mapper
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Hello world! * */ @SpringBootApplication @MapperScan("com.wyl.dao")//告诉mapper所在的包名 public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); SpringApplication.run(App.class, args); } }
5、测试结果
在上述结果中,输出的gradeNm字段的值为空,是由于设置接收的属性为name,可以在mapper中设置属性和数据库中字段的匹配
public interface GradeMapper { @Select("select * from grade where grade_nm=#{name}") @Results({ @Result(column="id", property="id"), @Result(column="grade_nm", property="gradeNm"), @Result(column="teacher_id", property="teacherId") }) public List<Grade> getByGradeNm(String name); }
之后结果为:
9、使用PageHelper分页插件
a、引入springboot自带的分页插件
<!-- 引入spring boot自带的pagehelper插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>
@RequestMapping("/getByGradeNm2") public List<Grade> getByGradeNm2(String name){ PageHelper.startPage(1,2); //显示第一页2条数据 return gradeService.getByGradeNm(name); }
b、引入外部分页插件
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.2</version> </dependency>
新建配置类,配置pagehelper的属性
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(){ PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); properties.setProperty("offsetAsPageNum","true"); properties.setProperty("rowBoundsWithCount","true"); properties.setProperty("reasonable","true"); pageHelper.setProperties(properties); return pageHelper; } }
@RequestMapping("/getByGradeNm2") public List<Grade> getByGradeNm2(String name){ PageHelper.startPage(1,2); //显示第一页2条数据 return gradeService.getByGradeNm(name); }