一、 在pom.xml文件中添加mybatis依赖,mysql数据库依赖
<!-- 添加mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
<!-- 数据库依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
若用到jsp页面,则需要添加jsp依赖:
<!-- 添加jsp依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>8.5.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
二、 application.properties(放在src/main/resources包下)属性配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/student
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
三、 po类
在com.huawei.po包下创建Stu类,如下:
package com.huawei.po;
public class Stu {
private int stu_id;
private String stu_name;
private int cla_id;
public Stu() {
super();
}
public Stu(int stu_id, String stu_name, int cla_id) {
super();
this.stu_id = stu_id;
this.stu_name = stu_name;
this.cla_id = cla_id;
}
public int getStu_id() {
return stu_id;
}
public void setStu_id(int stu_id) {
this.stu_id = stu_id;
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public int getCla_id() {
return cla_id;
}
public void setCla_id(int cla_id) {
this.cla_id = cla_id;
}
}
四、 mapper接口
在com.huawei.mapper包下创建StuMapper.java和StuMapper.xml文件,如下:
StuMapper.java
package com.huawei.mapper;
import java.util.List;
import com.huawei.po.Stu;
public interface StuMapper {
//查询所有学生
public List<Stu> findAllStu();
}
StuMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.huawei.mapper.StuMapper">
<select id="findAllStu" resultType="com.huawei.po.Stu">
select * from stu
</select>
</mapper>
五、 controller
HelloController:
package com.huawei.controller;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageHelper;
import com.huawei.mapper.StuMapper;
import com.huawei.po.Stu;
@RestController
public class HelloController {
@Autowired
private JdbcTemplate jdbcT;
@Autowired
private StuMapper stuMapper;
@RequestMapping("/hello")
public String hello(){
return "hello";
}
//查看数据库连接
@RequestMapping("/showDB")
public String showDB(){
System.out.println("进来了....");
String sql="select * from stu";
List<Map<String, Object>> mList=jdbcT.queryForList(sql);
return mList.toString();
}
//查询所有学生
@RequestMapping("/findAllStu")
public List<Stu> findAllStu(int pageNum){
System.out.println("pageNum:"+pageNum);
PageHelper.startPage(pageNum, 5);
List<Stu> sList=stuMapper.findAllStu();
return sList;
}
}
Ps.整合pagehelper
(1)添加依赖
<!-- 添加pagehelper分页依赖 -->
<dependency>
<groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.2</version>
</dependency>
(2)application.properties属性配置
#pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
(3)用例
//查询所有学生
@RequestMapping("/findAllStu")
public List<Stu> findAllStu(int pageNum){
System.out.println("pageNum:"+pageNum);
PageHelper.startPage(pageNum, 5);
List<Stu> sList=stuMapper.findAllStu();
return sList;
}
在查询之前先调用PageHelper.startPage(pageNum,pageSize)这个方法,即可自动完成分页查询。