依赖
<?xml version="1.0" encoding="UTF-8"?> <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.example</groupId> <artifactId>demo1</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo1</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--启动时启动内置tomcat --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!--对Jsp支持 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- 支持jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.46</version> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!--mybatis分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> <!-- MySql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.22</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
domain
package pom.domain; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; /** * @Title: User.java * @Package pom.domain * @Description: TODO(用一句话描述该文件做什么) * @author licy * @date 2018年10月9日 * @version V1.0 */ @Data @NoArgsConstructor @AllArgsConstructor public class User { private String id; private String name; }
controller
package pom.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.github.pagehelper.PageInfo; import pom.domain.User; import pom.service.UserService; @RestController // 表示该controller类下所有的方法都公用的一级上下文根 @RequestMapping(value = "/springboot") public class UserController { @Autowired private UserService userService; // 这里使用@RequestMapping注解表示该方法对应的二级上下文路径 @RequestMapping(value = "/getUserByGet", method = RequestMethod.GET) String getUserByGet(@RequestParam(value = "id") String id) { User user = new User(); // user.setId(id); // User u = userService.getUserById(user); PageInfo pageInfo = userService.getUserList(user); System.out.println(pageInfo.getList().size()); return "Hello " + id; } }
service
package pom.service; import com.github.pagehelper.PageInfo; import pom.domain.User; /** * @Title: UserService.java * @Package pom.service * @Description: TODO(用一句话描述该文件做什么) * @author licy * @date 2018年10月9日 * @version V1.0 */ public interface UserService { User getUserById(User user); PageInfo getUserList(User user); }
serviceImpl
package pom.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import pom.dao.UserDao; import pom.domain.User; /** * @Title: UserServiceImpl.java * @Package pom.service * @Description: TODO(用一句话描述该文件做什么) * @author licy * @date 2018年10月9日 * @version V1.0 */ @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public User getUserById(User user) { return userDao.getUserById(user); } @Override public PageInfo getUserList(User user) { PageHelper.startPage(10, 10); List<User> result = userDao.getUserList(user); PageInfo<User> pageInfo = new PageInfo<User>(result); return pageInfo; } }
dao
package pom.dao; import java.util.List; import org.apache.ibatis.annotations.Mapper; import pom.domain.User; /** * @Title: UserDao.java * @Package pom.dao * @Description: TODO(用一句话描述该文件做什么) * @author licy * @date 2018年10月9日 * @version V1.0 */ @Mapper public interface UserDao { User getUserById(User user); List<User> getUserList(User user); }
UserMapper.xml:mapper目录下
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="pom.dao.UserDao"> <select id="getUserById" parameterType="pom.domain.User" resultType="pom.domain.User"> SELECT * FROM user WHERE ID = #{id} </select> <select id="getUserList" parameterType="pom.domain.User" resultType="pom.domain.User"> SELECT * FROM user </select> </mapper>
application.properties
#\u8BBE\u7F6ETomcat\u7AEF\u53E3\uFF0C\u9ED8\u8BA48080 server.port=8080 #\u8BBE\u7F6ETomcat\u7F16\u7801 server.tomcat.uri-encoding=UTF-8 #--------------------------------spring--------------------------- #\u8BBE\u7F6E\u89C6\u56FE\u89E3\u6790\u5668\u8DEF\u5F84 spring.mvc.view.prefix=/WEB-INF/views/ #\u8BBE\u7F6E\u89C6\u56FE\u89E3\u6790\u5668\u540E\u7F00 spring.mvc.view.suffix=.jsp #\u6570\u636E\u5E93\u914D\u7F6E spring.datasource.url=jdbc:mysql://localhost:3306/licy?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver #-----------------------------mybatis------------------------------- #\u914D\u7F6E.xml\u6587\u4EF6\u8DEF\u5F84 mybatis.mapper-locations=classpath:mapper/*.xml #\u914D\u7F6E\u6A21\u578B\u8DEF\u5F84 mybatis.type-aliases-package=pom.domain #-----------------------------pagehelper--------------------------- pagehelper.helperDialect=mysql pagehelper.reasonable=true pagehelper.support-methods-arguments=true pagehelper.params=count=countSql
访问地址:http://localhost:8080/springboot/getUserByGet?id=a