陕西小吃美食价格交流群

【依葫芦画瓢】SSM-CRUD --- 2

2021-12-02 05:46:19

继续上一篇的讲解【依葫芦画瓢】SSM-CRUD --- 1


一、逆向工程-MyBatis Generator的使用

a、maven中引入generator的jar包

<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<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>

   <!-- 指定javabean生成的位置 -->
   <javaModelGenerator targetPackage="com.tyron.crud.bean"
     targetProject=".\src\main\java">

     <property name="enableSubPackages" value="true" />
     <property name="trimStrings" value="true" />
   </javaModelGenerator>

   <!-- 指定sql映射文件生成的位置 -->
   <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
     <property name="enableSubPackages" value="true" />
   </sqlMapGenerator>

   <!-- 指定dao接口生成的位置 -->
   <javaClientGenerator type="XMLMAPPER"
     targetPackage="com.tyron.crud.dao" targetProject=".\src\main\java">

     <property name="enableSubPackages" value="true" />
   </javaClientGenerator>

   <!-- table指定每个表的生成策略 -->
   <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());
%>

<!-- jQuery引入  -->
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<!-- bootstrap引入 -->
<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包

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

b、在mybatis的配置文件中,引入PageHelper插件

<!-- MyBatis的分页插件 -->
 <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) {
    // 获取第pn页,5条内容
   PageHelper.startPage(pn, 5);
   List<Employee> emps = employeeService.getAll();
   // 用PageInfo对结果进行包装,传入连续显示的页数
   PageInfo page = new PageInfo(emps, 5);
   // 将pageInfo交给页面
   model.addAttribute("pageInfo", page);
   return "list";
 }


四、使用Spring测试模块提供的测试请求功能

a、编写测试类,对请求进行测试

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:applicationContext.xml""classpath:springMVC.xml" })
public class MvcTest {

 // 传入SpringMvc的ioc
 @Autowired
 WebApplicationContext context;

 // 虚拟MVC请求,获取处理结果
 MockMvc mockMvc;
 
 /**
  * 初始化mockMvc
  * @Before 每次使用都要初始化
  */

 @Before
 public void initMockMvc() 
{
   mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
 }
 /**
  * 测试分页
  */

 @Test
 public void testPage() throws Exception 
{
   // perform模拟发送请求,拿到返回值
   MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn""5")).andReturn();

   // 请求成功以后,请求域中会有pageInfo,我们可以取出pageInfo进行验证
   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资源汇总

友情链接

Copyright © 2023 All Rights Reserved 版权所有 陕西小吃美食价格交流群