?前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。
1.環境:
maven 版本:3.5.1
ecelipse mars.2
JDK ?: ?jdk1.8.0_45
tomcat ?: ?apache-tomcat-8.0.0-RC1
?
2. 建maven 工程:
new -> other ->?Maven Project-> next-> 勾選 Create a simple project ?-> next?->?
?Group Id 填 包名 、Artifact Id 填 工程名、packaging 選 war , 其余5個不填。?-> finish 。
?
工程上有個小紅叉 ,解決方案:
在工程上右鍵->?javaEE tools -> Generate Deployment Descriptor Stub。
額外配置:工程上右鍵 -> properties?-> Project Facets?-> Dynamic Web Module 選3.1 、java 選 1.8 。
3.ssm工程:
model、mapper、conreoller... 代碼寫在src/main/java 文件夾下。
applicationContext.xml、log4j.properties、
dbconfig.properties、spring-mybatis.xml、spring-servlet.xml放在src/main/resources文件夾下。
?
?
?
在如圖選中的位置(src/main/resources)右鍵 new ->other -> Spring文件下選擇Spring Bean Configuration File ->next
?->取個名字 -> next ?-> 選擇需要的項,可選擇下面對應不同版本。不選版本默認為最新版本。-> finish
這樣就可以自動生成文件頭,如下圖:
?
4.整合:
applicationContext.xml ? :
<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--啟動注解 --><context:annotation-config /><import resource="spring-mybatis.xml"/></beans>
?
?
?
dbconfig.properties:
?
driverClassName=oracle.jdbc.driver.OracleDriver
validationQuery=SELECT 1 FROM DUAL
#jdbc_url=jdbc:oracle:thin:@192.168.2.203:1521:orcl
jdbc_url=jdbc:oracle:oci:@OFFICE_ORCL
jdbc_username=TEST1
jdbc_password=TEST1
log4j.properties:
?
?
log4j.rootLogger=INFO, stdout, logfilelog4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%nlog4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=mpsmodel.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n#kai write
log4j.logger.com.ibatis = DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource = DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner = DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = DEBUG
log4j.logger.java.sql.Connection = DEBUG
log4j.logger.java.sql.Statement = DEBUG
log4j.logger.java.sql.PreparedStatement = DEBUG
log4j.logger.java.sql.ResultSet = DEBUG
spring-mybatis.xml :
?
?
?
?
<?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"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:jdbc="http://www.springframework.org/schema/jdbc"xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 自動掃描(自動注入),掃描com包以及子包 --><context:component-scan base-package="com" /><!-- 引入dbconfig.properties屬性文件 --><context:property-placeholder location="classpath:dbconfig.properties" /><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="${driverClassName}" /><property name="url" value="${jdbc_url}" /><property name="username" value="${jdbc_username}" /><property name="password" value="${jdbc_password}" /></bean><!-- 把數據源注入給Session工廠 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 實例化sqlSessionFactory時需要使用上述配置好的數據源以及SQL映射文件 --><property name="dataSource" ref="dataSource" /><!-- 自動掃描mapper目錄下的所有映射的xml文件, 路徑下包中的所有xml文件可以被自動掃描。--><property name="mapperLocations" value="classpath.com.*.xml" /></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 掃描com.mapper這個包以及它的子包--><property name="basePackage" value="com.mapper" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /></bean><!-- 配置Spring的事務管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><tx:annotation-driven transaction-manager="transactionManager" /></beans>
spring-servlet.xml :
?
?
?
?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://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"><!--激活注解模式--><mvc:annotation-driven /><!--自動掃描包--><context:component-scan base-package="com.controller"/><!-- 定義視圖解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix"><value>/WEB-INF/views/</value></property><property name="suffix"><value>.jsp</value></property></bean><!--靜態資源訪問 --><mvc:resources location="/js/" mapping="/js/**"/><!-- 模板信息設置 --> <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resourceLoaderPath" value="WEB-INF/views" /><!--設置模板位置--> <property name="velocityProperties"> <props> <prop key="directive.foreach.counter.name">loopCounter</prop> <prop key="directive.foreach.counter.initial.value">0</prop> <prop key="input.encoding">UTF-8</prop><!--指定模板引擎進行模板處理的編碼--> <prop key="output.encoding">UTF-8</prop><!--指定輸出流的編碼--> </props> </property></bean><bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"><property name="cache" value="false"/><!--多ViewResovler配置--><property name="order" value="0"/><!--這是另外一種視圖 --><property name="suffix" value=".vm"/><!--避免亂碼--> <property name="contentType" value="text/html;charset=UTF-8"/></bean></beans>
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" version="2.5"><display-name>test6</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><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><listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>
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</groupId><artifactId>test6</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><springversion>4.2.4.RELEASE</springversion></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.4.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${springversion}</version><type>jar</type><scope>compile</scope></dependency><dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${springversion}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${springversion}</version> </dependency><dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${springversion}</version> </dependency><dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${springversion}</version> </dependency> <dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.3.0</version></dependency><!-- 添加mybatis與Spring整合的核心包 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.2.3</version></dependency><dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency><!-- 導入dbcp的jar包,用來在applicationContext.xml中配置數據庫 --> <dependency><groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency><dependency><groupId>com.oracle</groupId><artifactId>ojdbc6</artifactId><version>11.2.0.4.0</version></dependency><dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${springversion}</version> </dependency><dependency><groupId>org.apache.velocity</groupId><artifactId>velocity</artifactId><version>1.7</version></dependency></dependencies><build><finalName>test6</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><!--這是maven的版本 ,和工程中配置文件的版本無關--><version>3.5.1</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins><!--編譯的時候把xml文件也一起編譯,默認不編譯xml文件。 --><resources> <resource> <directory>src/main/java</directory> <includes><include>**/*.xml</include></includes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build>
</project>
可以寫小demo了。
?
?
?
?
?
?
?
?
?
?
?
?
?