环境-版本
jdk 1.8.0_152
maven 3.5.2
mysql 5.5.40

1.选择Spring Initializr。选择后点击Next

2.把项目信息写好,点击Next;

3.选择web勾选设置
选择template勾选设置
选择SQL勾选设置

4.选择finish

5.配置maven


6.搭建

7.需要加入的代码如下:
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<build>
<plugins>
<!--自动生成代码-->
<plugin>
<!--Generator插件依赖-->
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!--MyBaits-generator的配置文件generatorConfig.xml的位置-->
<configurationFile>src/test/resources/config/generatorConfig.xml</configurationFile>
<!--是否覆盖同名文件(只是针对XML文件,java文件生成类似*.java.1、*.java.2形式的文件)-->
<overwrite>true</overwrite>
<!--是否将生成过程输出至控制台-->
<verbose>true</verbose>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
8.application.properties代码如下:

#配置端口
server.port=8080
#外网地址
spring.datasource.url=jdbc:mysql://localhost:8080?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.initialSize=20
spring.datasource.minIdle=20
spring.datasource.maxActive=200
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
#配置这句话,控制台输出sql语句
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis.mapper-locations= classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.springbootdemo.model
9.index页面创建



10.首先建用户表

10.mybatis配置
1.在resources文件夹下建立config文件夹,并创建mybatis-config.xml,代码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.example.yvans.model"/>
</typeAliases>
</configuration>
2.在resources文件夹下建立mapper文件夹,并创建UserMapper.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.example.yvans.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.yvans.model.User" >
<id column="id" jdbcType="BIGINT" property="id" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="phone" jdbcType="VARCHAR" property="phone" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="role_id" jdbcType="BIGINT" property="roleId" />
<result column="locked" jdbcType="TIMESTAMP" property="locked" />
</resultMap>
<select id="selectUsers" resultMap="BaseResultMap">
select * from sys_user
</select>
</mapper>
3.application.properties配置文件代码如下:
#配置端口
server.port=8080
#外网地址
#spring.datasource.url=jdbc:mysql://120.27.21.82:8036/qian?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=utf-8
#spring.datasource.username=root
#spring.datasource.password=123456
#spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://120.27.21.82:8036/qian?characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=qyw5203344
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver
spring.datasource.initialSize=20
spring.datasource.minIdle=20
spring.datasource.maxActive=200
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
mybatis.config-location=classpath:config/mybatis-config.xml
mybatis.mapper-locations= classpath:mapper/*.xml
在pom.xml文件内将依赖改成compile就行

11.首先创建实体类
在com.example.yvans下建立model包,并在该包下建立User类
package com.example.yvans.model;
public class User {
private int id;
private String username;
private String password;
private String name;
private String phone;
private String email;
private int roleId;
private String locked;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getRoleId() {
return roleId;
}
public void setRoleId(int roleId) {
this.roleId = roleId;
}
public String getLocked() {
return locked;
}
public void setLocked(String locked) {
this.locked = locked;
}
}
12.mapper
在com.example.yvans下建立mapper包,并在该包下建立UserMapper接口
public interface UserMapper {
int insert(User user);
List<User> selectUsers();
}
13.Service层,这里是实现层还有一个接口层不写了
package com.example.yvans.service.impl;
import com.example.yvans.mapper.UserMapper;
import com.example.yvans.model.User;
import com.example.yvans.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public int addUser(User user) {
int insert = userMapper.insert(user);
return insert;
}
/*
* 这个方法中用到了我们开头配置依赖的分页插件pagehelper
* 很简单,只需要在service层传入参数,然后将参数传递给一个插件的一个静态方法即可;
* pageNum 开始页数
* pageSize 每页显示的数据条数
* */
@Override
public PageInfo<User> findAllUser(int pageNum, int pageSize) {
//将参数传给这个方法就可以实现物理分页了,非常简单。
PageHelper.startPage(pageNum, pageSize);
List<User> userDomains = userMapper.selectUsers();
PageInfo result = new PageInfo(userDomains);
return result;
}
}
14.controller控制器
在com.example.yvans下建立controller包,并在该包下建立UserController类
package com.example.yvans.controller;
import com.example.yvans.model.User;
import com.example.yvans.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@PostMapping("/add")
public int addUser(User user) {
return userService.addUser(user);
}
@ResponseBody
@GetMapping("/all")
public Object findAllUser(@RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "10") int pageSize) {
return userService.findAllUser(pageNum, pageSize);
}
}
15.启动类
@SpringBootApplication
@MapperScan("com.example.yvans.mapper")
public class YvansApplication {
public static void main(String[] args) {
SpringApplication.run(YvansApplication.class, args);
}
}
结构:

请求结果:

网友评论