1. 创建maven项目 springbootdemo

2. 配置pom.xml,导入jar包

  1. <properties>
  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3. <java.version>1.8</java.version>
  4. <mybatis_spring_boot.version>1.3.2</mybatis_spring_boot.version>
  5. </properties>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>1.4.0.RELEASE</version>
  10. </parent>
  11. <dependencies>
  12. <!-- 添加 web 应用的依赖 -->
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-web</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.mybatis.spring.boot</groupId>
  19. <artifactId>mybatis-spring-boot-starter</artifactId>
  20. <version>${mybatis_spring_boot.version}</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>mysql</groupId>
  24. <artifactId>mysql-connector-java</artifactId>
  25. </dependency>
  26. <!-- 分页插件 -->
  27. <dependency>
  28. <groupId>com.github.pagehelper</groupId>
  29. <artifactId>pagehelper-spring-boot-starter</artifactId>
  30. <version>1.1.2</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>com.alibaba</groupId>
  34. <artifactId>fastjson</artifactId>
  35. <version>1.2.31</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-test</artifactId>
  40. <scope>test</scope>
  41. </dependency>
  42. <!-- alibaba的druid数据库连接池 -->
  43. <dependency>
  44. <groupId>com.alibaba</groupId>
  45. <artifactId>druid-spring-boot-starter</artifactId>
  46. <version>1.1.1</version>
  47. </dependency>
  48. <!-- 热部署 -->
  49. <dependency>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-devtools</artifactId>
  52. <optional>true</optional>
  53. </dependency>
  54. <!-- 日志 -->
  55. <dependency>
  56. <groupId>org.springframework.boot</groupId>
  57. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  58. </dependency>
  59. </dependencies>
  60. <build>
  61. <!-- 将应用打包成一个可以运行的 jar -->
  62. <plugins>
  63. <plugin>
  64. <groupId>org.springframework.boot</groupId>
  65. <artifactId>spring-boot-maven-plugin</artifactId>
  66. </plugin>
  67. <plugin>
  68. <groupId>org.apache.maven.plugins</groupId>
  69. <artifactId>maven-compiler-plugin</artifactId>
  70. <configuration>
  71. <source>${java.version}</source>
  72. <target>${java.version}</target>
  73. </configuration>
  74. </plugin>
  75. </plugins>
  76. </build>

3. 配置application.properties 文件

logging.level.cn.mrdear.mapper=trace

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/my_db?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false
spring.datasource.username=root
spring.datasource.password=123456

spring.datasource.max-active=20      
spring.datasource.max-idle=8  
spring.datasource.min-idle=8  
spring.datasource.initial-size=10

mybatis.typeAliasesPackage=com.demo.model
mybatis.mapperLocations=classpath:com/demo/dao/mapper/*.xml

pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

server.port=11111

spring.devtools.restart.exclude=static/**,public/**

4. 创建 Appplication 启动类

  1. @SpringBootApplication
  2. @MapperScan(basePackages = { "com.demo.dao" })
  3. public class Application {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Application.class, args);
  6. }
  7. }

5. 创建实体类 User.java

  1. public class User {
  2. private Integer id;
  3. private String username;
  4. private String password;
  5. //setter and getter 自行补全
  6. }

6. 创建 dao 层

(1)创建UserDao 接口

public List<User> selectByPage();

(2)创建 UserMapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.demo.dao.UserDao">
  4. <resultMap id="BaseResultMap" type="com.demo.model.User">
  5. <result column="id" jdbcType="INTEGER" property="id" />
  6. <result column="username" jdbcType="VARCHAR" property="username" />
  7. <result column="password" jdbcType="VARCHAR" property="password" />
  8. </resultMap>
  9. <select id="selectByPage"
  10. resultType="com.demo.model.User">
  11. select
  12. <include refid="Base_Column_List" />
  13. from user
  14. </select>
  15. <sql id="Base_Column_List">
  16. id, username, password
  17. </sql>
  18. </mapper>

7. 创建 service层

(1)创建service 接口

public List<User> selectByPage(int pageNo, int pageSize);

(2)实现service接口

  1. @Service("userService")
  2. public class UserServiceImpl implements UserService {
  3. @Autowired
  4. private UserDao userDao;
  5. @Override
  6. public List<User> selectByPage(int pageNo, int pageSize) {
  7. PageHelper.startPage(pageNo, pageSize);
  8. return userDao.selectByPage();
  9. }
  10. }

8. 创建 Controller 层

  1. @RestController
  2. @RequestMapping("user")
  3. public class UserController {
  4. private static Logger logger = LoggerFactory.getLogger(UserController.class);
  5. @Autowired
  6. private UserService userService;
  7. @RequestMapping("selectByPage")
  8. public ModelMap selectByPage(Integer page, Integer rows) {
  9. logger.info("/user/selectByPage开始调用!");
  10. ModelMap model = new ModelMap();
  11. try {
  12. List<User> userList = userService.selectByPage(page, rows);
  13. // 获取分页查询后的数据
  14. PageInfo<User> pageInfo = new PageInfo<User>(userList);
  15. model.put("data", userList);
  16. // 总记录数
  17. model.put("total", pageInfo.getTotal());
  18. // 当前页
  19. model.put("page", pageInfo.getPageNum());
  20. } catch (Exception e) {
  21. logger.info("/user/selectByPage出现异常!!!");
  22. }
  23. return model;
  24. }
  25. }

9.  resources 目录下 创建 static 目录,用于存放静态资源


10. 运行项目,测试













相关发布