三、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文件,配置分页插件

<?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>

	<settings>
		<!-- 开启驼峰自动映射 -->
		<setting name="mapUnderscoreToCamelCase" value="true" />
	</settings>
	
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageHelper">
	        <property name="dialect" value="mysql"/>
	    </plugin>
	</plugins>
</configuration>

3、在mybatis/mappers目录下创建DepartmentMapper.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" >

<!-- 配置SQL语句,与实体类操作的对应关系。 -->
<!-- 保证唯一性 -->
<mapper namespace="com.fullmark.dao.DepartmentMapper">

    <!--
       将返回的数据  与对象对应。将返回的数据,按照配置组装成对应的对象。
     -->
    <select id="selectAll"  resultType="com.fullmark.model.Department">
        select id,departmentCode,departmentName from d_department
    </select>

</mapper>

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

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

    <!-- 加载mybatis全局配置文件 -->	
    <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
    <!-- 扫描 mappers目录以及子目录下所有的xml文件 -->
    <property name="mapperLocations" value="classpath:mybatis/mappers/**/*.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<Department> selectAll();
}

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

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<Department> 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<Department> Departments = departmentMapper.selectAll();
        PageInfo<Department> pageInfo = new PageInfo<Department>(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}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值