【依葫芦画瓢】SSM-CRUD --- 2
繼續上一篇的講解【依葫蘆畫瓢】SSM-CRUD --- 1
摘要:
- 逆向工程-MyBatis Generator使用
- 前端框架-bootstrap的簡單使用
- MyBatis分頁工具-pagehelper的使用
- Spring的模塊測試
一、逆向工程-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資源匯總
總結
以上是生活随笔為你收集整理的【依葫芦画瓢】SSM-CRUD --- 2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: socket服务器显示未响应,QTCPS
- 下一篇: Linux部署禅道在访问web页面进入w