SSM02

1.mybatis逆向工程

2.多条件查询的实现

3.查询动态SQL

4.条件回显

5.分页功能

6.分页功能完善

 

一、mybatis逆向工程

1.简介

       mybatis是目前很流行的持久层框架,其逆向工程更是大大缩减了我们的开发时间。所谓mybatis逆向工程,就是mybatis会根据我们设计好的数据表,自动生成pojo、mapper以及mapper.xml

2.示例

1、在数据库中创建一张表

2、新建一个资源文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<!-- 配置生成器 -->
<generatorConfiguration>
    <context id="mysql" defaultModelType="hierarchical" targetRuntime="MyBatis3Simple">
        <property name="useActualColumnNames" value="true" />
        <!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表; 一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖 -->
        <property name="autoDelimitKeywords" value="false"/>
        <!-- 生成的Java文件的编码 -->
        <property name="javaFileEncoding" value="UTF-8"/>

        <!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; -->
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <!-- 注释生成器 -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!-- 必须要有的,使用这个配置链接数据库 @TODO:是否可以扩展 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql:///ssm" userId="root" password="root">
            <!-- 这里面可以设置property属性,每一个property属性都设置到配置的Driver上 -->
        </jdbcConnection>

        <!-- java模型创建器,是必须要的元素 负责:1,key类(见context的defaultModelType);2,java类;3,查询类
            targetPackage:生成的类要放的包,真实的包受enableSubPackages属性控制; targetProject:目标项目,指定一个存在的目录下,生成的内容会放到指定目录中,如果目录不存在,MBG不会自动建目录 -->
        <javaModelGenerator targetPackage="com.xj.bean" targetProject="src/main/java">
            <!-- for MyBatis3/MyBatis3Simple 自动为每一个生成的类创建一个构造方法,构造方法包含了所有的field;而不是使用setter; -->
            <property name="constructorBased" value="false"/>
            <!-- for MyBatis3 / MyBatis3Simple 是否创建一个不可变的类,如果为true, 那么MBG会创建一个没有setter方法的类,取而代之的是类似constructorBased的类 -->
            <property name="immutable" value="false"/>
        </javaModelGenerator>


        <!-- 生成SQL map的XML文件生成器, 注意,在Mybatis3之后,我们可以使用mapper.xml文件+Mapper接口(或者不用mapper接口),
            或者只使用Mapper接口+Annotation,所以,如果 javaClientGenerator配置中配置了需要生成XML的话,这个元素就必须配置
            targetPackage/targetProject:同javaModelGenerator -->
        <sqlMapGenerator targetPackage="com.xj.mapper"
                         targetProject="src/main/java">
            <!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false -->
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 对于mybatis来说,即生成Mapper接口,注意,如果没有配置该元素,那么默认不会生成Mapper接口 targetPackage/targetProject:同javaModelGenerator
            type:选择怎么生成mapper接口(在MyBatis3/MyBatis3Simple下):
            1,ANNOTATEDMAPPER:会生成使用Mapper接口+Annotation的方式创建(SQL生成在annotation中),不会生成对应的XML;
            2,MIXEDMAPPER:使用混合配置,会生成Mapper接口,并适当添加合适的Annotation,但是XML会生成在XML中;
            3,XMLMAPPER:会生成Mapper接口,接口完全依赖XML;
            注意,如果context是MyBatis3Simple:只支持ANNOTATEDMAPPER和XMLMAPPER -->
        <javaClientGenerator targetPackage="com.xj.mapper"
                             type="XMLMAPPER" targetProject="src/main/java">
            <!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false -->
            <property name="enableSubPackages" value="true"/>

            <!-- 可以为所有生成的接口添加一个父接口,但是MBG只负责生成,不负责检查 <property name="rootInterface"
                value=""/> -->
        </javaClientGenerator>

        <table tableName="employee" domainObjectName="Employee">
            <property name="useActualColumnNames" value="true" />
            <!-- 参考 javaModelGenerator 的 constructorBased属性 -->
            <property name="constructorBased" value="false"/>
            <generatedKey column="id" sqlStatement="JDBC"/>
        </table>
    </context>
</generatorConfiguration>

3、添加运行插件

<!--mybatis的generator插件-->
      <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.5</version>
        <configuration>
          <verbose>true</verbose>
          <!-- 代表mybatis generator生成的内容不要覆盖已有的内容 -->
          <overwrite>false</overwrite>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
          </dependency>
        </dependencies>
      </plugin>

4、运行逆向工程命令

5、生成的类和mapper

注:自动的生成的salary,不是Bigdecimal,而是Long类型

二、多条件查询的实现

1.在list.jsp的标题下面添加条件查询的搜索框

   <form action="/query.do" method="post">
        部门名称:<input type="text" name="deptName"/>
        部门地址:<input type="text" name="deptLocation"/>
        <input type="submit" value="搜索"/> <br>
    </form>

2.新建QqueryObject(查询对象)类

封装查询的字段

@Setter@Getter@ToString
public class QueryObject {
    private String deptName;//前台传过来的请求参数名
    private String deptLocation;
}

3.在service中添加业务方法

List<Department> query(QueryObject qo);
    //多条件查询
    @Override
    public List<Department> query(QueryObject qo) {
        return mapper.query(qo);
    }

4.在mapper接口中添加方法以及映射文件中添加对应的SQL语句

