觀看本文前,請至少保證對 Spring+Spring MVC+MyBatis 有必定的瞭解,並熟悉使用Maven進行項目構建。
這裏我只列出大體的目錄結構,具體請根據本身的需求進行更改。css
1.編寫 pom.xml
添加框架所需依賴html
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yileaf</groupId> <artifactId>SSM</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>SSM Maven Web</name> <!-- 統一設置設置版本號 --> <properties> <junit-version>4.12</junit-version> <log4j-version>2.6.2</log4j-version> <lombok-version>1.18.8</lombok-version> <jackson.version>2.10.0</jackson.version> <druid-version>1.1.20</druid-version> <mybatis-version>3.5.3</mybatis-version> <mybatis-spring-version>2.0.3</mybatis-spring-version> <spring-version>5.2.1.RELEASE</spring-version> <mysql-connector-java-version>5.1.47</mysql-connector-java-version> </properties> <dependencies> <!-- spring依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-version}</version> </dependency> <!--aspectj切面編程支持--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency> <!--Jackson依賴--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <!-- mybatis依賴 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis-version}</version> </dependency> <!--mybatis-spring整合包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring-version}</version> </dependency> <!--數據庫鏈接驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector-java-version}</version> </dependency> <!-- druid鏈接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid-version}</version> </dependency> <!-- lombok支持 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok-version}</version> <scope>provided</scope> </dependency> <!--servlet支持--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- 單元測試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit-version}</version> <scope>test</scope> </dependency> <!-- log4j2日誌文件依賴 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>${log4j-version}</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>${log4j-version}</version> </dependency> <!--分頁插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.10</version> </dependency> <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>2.0</version> </dependency> </dependencies> <build> <finalName>SSM</finalName> <!--解決maven項目下,資源文件的讀取問題--> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
2.編寫數據庫鏈接配置(我這裏的名字是jdbc.properties
)前端
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/demo?useSSL=false jdbc.username=root jdbc.password=AngelBeats
3.編寫 MyBatis 核心配置文件(我這裏的名字是sqlMapConfig.xml
)java
<?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> <!--配置別名--> <typeAliases> <!--指定包名,別名就是類名--> <package name="com.ssm.study.entity"/> </typeAliases> <!--配置分頁--> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="reasonable" value="true"/> </plugin> </plugins> </configuration>
4.編寫 Spring 核心配置文件(我這裏的名字是applicationContext.xml
)mysql
<?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: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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--開啓註解掃描,我這裏直接掃描整個項目--> <context:component-scan base-package="com.ssm.study"> <!--排除Controller的註解掃描,讓Springmvc負責--> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 加載properties文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置數據源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 配置sessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--注入數據源--> <property name="dataSource" ref="dataSource"/> <!--加載mybatis核心配置--> <property name="configLocation" value="classpath:sqlMapConfig.xml"/> </bean> <!--配置Mapper掃描,讓Spring容器產生Mapper實現類--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ssm.study.mapper"/> </bean> <!--配置事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--開啓事務註解驅動--> <tx:annotation-driven/> </beans>
5.編寫 Spring MVC 配置文件(我這裏的名字是spring-mvc.xml
)git
<?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:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--Controller的組件掃描--> <context:component-scan base-package="com.ssm.study.web.controller"/> <!--mvc註解驅動--> <mvc:annotation-driven/> <!--開放靜態資源訪問--> <mvc:default-servlet-handler/> </beans>
6.編寫 web.xml
加載咱們的配置github
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--配置監聽器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--全局初始化參數--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--解決亂碼的過濾器--> <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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--配置SpringMVC的核心控制器--> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--初始化加載屬於spring-mvc的xml--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <!--設置啓動建立對象--> <load-on-startup>1</load-on-startup> </servlet> <!--配置攔截路徑--> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
1.編寫實體類,對應數據庫的字段web
2.編寫數據返回封裝類spring
3.編寫數據接口sql
4.編寫 Service 層
5.編寫 controller 層
6.編寫前端數據展現界面(我這裏使用Layui提供的動態表格進行數據展現)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>table模塊快速使用</title> <meta name="renderer" content="webkit"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <link rel="stylesheet" href="/layui/css/layui.css" media="all"> </head> <body> <div class="demoTable" style="margin: 30px 300px;"> 搜索用戶名: <div class="layui-inline"> <input class="layui-input" name="name" id="demoReload" autocomplete="off"> </div> <button class="layui-btn" data-type="reload">搜索</button> </div> <table class="layui-hide" id="demo"></table> <script src="/layui/layui.js"></script> <script> layui.use(['table','layer'], function () { const table = layui.table; const layer = layui.layer; const index = layer.load(1);//添加laoding,0-2兩種方式 //獲取參數 table.render({ elem: '#demo' , height: 460 , url: '/user/findAll' //數據接口 , title: '用戶表' , page: true //開啓分頁 , limits: [8, 10, 20] //每頁條數的選擇項 , limit: 8 //每頁默認顯示的數量 , cols: [[ //表頭 {type: 'checkbox', fixed: 'left'} , {field: 'id', title: 'ID', width: 60, sort: true} , {field: 'name', title: '用戶名', width: 180} , {field: 'age', title: '年齡', width: 80, sort: true} , {field: 'birthday', title: '生日', width: 180} , {field: 'phone', title: '聯繫電話', width: 160} , {field: 'bwh', title: '三圍', width: 160} , {field: 'address', title: '聯繫地址', width: 200} ]] , id: 'testReload' , request: { pageName: 'pageNum',//頁碼的參數名稱,默認:page limitName: 'pageSize' //每頁數據量的參數名,默認:limit } , response: { statusCode: 200,//從新規定成功的狀態碼爲 200,table 組件默認爲 0 msgName: 'message', //規定狀態信息的字段名稱,默認:msg }, done: function (res) { //返回數據執行回調函數 layer.close(index); //返回數據關閉loading } }); let $ = layui.$, active = { reload: function () { const demoReload = $('#demoReload'); //執行重載 table.reload('testReload', { page: { curr: 1 //從新從第 1 頁開始 } , where: { username: demoReload.val() } }, 'data'); } }; $('.demoTable .layui-btn').on('click', function () { const type = $(this).data('type'); active[type] ? active[type].call(this) : ''; }); }); </script> </body> </html>
案例源碼我存放在Frame-Study庫SSM分支。地址: https://github.com/CodeHaotia...