SpringMVC本身是Spring的组件,Spring整合Mybatis即把Mybatis框架的核心类交给Spring管理(IOC)
如Mybatis的SqlsessionFactory、Sqlsession和核心配置文件(四大金刚)
新建dynamic web project,修改默认输出的class路径,修改content directory名,勾选生成web.xml
部署Tomcat
导包:Spring、Spring测试、Servlet和jsp、数据库、Mybatis、Spring和Mybatis的整合包
resources下创建核心配置文件applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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.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
">
<bean id="date" class="java.util.Date">bean>
beans>
测试能否正常使用核心配置文件中的bean
@ContextConfiguration("classpath:applicationContext.xml") // 加载核心配置文件
@RunWith(SpringJUnit4ClassRunner.class)// 使用spring的测试
public class SpringTest{
@Autowired
private Date date;
@Test
public void testName() throws Exception {
System.out.println(date);
}
}
数据库1112下创建dept表
准备属性文件,并交给Spring管理 - (原来是放在mybatis-config中然后由Resources读取)
db.properties
db.username=root
db.password=root
db.url=jdbc:mysql:///1112
db.dirverClassName=com.mysql.jdbc.Driver
applicationContext.xml
<context:property-placeholder location="classpath:db.properties"
system-properties-mode="NEVER" />
通过连接池获取DataSource数据源
applicationContext.xml
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="${db.username}">property>
<property name="password" value="${db.password}">property>
<property name="url" value="${db.url}">property>
<property name="driverClassName" value="${db.dirverClassName}">property>
bean>
测试:SpringTest
@Autowired
private BasicDataSource dataSource;
@Test
public void testName() throws Exception {
System.out.println(dataSource.getConnection());
}
domain下新建Emp
代码略
mapper包下新建接口和映射文件
EmpMapper
public interface EmpMapper {
/**
* 查询所有
*/
List<Emp> findAll();
}
EmpMapper.xml
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.ming.mapper.EmpMapper">
<select id="findAll" resultType="cn.ming.domain.Emp">
select *from emp
select>
mapper>
applicationContext.xml
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="typeAliasesPackage" value="cn.ming.domain">property>
<property name="mapperLocations" value="classpath:cn/ming/mapper/*Mapper.xml">property>
bean>
测试:SpringTest
@Autowired
private SqlSessionFactory factory;
@Test
public void testName() throws Exception {
System.out.println(factory);
}
applicationContext.xml
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.ming.mapper">property>
bean>
测试:SpringTest
@Autowired
private EmpMapper mapper;// 代理对象
@Test
public void testName() throws Exception {
List<Emp> list = mapper.findAll();
list.forEach(System.out::println);
}
service包下新建IEmpService
public interface IEmpService {
List<Emp> findAll();
}
service.impl包下新建EmpServiceImpl,加上Service注解
@Service
public class EmpServiceImpl implements IEmpService {
@Autowired
private EmpMapper mapper;
@Override
public List<Emp> findAll() {
// TODO Auto-generated method stub
return mapper.findAll();
}
}
applicationContext.xml
<context:component-scan base-package="cn.ming.service">context:component-scan>
测试:SpringTest
@Autowired
private IEmpService service;
@Test
public void testName() throws Exception {
List<Emp> list = service.findAll();
list.forEach(System.out::println);
}
<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>day41_ssmdisplay-name>
<servlet>
<servlet-name>dispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springMVC.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>dispatcherServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<filter>
<filter-name>characterEncodingfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterEncodingfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.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 base-package="cn.ming.controller" />
<mvc:default-servlet-handler/>
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/">property>
<property name="suffix" value=".jsp">property>
bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>#{1024*1024*20}value>
property>
bean>
beans>
@Controller
public class EmpController {
@Autowired
private IEmpService service;
@RequestMapping("/findAll")
@ResponseBody // 返回json格式数据 不走视图解析器
public List<Emp> findAll(){
return service.findAll();
}
}
原因:web容器启动时只加载了spring-mvc.xml,并没有加载applicationContext.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
见工程
略
略
查询所有:查询所有后把数据放到request域中,jsp页面取值展示
删除:删除后重定向到查询所有
添加:跳往后端goSave,如果id为空跳转save页面,填值后跳往后端save,对象接收参数,如果没有隐藏域id就添加然后重定向到查询所有
修改:跳往后端goSave,如果id有值,查询此条数据,向request域传值并跳转save页面,save页面取值回显,改数据后跳往后端save,有隐藏域id就修改然后重定向到查询所有
略
mapper.xml文件可以放到resources同路径目录下,编译后是一样的
相对路径与绝对路径
<a href="del?id=${dept.id }">删除a>
<a href="/dept/del?id=${dept.id }">删除a>
A标签都往后端跳,后端跳转页面
添加与修改都是跳往后端同一个接口,然后跳转页面
略
interceptor包下新建MyInterceptor
public class MyInterceptor implements HandlerInterceptor {
// 到达处理器之前执行
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("进入拦截器了....");
return false;
}
// 处理器处理之后执行
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
}
// 响应到浏览器之前执行
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
}
}
springMVC.xml
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/dept/*"/>
<bean class="cn.ming.interceptor.MyInterceptor" />
mvc:interceptor>
mvc:interceptors>