通过对SpringMvc的入门程序简单的学习,接下来就学习SpringMvc整合mybatis。
整合目标:控制层采用springmvc、持久层使用mybatis实现。
实现商品查询列表,从mysql数据库查询商品信息。
1.jar包
包括:spring(包括springmvc)、mybatis、mybatis-spring整合包、数据库驱动、第三方连接池。
2.ssm整合思路
1.Dao层
pojo和映射文件以及接口使用逆向工程生成
SqlMapConfig.xml 和 mybatis核心配置文件
ApplicationContext-dao.xml 整合后spring在dao层的配置,包括:数据源、会话工厂、扫描Mapper
2. service层
事务:ApplicationContext-trans.xml
@Service注解扫描:ApplicationContext-service.xml
3. controller层
SpringMvc.xml
注解扫描:扫描@Controller注解
注解驱动:替我们显示的配置了最新版的处理器映射器和处理器适配器
视图解析器:显示的配置是为了在controller中不用每个方法都写页面的全路径
4. web.xml
springMvc:前端控制器配置
spring监听
配置文件
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>
</configuration>
ApplicationContext.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">
<!-- 加载配置文件 -->
<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>
<!-- 整合后会话工厂归spring管理 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定mybatis核心配置文件
spring配置:要加classpath
-->
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
<!-- 指定会话工厂使用的数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置原生Dao实现 class必须指定dao实现类的全路径名 -->
<bean id="userDao" class="com.dml.dao.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!-- mapper接口代理的实现 -->
<!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
配置mapper接口的全路径名称
<property name="mapperInterface" value="com.dml.mapper.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean> -->
<!-- 使用包扫描的方式批量引入Mapper
扫描后引用的时候可以使用类名,首字母小写.
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定要扫描的包全路径名称,如果有多个包用英文状态下的逗号分隔 -->
<property name="basePackage" value="com.dml.mapper"></property>
</bean>
</beans>
注意修改自己的dao配置路径和扫描包的包名
这里的db.properties文件请参照Mybatis整合spring
文章中的配置,注意修改数据库的用户名和密码
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.dml.service"></context:component-scan>
</beans>
这里注意修改扫描包的包名
ApplicationContext-trans.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">
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.dml.service.*.*(..))" />
</aop:config>
</beans>
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:dubbo="http://code.alibabatech.com/schema/dubbo" 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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.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.dml.controller"></context:component-scan>
<!-- 注解驱动:
替我们显示的配置了最新版的注解的处理器映射器和处理器适配器 -->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<!-- 配置视图解析器
作用:在controller中指定页面路径的时候就不用写页面的完整路径名称了,可以直接写页面去掉扩展名的名称
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 真正的页面路径 = 前缀 + 去掉后缀名的页面名称 + 后缀 -->
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置自定义转换器
注意: 一定要将自定义的转换器配置到注解驱动上
-->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<!-- 指定自定义转换器的全路径名称 -->
<bean class="com.dml.controller.converter.CustomGlobalStrToDateConverter"/>
</set>
</property>
</bean>
</beans>
注意修改扫描的包名和自定义转换器的全路径名
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ssm</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 加载spring容器,监听 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationContext-*.xml</param-value>
</context-param>
<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>
<param-value>classpath:SpringMvc.xml</param-value>
</init-param>
<!-- 在tomact启动的时候加载这个servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 配置过滤器解决post请求乱码 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
ItemsServiceImpl.java
@Service
public class ItemsServiceImpl implements ItemsService{
@Autowired
private ItemsMapper itemsMapper;
//查询列表,全部的数据
@Override
public List<Items> list() throws Exception {
//如果不需要任何查询条件,直接将ItemsExample对象new出来即可
ItemsExample example = new ItemsExample();
List<Items> list = itemsMapper.selectByExampleWithBLOBs(example);
return list;
}
//通过id查询商品对象
@Override
public Items findItemsById(Integer id) throws Exception {
Items items = itemsMapper.selectByPrimaryKey(id);
return items;
}
//更新商品信息
@Override
public void updateitem(Items items) throws Exception {
itemsMapper.updateByPrimaryKeyWithBLOBs(items);
}
}
ItemsController.java
@Controller
public class ItemsController {
//@Autowired自动注入,但是有多个实现类时就不知道注入哪个了
// @Resource(name="itemsService")
@Autowired
private ItemsService itemsService;
//指定url到请求方法的映射
//url中输入一个地址,找到这个方法,例如:localhost:8080/springmvc/list.action
@RequestMapping("/list")
public ModelAndView itemsList() throws Exception{
//调用service查询列表
List<Items> itemList = itemsService.list();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList",itemList);
//设置页面路径
modelAndView.setViewName("itemList");
return modelAndView;
}
/**
* 通过id查询商品对象
* springmvc中默认支持的参数类型:也就是说在controller方法中可以加入这些,也可以不加
* HttpServletRequest
* HttpServletResponse
* HttpSession
* Model
*/
@RequestMapping("/itemEdit")
public String itemEdit(HttpServletRequest request,HttpServletResponse response,
HttpSession session,Model model) throws Exception{
String idStr = request.getParameter("id");
Integer id = Integer.parseInt(idStr);
Items items = itemsService.findItemsById(id);
//Model模型:模型中放入了返回给页面的数据
//Model底层其实就是用的request域来传递数据,但是对request域进行了扩展
model.addAttribute("item",items);
//如果springmvc方法返回一个简单的string字符串,那么springmvc就会认为这个字符串就是页面的名称
return "editItem";
}
//更新商品信息
//springmvc可以直接接收基本数据类型,包括string,springmvc可以帮你自动进行类型转换
//controller方法接收的参数的变量名称必须要等于页面上input框的name属性值
// public String update(Integer id,String name,Float price,String detail) throws Exception{
//springmvc可以直接接收pojo类型:要求页面上input框的name属性名称必须等于pojo属性名称
@RequestMapping("/updateitem")
public String update(Items items) throws Exception{
// items.setCreatetime(new Date());
itemsService.updateitem(items);
return "success";
}
//高级查询
//如果Controller中接收的是Vo,那么页面上input框的name属性值要等于vo的属性.属性.属性
@RequestMapping("/search")
public String search(QueryVo vo) throws Exception{
System.out.println(vo);
return "success";
}
}
pojo和映射文件以及接口使用逆向工程生成,关于逆向工程
总结的很乱,想每天总结自己学习的内容,但是发现还是没有办法做到,东西多,很多也只是复制粘贴了老师的。总之还在努力吧。