1.web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--spring配置文件路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</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>*.action</url-pattern>
</filter-mapping>
<!-- 前端控制器 -->
<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>
</web-app>
2.applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.className}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="10"/>
<property name="minIdle" value="5"/>
</bean>
<!--配置mybatis工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:sqlMapConfig"/>
</bean>
<!--Mapper动态代理扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--自动扫描dao下面的配置文件-->
<property name="basePackage" value="springmvc.dao"/>
</bean>
<!--注解事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--context:component-scan>元素会扫描指定的包及其所有的子包,并查找出能够自动注册为Spring Bean的类。
默认情况下<context:component-scan>查找使用构造型注解所标注的类,这些特殊的注解如下:
@Component 通用的构造型注解,标识该类为Spring组件()
@Controller 标识将该类定义为Spring MVC controller(控制层)
@Repository 标识将该类定义为数据仓库(持久层)
@Service 标识将该类定义为服务(服务层,也就是业务层)
扫描controller @Service 扫描controller文件-->
<context:component-scan base-package="springmvc"/>
<!--<!–处理映射器–>-->
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>-->
<!--<!–处理适配器–>-->
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>-->
<!---->
<!--注解驱动-->
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/>
<!--配置Convert转换器 转换工厂 (日期,去掉前后空格)-->
<bean id="conversionServiceFactoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!--配置多个转换器-->
<property name="converters">
<list>
<bean class="springmvc.conversion.DateConverter"/> //自定义类
</list>
</property>
</bean>
</beans>
4.ItemServiceImpl
package springmvc.service;
//查询商品信息
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springmvc.dao.ItemsMapper;
import springmvc.pojo.Items;
import java.util.List;
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private ItemsMapper itemsMapper;
//查询商品列表
public List<Items> selectItemList(){
return itemsMapper.selectByExampleWithBLOBs(null);
}
//查询单个商品
public Items selectItemsById(Integer id) {
return itemsMapper.selectByPrimaryKey(id);
}
//更新商品信息
public void updateItmeById(Items items) {
itemsMapper.updateByPrimaryById(items);
}
}
5.ItemController
@Controller
public class ItemController {
@Autowired //从容器中找到ItemService类型的对象并赋值
private ItemService itemService;
@RequestMapping(value = "/item/itemlist.action")
public ModelAndView itemController(){
// 从dao层查找
List<Items> items = itemService.selectItemList();
ModelAndView mav = new ModelAndView();
mav.addObject("itemList",items);
mav.setViewName("/WEB-INF/jsp/itemList.jsp");
return mav;
}
//去修改页面入参id
@RequestMapping(value = "/itemEdit.action")
public ModelAndView toEdit(Integer id ){
//servlet时代开发
/* String idStr = request.getParameter("id");
Integer id = Integer.parseInt(idStr);
*/
//查询一个商品
Items items = itemService.selectItemsById(id); //jsp url= ?id=#{item.id}传递过来的
ModelAndView mav = new ModelAndView();
mav.addObject("item",items);
mav.setViewName("/WEB-INF/jsp/editItem.jsp");
return mav;
}
//提交修改页面入参 为Itmes对象
@RequestMapping(value = "/updateitem.action")
// public ModelAndView updateItem(Items items){ //属性值与jsp中name一致就可以自动封装过来
public ModelAndView updateItem(QueryVo queryVo){
itemService.updateItmeById(queryVo.getItems());
ModelAndView mav = new ModelAndView();
mav.addObject("item",queryVo.getItems());
mav.setViewName("/WEB-INF/jsp/success.jsp");
return mav;
}
}
6.DateConverter
public class DateConverter implements Converter<String,Date> {
public Date convert(String source){
try {
if(source != null){
DateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//2019:01-01 12_22-14
return df.parse(source);
}
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}