三、idea+maven+SMM简单实现单表查询或分页查询

1、在数据库中创建d_department表

CREATE TABLE `d_department` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `departmentCode` varchar(20) DEFAULT NULL COMMENT '所属部门编码',
  `departmentName` varchar(20) DEFAULT NULL COMMENT '所属部门名称',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

INSERT INTO `d_department` VALUES (NULL, 'xinlingchanpinbu', '信令产品部');
INSERT INTO `d_department` VALUES (NULL, 'yunyingbu', '运营部');

2、在mybatis目录下新建mybatis-config.xml文件,配置分页插件





	
		
		
	
	
	
		
	        
	    
	

3、在mybatis/mappers目录下创建DepartmentMapper.xml文件








    
    

4、修改spring目录下spring-mybatis.xml文件

之前是被我注释掉了,把注释去掉就可以了

    	
    
    
    

5、编写model包下创建实体类Department

public class Department {

    private int id;

    private String departmentCode;

    private String departmentName;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDepartmentCode() {
        return departmentCode;
    }

    public void setDepartmentCode(String departmentCode) {
        this.departmentCode = departmentCode;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
}

6、在dao包下创建接口DepartmentMapper(注意是interface不是class)

public interface DepartmentMapper {

    //查询所有
    public List selectAll();
}

7、在test包下创建编写测试类DepartmentServiceTest

三、idea+maven+SMM简单实现单表查询或分页查询_第1张图片

package com.fullmark.service;

import com.alibaba.fastjson.JSON;
import com.fullmark.dao.DepartmentMapper;
import com.fullmark.model.Department;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
//告诉junit spring的配置文件
@ContextConfiguration({"classpath:spring/spring-mybatis.xml"})
public class DepartmentServiceTest {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Test
    public void testSelectAll() throws Exception {
        List Departments = departmentMapper.selectAll();
        for (Department department : Departments) {
            System.err.println(department.getId() + "   " + department.getDepartmentCode()
                    + "  " + department.getDepartmentName());
        }
    }

    @Test
    public void testSelectByPage() throws Exception {
        PageHelper.startPage(1, 1);
        List Departments = departmentMapper.selectAll();
        PageInfo pageInfo = new PageInfo(Departments);
        JSON json = (JSON) JSON.toJSON(pageInfo);
            System.err.println(json.toJSONString());

    }


}

运行testSelectAll查询全部得到结果如下,说明在不使用分页插件的情况下,查询全部已经成功了。

1   xinlingchanpinbu  信令产品部
2   yunyingbu  运营部
3   xin  信令产品部

运行testSelectByPage得到分页数据如下,说明使用PageHelper分页插件成功

{"total":3,"lastPage":3,"nextPage":2,"hasNextPage":true,"pages":3,"pageSize":1,"navigatePages":8,"list":[{"id":1,"departmentCode":"xinlingchanpinbu","departmentName":"信令产品部"}],"hasPreviousPage":false,"navigatepageNums":[1,2,3],"size":1,"pageNum":1,"prePage":0,"endRow":1,"isLastPage":false,"startRow":1,"firstPage":1,"isFirstPage":true}

 

 

你可能感兴趣的:(SSM)