SSM:Spring+SpringMvc+Mybatis整合。
springmvc是用来处理用户请求和页面显示的,mybatis是用来访问数据库保证持久性的,spring是用来管理springmvc和mybatis中对象的。
SSM整合dbcp数据源的方式:
步骤:
(1)SSM整合的导包(关于数据源是commons-dbcp、commons-pool)
(2)配置文件
db.properties:
jdbc.url=jdbc:mysql:///springmvc
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.username=root jdbc.password=mysqlapplicationCOntext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" 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-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "> <!-- c3p0连接池 --> <context:property-placeholder location="classpath:db.properties"/> <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="url" value="${jdbc.url}"></property> <property name="driverClassName" value="${jdbc.driverClassName}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 配置Mybatis的工厂 --> <bean name="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:sqlMapConfig.xml"></property> </bean> <!-- 直接扫描Mapper接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.hu.dao"></property> </bean>//SSM关于事务的配置
<!-- 配置Mybatis的工厂 --> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 事务管理的注解开启 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>//当然要创建相应的Mapper包(Mapper接口、Mapper.xml):cn.hu.dao
springmvc.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: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"> <!-- controller层,service层的扫描,包括子包 --> <context:component-scan base-package="cn.hu"></context:component-scan> <!-- 三大组件 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 视图解析器前缀和后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean></beans>
sqlMapConfig.xml:
<?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> <!-- 设置别名,避免映射配置文件中类要写全名的情况 --> <typeAliases> <package name="cn.hu.pojo"/> </typeAliases> </configuration>web.xml:<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 配置一启动服务器就监听spring创建的监听器及初始化参数 -->//目的是可以加载spring的配置文件 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <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>*.action</url-pattern> </servlet-mapping> <filter>//编码过滤器 <filter-name>encoding</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> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> 通过逆向工程得到需要的pojo、Mapper接口、Mapper.xml。构建需要的Service、Controller。Controller:
@RequestMapping(value="/item")
public class ItemController { @Autowired//自动注入 private ItemService service; //查询商品列表 @RequestMapping(value="/itemList.action") public ModelAndView itemList(){ List<Items> list = service.selectItemList(); ModelAndView mav = new ModelAndView(); mav.addObject("itemList", list); mav.setViewName("itemList"); return mav; }Service:
public class ItemServiceImpl implements ItemService{ @Autowired private ItemsMapper mapper;//得到商品列表
public List<Items> selectItemList() { return mapper.selectByExample(null); }Mapper接口中的sql实现都在对应的Mapper.xml文件中,可以去具体的查看,有需求的时候可以直接修改里面的sql语句。
SSM整合druid数据源方式:
这次使用的数据库连接数据源是druid。所以需要在SSM整合包中添加druid包。下面介绍整合后的SSM小项目。
导入SSM整合包。配置总体的配置文件,加入日志文件。
SSM总体配置文件:
(1)web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 配置spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value>//实现了多spring配置文件分开写 </context-param> <!-- 配置监听器加载spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置过滤器,解决post的乱码问题 --> <filter> <filter-name>encoding</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> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置SpringMVC --> <servlet> <servlet-name>boot-crm</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> <!-- 配置springmvc什么时候启动,参数必须为整数 --> <!-- 如果为0或者大于0,则springMVC随着容器启动而启动 --> <!-- 如果小于0,则在第一次请求进来的时候启动 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>boot-crm</servlet-name> <!-- 所有的请求都进入springMVC --> <url-pattern>/</url-pattern>//springmvc拦截的路径是/,就是把静态资源也会拦截,所以springmvc.xml可以设置一下对静态资源可见 </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>(2)sqlMapConfig.xml文件
<?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> <!-- 别名 --> <typeAliases> <package name="com.itheima.crm.pojo"/> </typeAliases> </configuration>(3)springmvc.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: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"> <!-- 配置Controller扫描 --> <context:component-scan base-package="com.itheima.crm.controller" /> <context:property-placeholder location="classpath:resource.properties" />-------->需要使用资源文件的时候添加 <!-- 配置注解驱动 --> <mvc:annotation-driven /> <!-- 对静态资源放行 -->------------------------------------->配置不会拦截静态资源 <mvc:resources location="/css/" mapping="/css/**"/> <mvc:resources location="/js/" mapping="/js/**"/> <mvc:resources location="/fonts/" mapping="/fonts/**"/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 后缀 --> <property name="suffix" value=".jsp" /> </bean> </beans>(4)applicationContext-dao.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 配置 读取properties文件 jdbc.properties --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 配置 数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 配置SqlSessionFactory --> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 设置MyBatis核心配置文件 --> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /> <!-- 设置数据源 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置Mapper扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 设置Mapper扫描包 --> <property name="basePackage" value="com.itheima.crm.mapper" /> </bean> </beans>applicationContext-service.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 配置Service扫描 --> <context:component-scan base-package="com.itheima.crm.service" /> </beans>jdbc.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/crm?characterEncoding=utf-8 jdbc.username=root jdbc.password=mysql关于静态资源:
因为在web.xml中配置的springmvc拦截的路径是/(全部),所以会把静态资源也进行拦截,但是不应该拦截他们,所以在springmvc.xml中添加对静态资源的可访问。
如果在web.xml中配置的springmvc拦截的路径是*.action,那么就不会拦截静态资源,在springmvc.xml中就不用配置关于静态资源了。
关于资源文件:
(1)resource.properties:
fromType.code=002
(2)springmvc.xml中加载资源文件:
<context:property-placeholder location="classpath:resource.properties" />
(3)在Controller中使用资源文件
public class CustomerController {@Autowired
private BaseDictService baseDictService; @Autowired private CustomerService customerService; //注解在成员变量上 @Value("${fromType.code}") private String fromTypeCode;}
项目知识点:
(1)需要在页面上显示的数据是来自于Controller的
第一种方式:
ModelAndView mav = new ModelAndView();
mav.addObject("itemList",list); mav.setViewName("itemList");第二种方式:用默认的Model参数
@RequestMapping(value="/test.action")
public String get(Model model){ model.addAttribute("test1","test1" ); model.addAttribute("test2","test2" ); model.addAttribute("test3","test3" ); return "editItem"; }页面显示:
<h3>${test1}</h3>
<h3>${test2}</h3> <h3>${test3}</h3>