第一次接觸這3大框架,打算一個一個慢慢學,參照網上資料搭建了一個ssm項目,做爲新手吃虧在jar包的導入上,好比jdbc DataSource配置的時候因爲導入的jar包不兼容或者缺包致使項目沒法正常運行,commons-dbcp-1.4.jar和commons-pool-1.6.jar導進來以後就解決了,可是這個問題就搞了一個晚上。html
控制器依賴服務層的接口不依賴具體的實現,服務層依賴dao接口層不依賴具體數據訪問層。數據持久層使用mybatis框架來完成CRUD操做。config文件下存放因此實體的映射XML文件,一個dao接口對於一個xml實現,sql語句都寫在XML文件中。前端
以前寫的項目基本都是這樣的架構:分層、依賴抽象。spring框架在這裏的做用就是一個容器,它管理着項目裏的具體實現:服務層的具體實現,數據訪問層的具體實現,當controller想要某個實現spring容器就會注入進具體的service。spring還有一個做用就是AOP編程,這塊暫時還沒學到先無論它。java
springmvc框架在項目裏做用就是處理用戶請求,用戶輸入一個url(http://localhost:8080/ssm/employee/list)springmvc會根據url去找對應的controller方法,並返回一個view(jsp頁面)給用戶。mysql
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!--spirng 容器配置 --> <!--ContextLoaderListener監聽器會在網站啓動時初始化Spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mybatis.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <!--springmvc配置 加載前端控制器 --> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 加載配置文件 默認加載規範: * 文件命名:servlet-name-servlet.xml====springmvc-servlet.xml * 路徑規範:必須在WEB-INF目錄下面 修改加載路徑: --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<?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:p="http://www.springframework.org/schema/p" 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-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- springmvc只負責頁面轉發 --> <!-- 只掃描控制器 --> <context:component-scan base-package="com.ssmdemo.controller"></context:component-scan> <!-- 配置一個註解驅動,若是配置此標籤,那麼就能夠不用配置處理器映射器和處理器適配器 --> <mvc:annotation-driven /> <!-- 處理靜態資源 --> <mvc:default-servlet-handler /> <!-- 配置視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"> <!-- spring但願管理全部的邏輯組件。。等 --> <context:component-scan base-package="com.ssmdemo"> <context:exclude-filter type="annotation" expression="org.springframework.web.servlet.mvc.Controller" /> </context:component-scan> <!-- 加載配置文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 數據庫鏈接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="10" /> <property name="maxIdle" value="5" /> </bean> <!-- transaction manager, use DataSourceTransactionManager" for JDBC local tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 開啓基於註解的事務 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- mapper配置 --> <!-- 讓spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 數據庫鏈接池 --> <property name="dataSource" ref="dataSource" /> <!-- 自動掃描mybatis配置文件,這裏就配置控制檯打印sql --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <!-- 制定mapper文件的位置 --> <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml" /> </bean> <!-- 配置Mapper掃描器 --> <!-- scan for mappers and let them be autowired --> <mybatis:scan base-package="com.ssmdemo.dao" /> </beans>
數據庫鏈接字符串反正db.properties文件內web
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/mybatis
jdbc.username=root
jdbc.password=123456
下面是 mybatis-config.xml的配置,用來在控制檯打印mybatis執行的sql語句
<?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="logImpl" value="STDOUT_LOGGING" /> </settings> </configuration>
config下的EmployeeMapper.xml配置spring
<?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.ssmdemo.dao.EmployeeMapper"> <select id="getEmployeeList" resultType="com.ssmdemo.model.Employee"> select * from employee </select> </mapper>
@Controller @RequestMapping("/employee") public class EmployeeController { @Autowired private EmployeeService employeeService; @RequestMapping("/list") public ModelAndView list() { ModelAndView mv = new ModelAndView(); List<Employee> emps = employeeService.getEmployees(); mv.setViewName("employee/list"); mv.addObject("employees", emps); return mv; } }
public interface EmployeeService { List<Employee> getEmployees(); }
@Service public class EmployeeServiceImpl implements EmployeeService { @Autowired EmployeeMapper employeeMapper; @Override public List<Employee> getEmployees() { return employeeMapper.getEmployeeList(); } }
public interface EmployeeMapper { List<Employee> getEmployeeList(); }
public class Employee { @Override public String toString() { return "Employee [employeeId=" + employeeId + ", name=" + name + ", age=" + age + ", birthday=" + birthday + "]"; } private int employeeId; private String name; private int age; private Date birthday; public int getEmployeeId() { return employeeId; } public void setEmployeeId(int employeeId) { this.employeeId = employeeId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>ssm整合demo</title> </head> <body> <c:forEach items="${employees}" var="item"> 姓名: <c:out value="${item.name}" /><br/> </c:forEach> </body> </html>