2018-12-07 尚硅谷 ssm-crudSSM-CRUD簡(jiǎn)單搭建 筆記----壹

嘗試去用ssm(Spring+SpringMVC+MyBatis)寫(xiě)一個(gè)CRUD(增刪改查春感,Create+Retrieve+Update+Delete)砌创,按照自己搭建的步驟去寫(xiě)寫(xiě),希望加深印象鲫懒。
SpringMVC+Spring+MyBatis

CRUD:Create(創(chuàng)建) Retrieve(查詢(xún)) Update(更新) Delete(刪除)

功能點(diǎn):
1嫩实、 分頁(yè)
2、數(shù)據(jù)校驗(yàn)
? jquery前端校驗(yàn)+JSR303后端校驗(yàn)
3窥岩、ajax
4甲献、Rest風(fēng)格的URI;使用HTTP協(xié)議請(qǐng)求方式的動(dòng)詞谦秧,來(lái)表示對(duì)資源的操作(GET(查詢(xún))竟纳,POST(新增),PUT(修改)疚鲤,DELETE(刪除))

技術(shù)點(diǎn):
基礎(chǔ)框架-ssm(SpringMVC+Spring+MyBatis)
數(shù)據(jù)庫(kù)-MySQL
前端框架-bootstrap快速搭建簡(jiǎn)潔美觀的界面
項(xiàng)目的依賴(lài)管理-Maven
分頁(yè)-pagehelper
逆向工程-MyBatis Generator

推薦參考的官方文檔:

Spring:http://spring.io/docs
MyBatis:http://mybatis.github.io/mybatis-3/
Mybatis-PageHelper:https://github.com/pagehelper/Mybatis-PageHelper


作者:仁者12138
來(lái)源:CSDN
原文:https://blog.csdn.net/qq_35508033/article/details/78493042
版權(quán)聲明:本文為博主原創(chuàng)文章锥累,轉(zhuǎn)載請(qǐng)附上博文鏈接!
一: 項(xiàng)目功能點(diǎn):

    1.分頁(yè)查詢(xún)顯示     2.數(shù)據(jù)校驗(yàn):jquery前端校驗(yàn)與jsr303后端校驗(yàn)    3.ajax

   4.Rest風(fēng)格的URI;使用Http協(xié)議請(qǐng)求方式的動(dòng)詞集歇,來(lái)表示對(duì)資源的操作(get(查),post(增),put(修       改),delete(刪除))

 項(xiàng)目技術(shù)點(diǎn):

      .基礎(chǔ)框架-ssm(Spring+SpringMVC+MyBatis)

      .數(shù)據(jù)庫(kù)-mysql

      .前端框架-bootstrap搭建前端界面     

      .項(xiàng)目依賴(lài)管理-maven

      .分頁(yè)-pagehelper

      .逆向工程

二:創(chuàng)建maven項(xiàng)目

1.創(chuàng)建maven工程

