继续上一篇的讲解【依葫芦画瓢】SSM-CRUD --- 1
一、逆向工程-MyBatis Generator的使用
a、maven中引入generator的jar包
<dependency>
   <groupId>org.mybatis.generator</groupId>
   <artifactId>mybatis-generator-core</artifactId>
   <version>1.3.5</version>
</dependency>
b、根据官方文档(http://www.mybatis.org/generator/)中的『MyBatis GeneratorXML Configuration File Reference』文档配置相应的mbg.xml文件
<?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="DB2Tables" targetRuntime="MyBatis3">
   
   <commentGenerator>
     <property name="suppressAllComments" value="true" />
   </commentGenerator>
   
   <jdbcConnection driverClass="com.mysql.jdbc.Driver"
     connectionURL="jdbc:mysql://localhost:3306/ssm_crud?useSSL=false" userId="root" password="123456">
    </jdbcConnection>
   <javaTypeResolver>
     <property name="forceBigDecimals" value="false" />
   </javaTypeResolver>
   
   <javaModelGenerator targetPackage="com.tyron.crud.bean"
     targetProject=".\src\main\java">
     <property name="enableSubPackages" value="true" />
     <property name="trimStrings" value="true" />
   </javaModelGenerator>
   
   <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
     <property name="enableSubPackages" value="true" />
   </sqlMapGenerator>
   
   <javaClientGenerator type="XMLMAPPER"
     targetPackage="com.tyron.crud.dao" targetProject=".\src\main\java">
     <property name="enableSubPackages" value="true" />
   </javaClientGenerator>
   
   <table tableName="tbl_emp" domainObjectName="Employee"></table>
   <table tableName="tbl_dept" domainObjectName="Department"></table>
 </context>
</generatorConfiguration>
c、根据文档中的『Running MyBatis Generator With Java』配置相对应的测试类
public class MBGTest {
 public static void main(String[] args) throws Exception {
   List<String> warnings = new ArrayList<String>();
   boolean overwrite = true;
   File configFile = new File("mbg.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);
 }
}
d、运行main函数即可生成对应的bean以及mapper文件,有了基础的代码,便可以根据自己需求自定义修改。
二、前端框架-bootstrap的简单使用
a、参考官方文档(http://www.bootcss.com/)学习,重中之重;
b、下载bootstrap-3.3.7-dist,并导入项目,在jsp中引入,使用绝对路径
<%
 pageContext.setAttribute("APP_PATH", request.getContextPath());
%>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<link href="${APP_PATH }/static/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
<script src="${APP_PATH }/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
c、样式使用可查阅官方文档和资源中的视频讲解,我也正在学习中。
三、MyBatis分页工具-pagehelper的使用
a、参考官方文档(https://github.com/pagehelper/Mybatis-PageHelper),在maven中导入jar包
   <dependency>
     <groupId>com.github.pagehelper</groupId>
     <artifactId>pagehelper</artifactId>
     <version>5.0.0</version>
   </dependency>
b、在mybatis的配置文件中,引入PageHelper插件
 <plugins>
   <plugin interceptor="com.github.pagehelper.PageInterceptor">
   </plugin>
 </plugins>
c、简单使用,获取的数据可通过bootstrap显示
@RequestMapping("/emps")
 public String getEmps(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
    
   PageHelper.startPage(pn, 5);
   List<Employee> emps = employeeService.getAll();
   
   PageInfo page = new PageInfo(emps, 5);
   
   model.addAttribute("pageInfo", page);
   return "list";
 }
四、使用Spring测试模块提供的测试请求功能
a、编写测试类,对请求进行测试
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:applicationContext.xml", "classpath:springMVC.xml" })
public class MvcTest {
 
 @Autowired
 WebApplicationContext context;
 
 MockMvc mockMvc;
 
 
 @Before
 public void initMockMvc() {
   mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
 }
 
 @Test
 public void testPage() throws Exception {
   
   MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "5")).andReturn();
   
   MockHttpServletRequest request = result.getRequest();
   PageInfo pi = (PageInfo) request.getAttribute("pageInfo");
   System.out.println("当前页码:" + pi.getPageNum());
   System.out.println("总页码:" + pi.getPages());
   System.out.println("总记录数:" + pi.getTotal());
   System.out.print("在页面需要连续显示的页码:");
   int[] nums = pi.getNavigatepageNums();
   for (int i : nums) {
     System.out.print(" " + i);
   }
   System.out.println();
   
   List<Employee> list = pi.getList();
   for (Employee employee : list) {
     System.out.println("Id:" + employee.getEmpId() + "==>name:" + employee.getEmpName());
   }
 }
}
b、测试得到结果,将结果用bootstrap显示
c、注意事项:Spring4测试的时候,需要servlet3.0的支持
五、说明
①简单的查询功能已实现,后面文章中会讲解增加、修改、删除及校验功能;
②上一篇中说到学习SpringMVC可以参考『开涛』大神的博客,为了方便大家浏览,我在网上下载了PDF版本的系列文章,如有需要可在公众号后台回复『开涛』获取。
③学习中,官方文档是最好的学习资料!!!
推荐阅读
不忘初心 方得始终
【福利时刻】免费Java资源汇总