1、集成springhtml
2、集成springMVCjava
3、集成mybatismysql
1. pom.xmlweb
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ddh.test</groupId> <artifactId>maven_ssm</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>maven_ssm Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <srping.version>4.0.2.RELEASE</srping.version> <mybatis.version>3.2.8</mybatis.version> <slf4j.version>1.7.12</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- java ee包 --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <!-- spring框架包 start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${srping.version}</version> </dependency> <!-- spring框架包 end --> <!-- mybatis框架包 start --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- mybatis框架包 end --> <!-- 數據庫驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency> <!-- 導入dbcp的jar包,用來在applicationContext.xml中配置數據庫 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!-- jstl標籤類 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log END --> <!-- Json --> <!-- 格式化對象,方便輸出日誌 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.6</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <!-- 上傳組件包 start --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency> <!-- 上傳組件包 end --> </dependencies> <build> <finalName>maven_ssm</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.0.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.0</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
2. web.xmlspring
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app > <display-name>Archetype Created Web Application</display-name> <!--1.1配置spring初始化路徑--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--1.2監聽器的方式,加載spring,這就是spring和tomcat的接口--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--配置springmvc的核心控制器--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!--!!指定springMVC的配置文件--> <param-value>classpath:applicationContext-mvc.xml</param-value> </init-param> <!--啓動級別,和服務器一塊兒啓動--> <load-on-startup>1</load-on-startup> </servlet> <!--配置核心控制器映射,配置/意思是全部url都經過此servlet--> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3. applicationContext.xmlsql
<?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 "> <!--掃描service--> <context:component-scan base-package="com.daydayhave.ssm.service"/> <bean id="date" class="java.util.Date" /> <!--引入mybatis--> <import resource="applicationContext-mybatis.xml" /> <!-- 開啓spring對事務的註解支持 --> <tx:annotation-driven/> </beans>
4. applicationContext-mvc.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: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.daydayhave.ssm.web.controller"/> <!--靜態資源放行--> <mvc:default-servlet-handler/> <!--開啓springmvc註解支持-@RequestMapping--> <mvc:annotation-driven/> <!--文件上傳--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 設置上傳文件的最大尺寸爲1MB --> <property name="maxUploadSize"> <!-- spring el寫法:5MB --> <value>#{1024*1024*5}</value> </property> </bean> <!--視圖解析器:先後綴配置--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前綴配置 --> <property name="prefix" value="/WEB-INF/jsps/" /> <!-- 後綴配置 --> <property name="suffix" value=".jsp" /> </bean> </beans>
5. applicationContext-mybatis.xmlexpress
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--jdbc.properties datasoruce(dbcp) SqlSessionFatory mapper servce junit 事務處理 web--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置數據源--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <!--鏈接數據4個屬性 --> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!--maxActive: 最大鏈接數量 --> <property name="maxActive" value="150" /> <!--minIdle: 最小空閒鏈接 --> <property name="minIdle" value="5" /> <!--maxIdle: 最大空閒鏈接 --> <property name="maxIdle" value="20" /> <!--initialSize: 初始化鏈接 --> <property name="initialSize" value="30" /> <!-- 用來配置數據庫斷開後自動鏈接的 --> <!-- 鏈接被泄露時是否打印 --> <property name="logAbandoned" value="true" /> <!--removeAbandoned: 是否自動回收超時鏈接 --> <property name="removeAbandoned" value="true" /> <!--removeAbandonedTimeout: 超時時間(以秒數爲單位) --> <property name="removeAbandonedTimeout" value="10" /> <!--maxWait: 超時等待時間以毫秒爲單位 1000等於60秒 --> <property name="maxWait" value="1000" /> <!-- 在空閒鏈接回收器線程運行期間休眠的時間值,以毫秒爲單位. --> <property name="timeBetweenEvictionRunsMillis" value="10000" /> <!-- 在每次空閒鏈接回收器線程(若是有)運行時檢查的鏈接數量 --> <property name="numTestsPerEvictionRun" value="10" /> <!-- 1000 * 60 * 30 鏈接在池中保持空閒而不被空閒鏈接回收器線程 --> <property name="minEvictableIdleTimeMillis" value="10000" /> <property name="validationQuery" value="SELECT NOW() FROM DUAL" /> </bean> <!--配置sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--數據庫配置--> <property name="dataSource" ref="dataSource" /> <!--別名就是mapper.xml中,不用謝類的全稱,直接寫 --> <property name="typeAliasesPackage" value="com.daydayhave.ssm.domain,com.daydayhave.ssm.query" /> <!--映射文件--> <property name="mapperLocations" value="classpath:com/daydayhave/ssm/mapper/*Mapper.xml" /> </bean> <!--掃描一個特定包,給該包下面全部的接口都產生一個以上的配置就ok 掃描後 框架 會給這個包下的全部的mapper接口生成實現--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.daydayhave.ssm.mapper" /> </bean> </beans>
6. jdbc.propertiesapache
jdbc.driverClassName = com.mysql.jdbc.Driver
#company
#house
jdbc.url=jdbc:mysql:///testssm
jdbc.username=z2
jdbc.password=123456
2.1 domain.javajson
package com.daydayhave.ssm.domain; public class Department { private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Department() { } public Department(String name) { this.name = name; } }
2.2 DepartmentMapper.java
package com.daydayhave.ssm.mapper; import com.daydayhave.ssm.domain.Department; import java.util.List; public interface DepartmentMapper { List<Department> loadAll(); void save(Department department); }
2.3 DepartmentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.daydayhave.ssm.mapper.DepartmentMapper" > <!-- List<Department> loadAll(); void save(Department department); --> <select id="loadAll" resultType="Department"> SELECT * from t_department </select> <insert id="save" parameterType="Department"> INSERT INTO t_department(name) values(#{name}) </insert> </mapper>
2.4 IDepartmentService.java
package com.daydayhave.ssm.service; import com.daydayhave.ssm.domain.Department; import java.util.List; public interface IDepartmentService { List<Department> getAll(); void add(Department department); }
2.5 DepartmentServiceImpl.java
package com.daydayhave.ssm.service.impl; import com.daydayhave.ssm.domain.Department; import com.daydayhave.ssm.mapper.DepartmentMapper; import com.daydayhave.ssm.service.IDepartmentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service public class DepartmentServiceImpl implements IDepartmentService { @Autowired private DepartmentMapper departmentMapper; @Override public List<Department> getAll() { return departmentMapper.loadAll(); } //寫事務:須要一個事務,而且不是隻讀 //@Transactional(propagation = Propagation.REQUIRED,readOnly = false) @Transactional//默認就是寫事務 @Override public void add(Department department) { departmentMapper.save(department); //int i = 1/0; } }
3.1 baseTest.java
package com.daydayhave.ssm; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class BaseTest { }
3.2 DepartmentServiceImplTest.java
package com.daydayhave.ssm.service.impl; import com.daydayhave.ssm.BaseTest; import com.daydayhave.ssm.domain.Department; import com.daydayhave.ssm.mapper.DepartmentMapper; import com.daydayhave.ssm.service.IDepartmentService; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import java.util.List; import static org.junit.Assert.*; public class DepartmentServiceImplTest extends BaseTest{ @Autowired IDepartmentService departmentService; @Autowired DepartmentMapper departmentMapper; @Test public void getAll() { List<Department> departments = departmentService.getAll(); System.out.println(departments); } @Test public void add() { System.out.println(departmentMapper); } }
4.1 DepartmentController.java
package com.daydayhave.ssm.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/department") public class DepartmentController { //跳轉管理頁面 @RequestMapping("/index") public String index(){ return "department/index"; } }