2.引入項(xiàng)目所需的jar包桶略,編寫(xiě)pom.xml。引入必備jar包,比如(spring,springmvc,mybatis,數(shù)據(jù)庫(kù)連接池际歼,驅(qū)動(dòng)包)在這里我是直接全部引入了惶翻。在maven中央倉(cāng)庫(kù):(http://mvnrepository.com/)搜索引入相關(guān)jar包。具體代碼如下:

POM.XML
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.atguigu</groupId>
<artifactId>ssm-crud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>




<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>



<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>



<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>



<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
<scope>test</scope>
</dependency>



<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>



<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>


<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.0</version>
</dependency>



<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>



<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>



<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>


<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>

</dependencies>
</project>
三:引入BootStrap前端框架
官網(wǎng)下載bootstrap jar包鹅心,https://v3.bootcss.com/getting-started/ 導(dǎo)入到項(xiàng)目中
新建index.jsp 引入bootstrap樣式 引入jquery樣式
(項(xiàng)目導(dǎo)視圖)

index.jsp(目前進(jìn)行引入吕粗,后期在編寫(xiě)前端頁(yè)面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:forward page="/emps/"></jsp:forward>
<!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>Insert title here</title>

<script type="text/javascript" src="static/js/jquery-1.12.4.min.js"></script>


<link rel="stylesheet">
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" >
</script>

</head>
<body>

</body>
</html>
四: ssm框架搭建 配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>ssm-crud</display-name>



<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>



<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>location</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>


<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>

</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>
五:編寫(xiě) dbconfig.properties 配置數(shù)據(jù)源
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_crud
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456
六:配置SpringMvc,Spring,Mbatis.xml
spring的配置文件代碼:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:component-scan base-package="com.atguigu">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>



<context:property-placeholder location="classpath:dbconfig.properties"/>
<bean id="PooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="{jdbc.jdbcUrl}"></property> <property name="driverClass" value="{jdbc.driverClass}"></property>
<property name="user" value="{jdbc.user}"></property> <property name="password" value="{jdbc.password}"></property>
</bean>


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="configLocation" value="classpath:mybatis-config.xml"></property>

<property name="dataSource" ref="PooledDataSource"></property>
    <!-- 指定mybatis,mapper配置文件的位置 -->
<property name="mapperLocations" value="classpath:mapper/*"></property>
</bean>



<!-- 配置掃描器旭愧,將mybatis接口的實(shí)現(xiàn)加入到ioc容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.atguigu.crud.dao"></property>
</bean>

     <!-- 事物 控制的配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!-- 控制數(shù)據(jù)源 -->
    <property name="dataSource" ref="PooledDataSource"></property>
</bean>
<!-- 開(kāi)啟給予注解的是事務(wù)控制颅筋,使用xml配置形式的的事務(wù)(必須主要的都是使用配置式) -->
<aop:config>
    <!-- 切入點(diǎn)表達(dá)式 -->
    <aop:pointcut expression="execution(* com.atguigu.crud.service..*(..))" id="txPoint"/>
    <!-- 配置事務(wù)增強(qiáng) -->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    
</aop:config>
<!-- 配置事務(wù)增強(qiáng),事務(wù)如何切入 -->
<tx:advice id="txAdvice">
    <tx:attributes>
        <!--所有方法都是事務(wù)方法  -->
        <tx:method name="*"/>
        <!-- 以get開(kāi)始的方法 -->
        <tx:method name="get*" read-only="true"/>
    </tx:attributes>
</tx:advice>
<!-- spring配置文件的 配置核心點(diǎn)-->

</beans>
springMVC的配置文件输枯,主要負(fù)責(zé)網(wǎng)站的邏輯控制议泵,配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<!-- springMVC的配置文件,主要負(fù)責(zé)網(wǎng)站的邏輯控制桃熄,配置 -->
<context:component-scan base-package="com.atguigu" use-default-filters="false">
<!-- 只掃描控制器 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan> 
<!-- 配置視圖解析器先口,方便頁(yè)面返回 --> 
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

<!-- 兩個(gè)配置標(biāo)準(zhǔn) -->
<!-- 將springmvc 不能處理的請(qǐng)求交給tomcat-->
<mvc:default-servlet-handler/>
<!--能支持springmvc的高級(jí)功能,最主要的是映射動(dòng)態(tài)請(qǐng)求  -->
<mvc:annotation-driven/>

</beans>
mybatis配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

<!-- 類(lèi)型別名 -->
<typeAliases>
    <package name="com.atguigu.crud.bean"/>
</typeAliases>

<!-- 分頁(yè)插件   必須放在typeAliases后面 -->
<plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <property name="reasonable" value="true"/>
    </plugin>
</plugins>

</configuration>


作者:和你一起多吃點(diǎn)
來(lái)源:CSDN
原文:https://blog.csdn.net/wmlwml0000/article/details/80226095
版權(quán)聲明:本文為博主原創(chuàng)文章瞳收,轉(zhuǎn)載請(qǐng)附上博文鏈接碉京!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市缎讼,隨后出現(xiàn)的幾起案子收夸,更是在濱河造成了極大的恐慌,老刑警劉巖血崭,帶你破解...
    沈念sama閱讀 221,548評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件卧惜,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡夹纫,警方通過(guò)查閱死者的電腦和手機(jī)咽瓷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)舰讹,“玉大人茅姜,你說(shuō)我怎么就攤上這事≡孪唬” “怎么了钻洒?”我有些...
    開(kāi)封第一講書(shū)人閱讀 167,990評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)锄开。 經(jīng)常有香客問(wèn)我素标,道長(zhǎng),這世上最難降的妖魔是什么萍悴? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,618評(píng)論 1 296
  • 正文 為了忘掉前任头遭,我火速辦了婚禮寓免,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘计维。我一直安慰自己袜香,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布鲫惶。 她就那樣靜靜地躺著蜈首,像睡著了一般。 火紅的嫁衣襯著肌膚如雪剑按。 梳的紋絲不亂的頭發(fā)上疾就,一...
    開(kāi)封第一講書(shū)人閱讀 52,246評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音艺蝴,去河邊找鬼。 笑死鸟废,一個(gè)胖子當(dāng)著我的面吹牛猜敢,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播盒延,決...
    沈念sama閱讀 40,819評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼缩擂,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了添寺?” 一聲冷哼從身側(cè)響起胯盯,我...
    開(kāi)封第一講書(shū)人閱讀 39,725評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎计露,沒(méi)想到半個(gè)月后博脑,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,268評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡票罐,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評(píng)論 3 340
  • 正文 我和宋清朗相戀三年叉趣,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片该押。...
    茶點(diǎn)故事閱讀 40,488評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡疗杉,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蚕礼,到底是詐尸還是另有隱情烟具,我是刑警寧澤,帶...
    沈念sama閱讀 36,181評(píng)論 5 350
  • 正文 年R本政府宣布奠蹬,位于F島的核電站朝聋,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏罩润。R本人自食惡果不足惜玖翅,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評(píng)論 3 333
  • 文/蒙蒙 一翼馆、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧金度,春花似錦应媚、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,331評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至跟伏,卻和暖如春丢胚,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背受扳。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,445評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工携龟, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人勘高。 一個(gè)月前我還...
    沈念sama閱讀 48,897評(píng)論 3 376
  • 正文 我出身青樓峡蟋,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親华望。 傳聞我的和親對(duì)象是個(gè)殘疾皇子蕊蝗,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容