在IDEA上建立SSM專案配置
一,建立Maven專案
二,在pom.xml檔案中新增必要的相關依賴spring,springmvc,mybatis,資料庫連線池,驅動包,其他(jstl,jsp,servletr- api,junit)等
注:依賴匯入時spring的儘量版本號一致
<!--Mybatis Generator--> <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-cor--> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> <!--spring mvc--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.9.RELEASE</version> </dependency> <!--spring context--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.9.RELEASE</version> </dependency> <!--Spring jdbc--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.0.9.RELEASE</version> </dependency> <!--spring test--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.9.RELEASE</version> <scope>test</scope> </dependency> <!--spring 面向切面--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.0.9.RELEASE</version> </dependency> <!--Mybatis--> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!--mybatis 整合spring--> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <!--資料庫連線池 驅動--> <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- Servlet+JSP+JSTL --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.9.RELEASE</version> </dependency> <!--junit測試--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>compile</scope> </dependency>
三,引用bootstrap前端框架
1,在官網下載:https://v3.bootcss.com/
2,在專案webapp檔案下建立靜態資料夾static將下載好的bootstrap資料夾放入下面
3,在再static資料夾下建立js資料夾放入jquery檔案
(推薦一個網站有多個版本的jquery檔案提供下載)
js檔案下載地址:http://www.jq22.com/jquery-info122
結構如下:
4,在jsp檔案中引入必須的檔案
<!--引入jquery--> <script type="text/javascript" src="static/js/jquery-3.3.1.min.js"/> <!--引入樣式--> <link href="static/bootstrap-3.3.7-dist/css/bootstrap.min.css"/> <script type="text/javascript" src="static/bootstrap-3.3.7-dist/js/bootstrap.min.js"/>
四,SSM整合關鍵配置檔案
1,spring配置(放在資原始檔夾resource下)
先建立資料庫連線相關檔案db.properties(放在資原始檔夾resource下)
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <context:component-scan base-package="cn.xf"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--spring的配置檔案,這裡主要配置和業務邏輯有關的--> <!--=====================資料來源,事務控制,XXX=====================--> <context:property-placeholder location="classpath:dbconfig.properties"/> <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--==================================================================--> <!--===============================配置Mybatis的整合=======================--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--指定MyBatis全域性配置檔案的位置--> <property name="configLocation" value="classpath:mybatis-cinfig.xml"/> <property name="dataSource" ref="pooledDataSource"/> <!--指定mybatis,mapper檔案的位置--> <property name="mapperLocations" value="classpath*:/mapper/*Mapper.xml"/> </bean> <!--配置掃描器,將mybatis介面的實現加入到ioc容器中--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--掃描所有的dao介面的實現,加入到ioc容器中--> <property name="basePackage" value="cn.xf.dao"/> </bean> <!--=========================================================================--> <!--配置一個可以執行批量操作的sqlSession--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/> <constructor-arg name="executorType" value="BATCH"></constructor-arg> </bean> <!--====================================事務控制的配置===============================--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--控制住資料來源--> <property name="dataSource" ref="pooledDataSource"></property> </bean> <!--開啟基於註解的事務,使用xml配置形式的事務--> <aop:config> <!--切入點表示式--> <aop:pointcut expression="execution(* cn.xf.service..*(..))" id="txPomnit"/> <!--配置事務增強--> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPomnit"/> </aop:config> <!--配置事務增強,事務如何切入--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!--所有方法都是事務方法--> <tx:method name="*"></tx:method> <!--以get開頭的所有方法--> <tx:method name="get*" read-only="true"/> </tx:attributes> </tx:advice> <!--==========================================================================--> </beans>
2,springmvc配置(配置檔案放在wenapp/WEB-INF下)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--
以下是有關HelloWeb-servlet.xml檔案的重點說明:
[servlet-name]-servlet.xml檔案將用於建立定義的bean,
它會覆蓋在全域性範圍中使用相同名稱定義的任何bean的定義。
<context:component-scan ...>標籤將用於啟用Spring MVC註釋掃描功能,
允許使用@Controller和@RequestMapping等註釋。InternalResourceViewResolver將定義用於解析檢視名稱的規則。
根據上面定義的規則,hello的邏輯檢視將委託給位於/WEB-INF/jsp/hello.jsp這個檢視來實現。
-->
<!--springmvc的配置檔案,包含網站跳轉邏輯的控制,配置-->
<context:component-scan base-package="cn.xf" use-default-filters="false">
<!--只掃描控制器-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置檢視解析器,方便頁面返回-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--兩個標準配置-->
<!--將springmvc不能處理的請求交給tomcat-->
<mvc:default-servlet-handler/>
<!--能支援springmvc的高階的一些功能,jsr303校驗,快捷的ajax..對映動態請求-->
<mvc:annotation-driven />
</beans>
3,配置mybatis(在sources資原始檔夾下建立)
<?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>
<typeAliases>
<package name="cn.xf.bean"/>
</typeAliases>
</configuration>
4,配置web.xml檔案
<!--1 啟動spring容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--2, Springmvc的前端控制器,攔截所有請求-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--3 字元過濾器-->
<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>
<!--4 使用Rest風格的url,將頁面普通的post請求轉為指定的delete或者put-->
<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>
五,使用mybatis逆向工程
1,在專案更目錄下建立配置檔案配置資料庫中的表和專案對映
注:現在專案下建立相應的資料夾包
如下:
<?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="資料庫連線地址"
userId="資料庫使用者名稱"
password="密碼">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--指定javaBean生成位置-->
<javaModelGenerator
targetPackage="cn.xf.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介面生成的位置,mapper介面-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="cn.xf.dao"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--table指定每個表的生成策略-->
<table tableName="資料庫中的表名" domainObjectName="對映到專案中的實體名"></table>
<table tableName="資料庫中的表名" domainObjectName="對映到專案中的實體名"></table>
</context>
</generatorConfiguration>
2,執行mybatis逆向工程
在包中建立一個java檔案
package cn.xf.test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class MBGTest {
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("建立mybatis逆向工程的配置檔案");
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);
}
}
3,點選執行該類,則會在相應的資料夾下生成檔案
注:想了解更多mybatis配置和逆向工程的,請移步到mybatis官網,有更詳細的文件說明
網址:http://www.mybatis.org/mybatis-3/
就這樣ssm的基本配置就完成啦......
如有什麼不太清楚的和建議,請多多留言,大家互相交流學習啊。。。