-
条件
-
使用Spring boot、mybatis、mysql实现以下功能
-
用户列表
-
用户增加
-
信息修改
-
信息查看
-
用户搜索
-
分页
ps: 页面引擎使用Spring boot推荐的thymeleaf引擎
-
效果图如下
-
步骤
-
使用IDEA的Spring Initializr构建
-
模板选择web、Thymeleaf、MyBatis、MySQL(以上步骤都不懂的自行百度)
-
在src下创建以下包名,目录如下:
src
main
java
com.xxx.xxx
controller
mapper
pojo
tools
resources
templates
-
修改resources目录下的application.properties
#thymeleaf 配置 spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.servlet.content-type=text/html #缓存设置为false, 这样修改之后马上生效,便于调试 spring.thymeleaf.cache=false #数据库配置 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/zx?useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true spring.datasource.username=root spring.datasource.password=root # Mysql8.0请改为com.mysql.cj.jdbc.Driver spring.datasource.driver-class-name=com.mysql.jdbc.Driver
-
在pom.xml的<dependencies>中加入以下配置
<!-- servlet依赖. --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- tomcat的支持.--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- pageHelper 用于分页 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency>
-
在pojo里添加实体类
public class Users { private int id; private String mobName; private String userName; ... }
-
在mapper里添加注解方式数据库操作接口
@Mapper public interface UsersMapper { @Select("select * from users where CONCAT(mobName,userName,nickname,phone) like CONCAT('%',#{name},'%')") List<Users> getUsersList(String name); @Insert("insert into users (id,mobName,userName,nickname,phone,register) " + "values (#{id},#{mobName},#{userName},#{nickname},#{phone},#{register})") public int add(Users users); @Delete(" delete from users where id= #{id} ") public void del(int id); @Select("select * from users where id= #{id} ") public Users getUser(int id); @Update("update users set " + "mobName = #{mobName}," + "userName = #{userName}," + "nickname = #{nickname}," + "phone = #{phone}," + "register = #{register} " + "where id=#{id} ") public int update(Users users); }
-
在tools里添加分页工具类
import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class PageHelperConfig {
@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;
}
}
-
添加controller
@Controller
public class UserController {
@Resource
private UsersMapper usersMapper;
//添加页面
@RequestMapping("add")
public String add() {
return "add";
}
//查找(用于查询)
@RequestMapping("getUser")
public String getUser(int id, Model model) throws Exception {
Users Users = usersMapper.getUser(id);
model.addAttribute("Users", Users);
return "userShow";
}
...
}
-
在templates添加.html页面
-
具体代码请看
-
PageHelper详解
PageHelper.startPage(int PageNum,int PageSize): 用来设置页面的位置和展示的数据条目数;
PageInfo<xxx> pageInfo = new PageInfo<xxx>(list); PageInfo用来封装页面信息,返回给前台界面
pageHelper中一些常见的参数:
PageInfo.list | 结果集 |
---|---|
PageInfo.pageNum | 当前页码 |
PageInfo.pageSize | 当前页面显示的数据条目 |
PageInfo.pages | 总页数 |
PageInfo.total | 数据的总条目数 |
PageInfo.prePage | 上一页 |
PageInfo.nextPage | 下一页 |
PageInfo.isFirstPage | 是否为第一页 |
PageInfo.isLastPage | 是否为最后一页 |
PageInfo.hasPreviousPage | 是否有上一页 |
PageHelper.hasNextPage | 是否有下一页 |