ssm整合mybitis的逆向工程
1. 引入jar包
mybatis generator http://www.mybatis.org/generator/
代码语言:javascript复制 <!-- MyBatis Generator Core-->
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
2、添加一个配置文件mgb.xml(MBG配置(mybatis generator简称))
参考文档: http://www.mybatis.org/generator/configreference/xmlconfig.html
代码语言:javascript复制<?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>
<!--导入的类什么的,我们不需要,直接删除-->
<!--<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />-->
<context id="DB2Tables" targetRuntime="MyBatis3Simple">
<!--如果不配置这个,生成的文件会有很多的注释,不方便看代码-->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--配置数据库连接-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://kt01:3306/kongtao?characterEncoding=utf8&useSSL=false"
userId="root"
password="123456">
</jdbcConnection>
<!--配置数据库连接-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--JAVA模型生成,指定javabean生成位置-->
<javaModelGenerator targetPackage="cn.qkongtao.model" targetProject=".srcmainjava">
<!--JAVA模型生成,指定javabean生成位置-->
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--指定sql映射文件生成位置-->
<sqlMapGenerator targetPackage="mapper" targetProject=".srcmainresources">
<!--指定sql映射文件生成位置-->
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--指定dao生成的位置,mapper接口-->
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.qkongtao.mapper" targetProject=".srcmainjava">
<!--指定dao生成的位置,mapper接口-->
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--table指定表的生成策略-->
<table tableName="areaEd" domainObjectName="Area" >
<!--字段区分大小写-->
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="historyEd" domainObjectName="History">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="provinceEd" domainObjectName="Province">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="totalEd" domainObjectName="Total">
<property name="useActualColumnNames" value="true"/>
</table>
</context>
</generatorConfiguration>
3. 编写一个Generator.java类(用来运行配置生成生成代码)
代码语言:javascript复制import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
public class Generator {
public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("src/main/mgb.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
callback, warnings);
myBatisGenerator.generate(null);
}
public static void main(String[] args) throws Exception {
try {
Generator generatorSqlmap = new Generator();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.点击运行
就会在配置好的对应文件里面生成model、mapper和xml等文件。 如图所示
5. 测试逆向工程生成的方法
代码语言:javascript复制package cn.qkongtao.test;/*
*Created by tao on 2020-04-27.
*/
import cn.qkongtao.mapper.ProvinceMapper;
import cn.qkongtao.model.Province;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
/*
* 使用Junit单元测试:测试我们的配置
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class GeneratorTest {
@Autowired
private ProvinceMapper provinceMapper;
@Test
public void findAll() {
List<Province> provinces = provinceMapper.selectAll();
for (Province province : provinces) {
System.out.println(province);
}
}
}
测试结果如下
ssm整合分页插件
1. 引入jar包
代码语言:javascript复制 <!-- 引入mybatis的 pagehelper 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
2. 整合Mybatis配置文件增加分页插件
代码语言:javascript复制<!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 扫描model包 -->
<!--修改-->
<property name="typeAliasesPackage" value="cn.qkongtao.model"/>
<!-- 扫描sql配置文件:mapper需要的xml文件-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!--分页插件-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect"></prop>
<prop key="reasonable"></prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
- 分页插件的使用
* 控制类
代码语言:javascript复制package cn.qkongtao.controller;/*
*Created by tao on 2020-04-27.
*/
import cn.qkongtao.mapper.ProvinceMapper;
import cn.qkongtao.model.Province;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Controller
public class ProvinceController {
@Autowired
private ProvinceMapper provinceMapper;
/**
* 分页查询:
* 参数:把当前页数传到后台,并且给他一个默认值
* 把查到的数据和分页类传到前台页面
*/
@RequestMapping("queryByPage")
public String queryByPage(@RequestParam(value=("page"),defaultValue = "1")Integer page, Model model){
PageHelper.startPage(page, 10);//每页条数=3
//查询数据
List<Province> provinces = provinceMapper.selectAll();
//查看一下是否查到了数据
for (Province province : provinces) {
System.out.println(province);
}
/**
* pageNum:当前页
* pageSize:每页的数量
* size:当前页的数量
* pages:总页数
* total:总记录数
*/
/**
* PageInfo就是当初那个分页工具类(分页bean)
*/
PageInfo<Province> pageInfo = new PageInfo<Province>(provinces);//把数据放在分页类中
System.out.println(pageInfo.toString());
//封装数据
model.addAttribute("provinces", provinces);
//传分页类
model.addAttribute("pageInfo", pageInfo);
return "provinceList";//跳转页面
}
}
- 前台页面jsp 使用bootstrap的分页样式
<%--
Created by IntelliJ IDEA.
User: tao
Date: 2020-04-27
Time: 19:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!-- 引入jstl核心标签库-->
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>省份列表</title>
<style type="text/css">
td, th {
text-align: center;
}
</style>
</head>
<!-- 1. 导入CSS的全局样式 -->
<link href="${pageContext.request.contextPath}/css/bootstrap.min.css" rel="nofollow noopener" rel="stylesheet">
<!-- 2. jQuery导入,建议使用1.9以上的版本 -->
<script src="${pageContext.request.contextPath}/js/jquery-2.1.0.min.js"></script>
<!-- 3. 导入bootstrap的js文件 -->
<script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
<body>
<h4 style="text-align: center">省份信息列表</h4>
<!-- 数据部分 -->
<table border="1" class="table table-bordered table-hover" style="width: 1200px;margin: 20px auto;">
<tr>
<td>编号</td>
<td>省份名称</td>
<td>确诊人数</td>
<td>死亡人数</td>
<td>治愈人数</td>
</tr>
<tbody>
<c:forEach items="${provinces}" var="province" varStatus="s">
<tr>
<td>${s.count}</td>
<td>${province.provinceName}</td>
<td>${province.confirmedNum}</td>
<td>${province.deathsNum}</td>
<td>${province.curesNum}</td>
</tr>
</c:forEach>
</tbody>
</table>
<br>
<%--分页--%>
<div style="width: 800px;text-align: center;margin: 20px auto">
<nav aria-label="Page navigation">
<ul class="pagination">
<%--如果是第一页不允许上一页--%>
<c:if test="${pageInfo.pageNum == 1}">
<li class="disabled"><span aria-hidden="true">«</span></li>
</c:if>
<c:if test="${pageInfo.pageNum != 1}">
<li>
<a href="/queryByPage?page=${pageInfo.prePage}" rel="nofollow noopener"
aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<%--分页的页码--%>
<c:forEach begin="1" end="${pageInfo.pages}" var="i">
<c:if test="${pageInfo.pageNum == i}">
<li class="active"><a
href="/queryByPage?page=${i}" rel="nofollow noopener" rel="nofollow noopener" >${i}</a>
</li>
</c:if>
<c:if test="${pageInfo.pageNum != i}">
<li>
<a href="/queryByPage?page=${i}" rel="nofollow noopener" rel="nofollow noopener" >${i}</a>
</li>
</c:if>
</c:forEach>
<%--判断是否是最后一页--%>
<c:if test="${pageInfo.pageNum == pageInfo.pages}">
<li class="disabled"><span aria-hidden="true">»</span></li>
</c:if>
<c:if test="${pageInfo.pageNum != pageInfo.pages}">
<li>
<a href="/queryByPage?page=${pageInfo.nextPage}" rel="nofollow noopener"
aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
<span style="font-size: 25px;margin-left: 5px;">
共${pageInfo.total}条记录,共${pageInfo.pages}页
</span>
</ul>
</nav>
</div>
</body>
</html>