原生的mybatis需要手写sql语句,项目数据库表多了之后,可以让你写sql语句写到手软,于是mybatis官方提供了mybatis-generator:mybatis逆向工程代码生成工具,用于简化mybatis单表操作。
在PageHelper3.几的版本的时候,使用它对逆向工程生成的查询方法进行分页时出现失效的情况,而PageHelper4开始,亲测能够与mybatis逆向工程生成的方法完美兼容,今天就和大家分享spring+mybatis+PageHelper的一个整合小案例
4.12
4.3.10.RELEASE
3.4.1
1.3.0
5.1.32
1.6.4
2.8.8
1.0.9
4.3.5
1.2
2.5
2.0
2.5
3.3.2
1.3.2
3.3
4.1.0
0.9.1
1.3.1
2.7.2
4.10.3
2.5.3
3.4.7
0.1
5.11.2
2.3.23
2.2.2
joda-time
joda-time
${joda-time.version}
org.apache.commons
commons-lang3
${commons-lang3.version}
org.apache.commons
commons-io
${commons-io.version}
commons-net
commons-net
${commons-net.version}
com.fasterxml.jackson.core
jackson-databind
${jackson.version}
org.apache.httpcomponents
httpclient
${httpclient.version}
org.quartz-scheduler
quartz
${quartz.version}
junit
junit
${junit.version}
test
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
${mybatis.spring.version}
com.github.pagehelper
pagehelper
${pagehelper.version}
mysql
mysql-connector-java
${mysql.version}
com.alibaba
druid
${druid.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-jms
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
provided
org.apache.logging.log4j
log4j-api
2.7
org.apache.logging.log4j
log4j-core
2.7
org.apache.logging.log4j
log4j-web
2.7
jstl
jstl
${jstl.version}
javax.servlet
servlet-api
${servlet-api.version}
provided
javax.servlet
jsp-api
${jsp-api.version}
provided
commons-fileupload
commons-fileupload
${commons-fileupload.version}
redis.clients
jedis
${jedis.version}
org.apache.solr
solr-solrj
${solrj.version}
com.alibaba
dubbo
${dubbo.version}
org.apache.zookeeper
zookeeper
${zookeeper.version}
com.github.sgroschupf
zkclient
${zkclient.version}
org.apache.activemq
activemq-all
${activemq.version}
org.freemarker
freemarker
${freemarker.version}
org.apache.maven.plugins
maven-resources-plugin
2.7
UTF-8
org.apache.maven.plugins
maven-compiler-plugin
3.2
1.7
UTF-8
org.apache.tomcat.maven
tomcat7-maven-plugin
8080
/
D:/log4j2/mybatis/genertor/logs/
error
注意:这里为了方便阅读及后期扩展,将配置文件进行了分模块拆分,另外,正式的项目应该要加上事务的相关配置,这里不做演示
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/stu?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
Mybatis-generator
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
characterEncoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncoding
/*
org.apache.logging.log4j.web.Log4jServletContextListener
log4jServletFilter
org.apache.logging.log4j.web.Log4jServletFilter
log4jServletFilter
/*
REQUEST
FORWARD
INCLUDE
ERROR
httpPutFormFilter
org.springframework.web.filter.HttpPutFormContentFilter
httpPutFormFilter
/*
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:spring/applicationContext*.xml
springmvc
org.springframework.web.servlet.DispatcherServlet
1
contextConfigLocation
classpath:spring/springmvc.xml
springmvc
/
package com.stu.service;
import java.util.List;
import com.stu.pojo.TUser;
public interface UserService {
public List getUserInfo( String keywords, Integer page, Integer size);
}
package com.stu.service.impl;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stu.dao.TUserMapper;
import com.stu.pojo.TUser;
import com.stu.pojo.TUserExample;
import com.stu.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private TUserMapper userMapper;
@Override
public List getUserInfo(String keywords, Integer page, Integer size) {
PageHelper.startPage(page, size);
TUserExample example = new TUserExample();
if (!StringUtils.isBlank(keywords)) {
example.createCriteria().andUsernameLike(keywords + "%");
}
List list = userMapper.selectByExample(example);
PageInfo info = new PageInfo(list);
System.out.println("页码: " + info.getPageNum());
System.out.println("每页大小: " + info.getSize());
System.out.println("总页数: " + info.getPages());
System.out.println("总记录数: " + info.getTotal());
return list;
}
}
注意:keywords是查询条件,对应表字段---->username
package com.stu.api.controller;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.stu.pojo.TUser;
import com.stu.service.UserService;
@RestController
@RequestMapping("/api/v2/users")
public class UserController {
@Resource
private UserService userService;
@GetMapping("/")
public List getUserInfo( String keywords ,Integer page, Integer size){
System.out.println("page : " + page);
System.out.println("size : " + size);
if(keywords != null){
try {
keywords=new String(keywords.getBytes("ISO-8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
List user = userService.getUserInfo(keywords , page, size);
return user;
}
}
{"data":[{"id":1,"username":"王五","password":null,"state":0,"isdel":false,"addTime":1487644664000,"money":null,"leftMoney":22.1,"remark":null},{"id":2,"username":"张三","password":null,"state":0,"isdel":false,"addTime":1487644811000,"money":null,"leftMoney":22.1,"remark":null}],"meta":{"respCode":0,"respMsg":null,"detail":"OK","url":null},"pagination":{"page":1,"size":2,"totalPage":58,"totalCode":116}}