第一次接触这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>