日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發(fā)布時間:2024/3/13 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【依葫芦画瓢】SSM-CRUD --- 2 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

繼續(xù)上一篇的講解【依葫蘆畫瓢】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、根據(jù)官方文檔(http://www.mybatis.org/generator/)中的『MyBatis GeneratorXML Configuration File Reference』文檔配置相應(yīng)的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>
? ?<!-- 配置數(shù)據(jù)庫連接 -->
? ?<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、根據(jù)文檔中的『Running MyBatis Generator With Java』配置相對應(yīng)的測試類

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函數(shù)即可生成對應(yīng)的bean以及mapper文件,有了基礎(chǔ)的代碼,便可以根據(jù)自己需求自定義修改。


二、前端框架-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、簡單使用,獲取的數(shù)據(jù)可通過bootstrap顯示

@RequestMapping("/emps")
?public?String?getEmps(@RequestParam(value =?"pn", defaultValue =?"1") Integer pn, Model model) {
?? ?// 獲取第pn頁,5條內(nèi)容
? ?PageHelper.startPage(pn,?5);
? ?List<Employee> emps = employeeService.getAll();
? ?// 用PageInfo對結(jié)果進行包裝,傳入連續(xù)顯示的頁數(shù)
? ?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請求,獲取處理結(jié)果
?MockMvc mockMvc;
?
?/**
? * 初始化mockMvc
? * @Before 每次使用都要初始化
? */

?@Before
?public?void?initMockMvc()?
{
? ?mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
?}
?/**
? * 測試分頁
? */

?@Test
?public?void?testPage() throws Exception?
{
? ?// perform模擬發(fā)送請求,拿到返回值
? ?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("總記錄數(shù):"?+ pi.getTotal());
? ?System.out.print("在頁面需要連續(xù)顯示的頁碼:");
? ?int[] nums = pi.getNavigatepageNums();
? ?for?(int?i : nums) {
? ? ?System.out.print(" "?+ i);
? ?}
? ?System.out.println();
? ?// 獲取員工數(shù)據(jù)
? ?List<Employee> list = pi.getList();
? ?for?(Employee employee : list) {
? ? ?System.out.println("Id:"?+ employee.getEmpId() +?"==>name:"?+ employee.getEmpName());
? ?}
?}
}

b、測試得到結(jié)果,將結(jié)果用bootstrap顯示

c、注意事項:Spring4測試的時候,需要servlet3.0的支持


五、說明

①簡單的查詢功能已實現(xiàn),后面文章中會講解增加、修改、刪除及校驗功能;

②上一篇中說到學習SpringMVC可以參考『開濤』大神的博客,為了方便大家瀏覽,我在網(wǎng)上下載了PDF版本的系列文章,如有需要可在公眾號后臺回復『開濤』獲取。

③學習中,官方文檔是最好的學習資料!!!


推薦閱讀

不忘初心 方得始終

【福利時刻】免費Java資源匯總

總結(jié)

以上是生活随笔為你收集整理的【依葫芦画瓢】SSM-CRUD --- 2的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。