ssm
jar包
配置文件
applicationContext-dao
<?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>
<!-- mapper配置 -->
<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
</bean>
<!-- 配置Mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.huida.dao"/>
</bean>
</beans>
applicationContext-service
<?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.huida.service"/>
</beans>
applicationContext-trans
<?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>
<!-- 传播行为 -->
<!-- 以save开头就有事务 -->
<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.huida.service.*.*(..))"/>
</aop:config>
</beans>
剩下的dbproperties.xml springmvc.xml
SqlMapConfig.xml 与前边一样
ItemController
package com.huida.controller;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.huida.pojo.Items;
import com.huida.service.ItemService;
import com.huida.vo.QueryVo;
@Controller
//窄化请求映射
@RequestMapping("items")//此时后边的需要加/
public class ItemController {
@Autowired
private ItemService itemService;
@RequestMapping(value="/itemsList",method=RequestMethod.GET)
public ModelAndView getItemList(){
List<Items> list=itemService.getItemList();
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject(list);
modelAndView.setViewName("itemList");
return modelAndView;
}
@RequestMapping("itemEdit/{id}") // /{name}
public String findItemsById(@PathVariable("id") Integer id,HttpServletRequest request,HttpServletResponse response,Model model,HttpSession session){
/*//request取参数id
String strId=request.getParameter("id");
//解决get乱码
String username="张三";
username=request.getParameter(username).getBytes("ISO8859-1"),"utf-8");
Integer id=null;
if(strId!=null && !"".equals(strId)){
id=new Integer(strId);
}else{
return null;
}*/
Items item=itemService.findItemById(id);
//model.addAttribute("item",item);
request.setAttribute("item", item);
return "editItem";
//返回值为空时
//request.getRequestDispatcher("/WEB-INF/jsp/editItem.jsp").forward(request, response); 注意抛异常
//void 破坏了springmvc规则
}
@RequestMapping("updateitem")
public String updateitem(MultipartFile pictureFile,Items item,Model model,HttpServletRequest request) throws IllegalStateException, IOException{
//直接传Items item pojo
/*Items item=new Items();
Integer id,String name,Float price,String detail
item.setId(id);
item.setName(name);
item.setPrice(price);
item.setDetail(detail);
item.setCreatetime(new Date());*/
//获取图片路径
String fileStr = pictureFile.getOriginalFilename();
//使用随机生成的字符串+扩展名
String newFileName=UUID.randomUUID().toString()+fileStr.substring(fileStr.lastIndexOf("."));
//将图片保存到硬盘
pictureFile.transferTo(new File("D:\\daima\\"+newFileName));
//将图片保存到数据
item.setPic(newFileName);
itemService.updateItem(item);
return "success";
//forward:请求转发 浏览器url不发生改变,request域中的数据可以带到重定向的方法中
//重定向 ,url发生改变 request域中的数据不能带到重定向的方法中
//model.addAttribute("id", item.getId()); //request的加强
//return "forward:itemEdit.action"
//request.setAttribute("id", item.getId()); //会报空指针异常
//return "redirect:itemEdit.action";
}
@RequestMapping("search")
public String search(QueryVo vo){
//高级查询
System.out.println(vo);
return "";
}
@RequestMapping("delAll")
public String delAll(QueryVo vo){
System.out.println(vo);
return null;
}
@RequestMapping("updateAll")
public String updateAll(QueryVo vo){
System.out.println(vo);
return null;
}
@RequestMapping("editItemSubmit_RequestJson")
public @ResponseBody Items editItemSubmit_RequestJson(@RequestBody Items item){
System.out.println(item);
return item;
}
}