List<Department> query(QueryObject qo);
<select id="query" resultMap="departmentMap">
        select * from department where name like concat('%',#{deptName},'%') and location like concat('%',#{deptLocation},'%')
    </select>

注:concat拼接字符串(任意多个字符串)

5.在DepartmentController中添加query方法

    @RequestMapping("/query")
    public String query(QueryObject qo,Model model){
        //调用业务的方法执行查询
        List<Department> list = service.query(qo);
        model.addAttribute("list",list);
        return "department/list";
    }

三、查询动态SQL

当没有搜索的时候,列举出表格中所有的数据;按搜索条件动态查询

修改映射文件中query的SQL语句

    <select id="query" resultMap="departmentMap">
        select * from department
        <where>
            <if test="deptName!=null and deptName!=''">
                and name like concat('%',#{deptName},'%')
            </if>
            <if test="deptLocation!=null and deptLocation!=''">
                and location like concat('%',#{deptLocation},'%')
            </if>
        </where>
    </select>

四、条件回显

前面两步可以省略

1、重写QueryObject的get方法

    public String getDeptName() {
        /*if(deptName==null || deptName==""){
            return null;
        }else{
            return deptName;
        }*/
        return  StringUtils.isNullOrEmpty(this.deptName) ? null : this.deptName;
    }

    public String getDeptLocation() {
        return  StringUtils.isNullOrEmpty(this.deptLocation) ? null : this.deptLocation;
    }

2、修改映射文件中query的SQL语句

    <select id="query" resultMap="departmentMap">
        select * from department
        <where>
            <if test="deptName!=null">
                and name like concat('%',#{deptName},'%')
            </if>
            <if test="deptLocation!=null">
                and location like concat('%',#{deptLocation},'%')
            </if>
        </where>
    </select>

3、list.jsp搜索框后面添加value

   <form action="/query.do" method="post">
        部门名称:<input type="text" name="deptName" value="${queryObject.deptName}"/>
        部门地址:<input type="text" name="deptLocation"value="${queryObject.deptLocation}"/>
        <input type="submit" value="搜索"/> <br>
    </form>

五、分页功能

1、导包

  <!-- mybatis分页插件 -->
  <dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.4</version>
  </dependency>

2、在mybatis-config中配置一个拦截器

      <plugins>
            <!-- com.github.pagehelper为PageHelper类所在包名 -->
            <plugin interceptor="com.github.pagehelper.PageInterceptor">
            </plugin>
      </plugins>

3、在QueryObject中添加两个属性

    private Integer currentPage = 1;//默认值
    private Integer pageSize = 2;

4、service(Controller传过来的对象)

 public PageInfo query2(QueryObject qo);
    //PageHelper分页插件的使用
    public PageInfo query2(QueryObject qo){
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Department> list = mapper.query(qo);//紧跟着的这个方法会被分页
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

5、Controller

    @RequestMapping("/query")
    public String query(QueryObject qo,Model model){
        PageInfo pageInfo = service.query2(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "department/list";
    }

6、list页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
    <script>
        function goPage(page){
            //设置当前页的值
            document.getElementById("currentPage").value=page;
            //提交表单
            var form = document.getElementById("searchForm");
            form.submit();//提交表单
        }
    </script>
</head>
<body>
    <h1>部门列表</h1>

    <form id="searchForm" action="/query.do" method="post">
        部门名称:<input type="text" name="deptName" value="${queryObject.deptName}"/>
        部门地址:<input type="text" name="deptLocation"value="${queryObject.deptLocation}"/>
        <input type="submit" value="搜索"/> <br>



        <a href="/edit.do">新增</a>

        <table border="1px" width="50%">
            <tr>
                <th>部门编号</th>
                <th>部门名称</th>
                <th>部门地址</th>
                <th>操作</th>
            </tr>

            <c:forEach items="${pageInfo.list}" var="dept" varStatus="index">
                <tr>
                    <th>${index.count}</th>
                    <th>${dept.name}</th>
                    <th>${dept.location}</th>
                    <th align="center">
                        <a href="/edit.do?id=${dept.id}">修改</a>
                        <a href="/delete.do?id=${dept.id}">删除</a>
                    </th>
                </tr>
            </c:forEach>

            <tr>
                <td colspan="4">
                    <a href="javascript:void(0);" onclick="goPage(1)">首页</a>&nbsp&nbsp
                    <a href="javascript:void(0);" onclick="goPage(${pageInfo.prePage}==0?1:${pageInfo.prePage})">上页</a>&nbsp&nbsp
                    <a href="javascript:void(0);" onclick="goPage(${pageInfo.nextPage})">下页</a>&nbsp&nbsp
                    <a href="javascript:void(0);" onclick="goPage(${pageInfo.pages})">尾页</a>&nbsp&nbsp
                    <input type="text" name="currentPage" id="currentPage"/><input type="button" value="跳转"/>&nbsp&nbsp
                    每页显示:
                    <select name="pageSize">
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="5">5</option>
                    </select>&nbsp&nbsp&nbsp&nbsp
                    当前${pageInfo.pageNum}页/共${pageInfo.pages}页/共${pageInfo.total}条
                </td>
            </tr>
        </table>
    </form>

</body>
</html>

点击分页的功能,会显示对应页面的数据

六、分页功能完善

完善“跳转”和“每页显示”这两个功能

修改list.jsp这两个功能对应的代码

 <input type="text" name="currentPage" id="currentPage" value="${queryObject.currentPage}"/><input type="submit" value="跳转"/>&nbsp&nbsp
                    每页显示:
                    <select name="pageSize" onchange="goPage(1)">
                        <option value="2" ${queryObject.pageSize == 2 ? 'selected':''}>2</option>
                        <option value="3" ${queryObject.pageSize == 3 ? 'selected':''}>3</option>
                        <option value="5" ${queryObject.pageSize == 5 ? 'selected':''}>5</option>
                    </select>&nbsp&nbsp&nbsp&nbsp

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值