一、SSM三大框架整合-配置
1、创建项目,导包
2、配置web.xml:配置springmvc前端控制器、读取spring配置文件、设置页面拦截规则
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ssm_project_springmvc</display-name>
<!-- 配置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:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<!-- 拦截规则 -->
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<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>
</web-app>
3、数据库配置文件db.properties、spring配置文件applicationContext.xml
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm
jdbc.user=root
jdbc.password=root
数据库配置连接、注解扫描、事务(事务核心管理器、注解事务)、视图解析器、Mybatis、Mapper动态扫描。
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 读取db.properties文件信息 -->
<context:property-placeholder location="db.properties"/>
<!-- 配置连接池 -->
<!-- 配置c3p0连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.dunka"/>
<!-- 事务核心管理器 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置Mybatis -->
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 配置别名,就不用另起炉灶太麻烦了 -->
<property name="typeAliasesPackage" value="com.dunka.bean"/>
</bean>
<!-- Mapper工厂 mapper动态代理开发 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dunka.mapper"/>
</bean>
</beans>
4、准备测试用例,以上篇的例子为数据。
Mapper:
/**
*
* @author Dunka
* @Time 2019年2月28日
* @Todo ItemMapper
*/
public interface ItemMapper {
//查询所有游戏
public List<ItemInfo> selectAll();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dunka.mapper.ItemMapper">
<select id="selectAll" resultType="ItemInfo">
select * from item_info
</select>
</mapper>
Service:
public interface ItemService {
//查询所有游戏
public List<ItemInfo> selectAll();
}
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private ItemMapper mapper;
@Override
public List<ItemInfo> selectAll() {
return mapper.selectAll();
}
}
Controller:
@Controller
@RequestMapping("/item/")
public class ItemController {
@Autowired
private ItemService service;
@RequestMapping("allList.do")
public ModelAndView list() {
ModelAndView mav = new ModelAndView();
//查询
List<ItemInfo> itemList = service.selectAll();
//将结果赋值给ModelAndView
mav.addObject("itemList", itemList);
//将视图名设置
mav.setViewName("item_list");
return mav;
}
}
Tomcat跑起来,ok。
二、参数绑定
1、默认参数绑定
//默认参数绑定
@RequestMapping("select.do")
public String select(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model model) {
//获取参数
String id = request.getParameter("id");
//查询
ItemInfo itemInfo = service.selectItemInfoById(id);
List<ItemInfo> itemList = new ArrayList<ItemInfo>();
itemList.add(itemInfo);
//传回对象
model.addAttribute("itemList", itemList);
//设置视图名
return "item_list";
}
2、基本数据类型参数绑定
//基本类型 参数绑定
@RequestMapping("delete.do")
//public String delete(@RequestParam(value="id",required=false,defaultValue="5")String itemId) {
public String delete(String id) {
System.out.println("delete id:"+id);
//删除
service.deleteById(id);
//重定向
return "redirect:allList.do";
}
3、bean对象参数绑定
//bean对象 参数绑定
@RequestMapping("save.do")
public String save(ItemInfo itemInfo) {
System.out.println(itemInfo);
//保存对象
service.save(itemInfo);
//重定向
return "redirect:allList.do";
}
4、包装类参数绑定
//包装类 参数绑定
@RequestMapping("selectByVo.do")
public String selectByVo(ItemInfoVo vo, Model model) {
System.out.println("ItemInfoVo:"+vo.getItemInfo());
//获取到对象参数
List<ItemInfo> itemList = service.selectByVo(vo);
//查询
model.addAttribute("itemList", itemList);
//设置视图名
return "item_list";
}
5、解决参数绑定中post提交方法中的“中文乱码”问题--web.xml
<!-- 拦截器 解决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>*.do</url-pattern>
</filter-mapping>