使用Springmvc和mybatis完成商品列表的查询
- 表现层:springmvc
- 业务层:service接口
- 持久层:mybatis
- mysql数据库
- spring的作用
spring将各层进行整合,通过spring管理持久层的mapper(相当于dao接口)
通过spring管理业务层service,service中可以调用mapper接口,spring进行事物控制
通过Spring管理表现层handler,handler中可以调用service接口
mapper,service,handler都是javabean
整合dao
- mybatis和spring进行整合
SqlMapConfig.xml
<configuration>
<!-- 和Spring整合后,使用mapper扫描器,这里不需要配置了 -->
<!-- 全局setting -->
<!-- 配置别名 -->
<typeAliases>
<package name="com.shagou.ssm.po"/>
</typeAliases>
</configuration>
applicationContext.xml
<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}" />
</bean>
<!-- sqlSessionFactory -->
<bean id ="sqlSessionFactory" class ="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value ="classpath:mybatis/SqlMapConfig.xml"/>
<property name="dataSource" ref ="dataSource"/>
</bean>
<bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name = "basePackage" value = "com.shagou.ssm.mapper"/>
<property name ="sqlSessionFactoryBeanName" value ="sqlSessionFactory"/>
</bean>
逆向工程生成po类及mapper(单表的增删改查)
- 手动定义商品查询的mapper
- 针对综合查询mapper,一般情况都会有关联查询,建议自定义mapper
mapper.xml
<mapper namespace="com.shagou.ssm.mapper.ItemsMapperCustom">
<!-- 定义商品查询的sql片段 -->
<sql id = "query_items_where">
<!-- 动态sql,通过if判断,满足条件进行sql拼接 -->
<if test = "itemsCustom!=null">
<if test = "itemsCustom.name!=null and itemsCustom.name!=''">
items.name LIKE '%${itemsCustom.name}%'
</if>
</if>
</sql>
<!-- 商品列表查询
parameterType传入包装对象(查询条件)
resultType建议使用扩展对象
-->
<select id= "findItemsList" parameterType = "com.shagou.ssm.po.ItemsQueryVo" resultType = "com.shagou.ssm.po.ItemsCustom">
SELECT items.* FROM items
<where>
<include refid = "query_items_where"></include>
</where>
</select>
</mapper>
ItemsCustom作为商品信息的扩展类
public class ItemsCustom extends Items{
//商品信息的扩展类
}
对商品数据库的字段进行操作时,通过在扩展类ItemsCustom中添加
ItemsQueryVo封装的pojo
public class ItemsQueryVo {
//商品信息包装
private Items items;
//对原始po进行扩展
private ItemsCustom itemsCustom;
public Items getItems() {
return items;
}
public void setItems(Items items) {
this.items = items;
}
public ItemsCustom getItemsCustom() {
return itemsCustom;
}
public void setItemsCustom(ItemsCustom itemsCustom) {
this.itemsCustom = itemsCustom;
}
}
将商品信息与商品信息的扩展类封装到ItemsQueryVo中作为resultType输出类型
接口
public interface ItemsMapperCustom {
//商品查询列表
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;
}
整合service
让spring管理service接口
- 定义service接口
public interface ItemsService {
//商品管理Service
//商品查询列表
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;
}
用来对商品列表进行查询
- 在Spring中配置Service
创建applicationContext-service.xml
<!-- service -->
<bean id ="itemsService" class = "com.shagou.ssm.service.impl.ItemsServiceImpl"></bean>
创建事务控制
applicationContext-transaction.xml
<!-- 事务管理器
对mybatis操作数据库事务控制,Spring使用jdbc的事务控制类
-->
<bean id ="transactionManager" class ="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name = "dataSource" ref = "dataSource"></property>
</bean>
<!-- 通知 -->
<tx:advice id = "txAdvice" transaction-manager = "transactionManager">
<tx:attributes>
<tx:method name = "save*" propagation = "REQUIRED"/>
<tx:method name = "delete*" propagation = "REQUIRED"/>
<tx:method name = "insert*" propagation = "REQUIRED"/>
<tx:method name = "update*" propagation = "REQUIRED"/>
<tx:method name = "find*" propagation = "SUPPORTS" read-only = "true"/>
</tx:attributes>
</tx:advice>
<!-- aop -->
<aop:config>
<aop:advisor advice-ref = "txAdvice" pointcut = "execution(* com.shagou.ssm.service.impl.*.*(..))"/>
</aop:config>
整合springmvc,配置处理器映射器处理器适配器,视图解析器
springmvc.xml
<context:component-scan base-package="com.shagou.ssm.controller"></context:component-scan>
<mvc:annotation-driven ></mvc:annotation-driven>
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp路径的前缀和后缀 -->
<property name="prefix" value = "/WEB-INF/jsp/"></property>
<property name="suffix" value = ".jsp"></property>
</bean>
配置前端控制器
<!-- springmvc前端控制器 -->
<servlet>
<servlet-name>springmvc_mybatis</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfiglocation配置springmvc加载的配置文件(配置处理器映射器,适配器等等)
如果不配置contextConfiglocation,默认加载的是/WEB-INF/servlet名称-servlet.xml(springmvc-servlet.xml)
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc_mybatis</servlet-name>
<!--
第一种:*.action 访问.action结尾由DispatcherServlet进行解析
第二种:/ 所有访问地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置不让DispatcherServlet进行解析
使用此种方式可以实现RESTful风格的url
第三种:/* 这样配置不对,使用这种配置,最终要转发到一个jsp页面时,仍然会由DispatcherServlet解析jsp地址,不能根据jsp页面找到Handler,会报错
-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
加载spring容器将mapper.service.controller加载到spring容器中
<context:component-scan base-package="com.shagou.ssm.controller"></context:component-scan>
<mvc:annotation-driven ></mvc:annotation-driven>
在web.xml中添加spring容器监听器
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>