SpringMVC教程IDEA版-3天-2018黑马SSM-03-笔记
https://www.bilibili.com/video/av4795324410.请求参数绑定自定义类型转换器16.获取Servlet原生的API17.RequestParam注解:18.RequestBody注解:body代表你提交的整个内容19.PathVariable注解:21.Request...
https://www.bilibili.com/video/av47953244
10.请求参数绑定
自定义类型转换器
16.获取Servlet原生的API
17.RequestParam注解:
18.RequestBody注解:
body代表你提交的整个内容
19.PathVariable注解:
21.RequestHeader
23.ModelAttribute注解
24.SessionAttribute
26.
27.响应返回值是void类型
默认会返回一个testVoid值,跳转到testVoid.jsp页面,类似于返回success情况
28.响应返回值是ModelAndView类型
29.forward和redirect
return为固定写法,forward代表的是转发,redirect代表重定向,后面接要跳转的页面的路径
就是说想要实现重定向或者转发,只需要return的一个想要跳转的路径字符串即可
30.响应json数据之过滤静态资源
文件上传:
springmvc方式上传原理
40.
42.SpringMVC拦截器
预处理,controller方法执行前
后处理,controller方法执行后页面跳转前
页面跳转前
springmvc.xml文件配置拦截器
==========================================================================================
46.ssm整合
配置spring:
1.项目的pom.xml文件中引入坐标依赖
2.main目录下面创建java.itcast文件夹,创建resources文件夹,右键Mark Directory as第一个、第三个
3.resources文件夹下创建applicationContext.xml
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/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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启注解的扫描,希望处理service和dao,controller不需要Spring框架去处理-->
<context:component-scan base-package="cn.itcast" >
<!--配置哪些注解不扫描-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!--Spring整合MyBatis框架-->
<!--配置连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///ssm"/>
<property name="user" value="root"/>
<property name="password" value="数据库密码"/>
</bean>
<!--配置SqlSessionFactory工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<!--配置AccountDao接口所在包-->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itcast.dao"/>
</bean>
<!--配置Spring框架声明式事务管理-->
<!--配置事务管理器-->
<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="find*" read-only="true"/>
<tx:method name="*" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!--配置AOP增强-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.itcast.service.impl.*ServiceImpl.*(..))"/>
</aop:config>
</beans>
4.java文件夹下面新建domain文件夹,新建Account实体类,编写和数据库表属性一样的属性,创建set、get方法,创建toString方法
5.java文件夹下面新建dao文件夹,新建AccountDao接口,创建想要实现的功能的方法
6.java文件夹下面新建service文件夹,编写AccountService接口,创建impl文件夹,新建AccountServiceImpl实现类,实现AccountService接口,类上添加@Service("accountService")
---------------------------------------------------------------------------------------------------------------------------------------------------------
配置springmvc:
1. 在web.xml中配置DispatcherServlet前端控制器
<!-- 配置前端控制器:服务器启动必须加载,需要加载springmvc.xml配置文件 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 服务器启动的时候,让DispatcherServlet对象创建 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
2. 在web.xml中配置DispatcherServlet过滤器解决中文乱码
<!-- 配置解决中文乱码的过滤器 -->
<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>
3. 创建springmvc.xml的配置文件,编写配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描controller的注解,别的不扫描 -->
<context:component-scan base-package="cn.itcast">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置视图解析器 -->
<bean id="viewResolver
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- JSP文件所在的目录 -->
<property name="prefix" value="/WEB-INF/pages/" />
<!-- 文件的后缀名 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 设置静态资源不过滤 -->
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/images/" mapping="/images/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<!-- 开启对SpringMVC注解的支持 -->
<mvc:annotation-driven />
</beans>
4. 测试SpringMVC的框架搭建是否成功
1. 编写index.jsp和list.jsp编写,超链接
<a href="account/findAll">查询所有</a>
2. 创建AccountController类,编写方法,进行测试
package cn.itcast.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/account")
public class AccountController {
/**
* 查询所有的数据
* @return
*/
@RequestMapping("/findAll")
public String findAll() {
System.out.println("表现层:查询所有账户...");
return "list";
}
}
----------------------------------------------------------------------------------------------------------------------------
spring整合springmvc:
目的:在controller中能成功的调用service对象中的方法
1.在项目启动的时候就加载applicationContext.xml的配置文件,在web.xml中配置ContextLoaderListener监听器(该监听器只能加载WEB-INF目录下的applicationContext.xml的配置文件)
<!-- 配置Spring的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listenerclass>
</listener>
<!-- 配置加载类路径的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
2.在controller中注入service对象,调用service对象的方法进行测试
package cn.itcast.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.itcast.service.AccountService;
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accoutService;
/**
* 查询所有的数据
* @return
*/
@RequestMapping("/findAll")
public String findAll() {
System.out.println("表现层:查询所有账户...");
accoutService.findAll();
return "list";
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
MyBatis配置:
1.resources包下创建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>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///ssm"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 使用的是注解 -->
<mappers>
<!-- <mapper class="cn.itcast.dao.AccountDao"/> -->
<!-- 该包下所有的dao接口都可以使用 -->
<package name="cn.itcast.dao"/>
</mappers>
</configuration>
2.在AccountDao接口的方法上添加注解,编写SQL语句
package cn.itcast.dao;
import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import cn.itcast.domain.Account;
public interface AccountDao {
@Insert(value="insert into account (name,money) values (#{name},#{money})")
public void saveAccount(Account account);
@Select("select * from account")
public List<Account> findAll();
}
3.编写测试的方法
package cn.itcast.test;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import cn.itcast.dao.AccountDao;
import cn.itcast.domain.Account;
public class Demo1 {
@Test
public void run1() throws Exception {
// 加载配置文件
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
// 创建工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
// 创建sqlSession对象
SqlSession session = factory.openSession();
// 获取代理对象
AccountDao dao = session.getMapper(AccountDao.class);
// 调用查询的方法
List<Account> list = dao.findAll();
for (Account account : list) {
System.out.println(account);
} /
/ 释放资源
session.close();
inputStream.close();
} @
Test
public void run2() throws Exception {
Account account = new Account();
account.setName("熊大");
account.setMoney(400d);
// 加载配置文件
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
// 创建工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
// 创建sqlSession对象
SqlSession session = factory.openSession();
// 获取代理对象
AccountDao dao = session.getMapper(AccountDao.class);
dao.saveAccount(account);
// 提交事务
session.commit();
// 释放资源
session.close();
inputStream.close();
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------
spring整合MyBatis:
1.把SqlMapConfig.xml配置文件中的内容配置到applicationContext.xml配置文件中
<!-- 配置C3P0的连接池对象 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql:///ssm" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- 配置SqlSession的工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置扫描dao的包 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itcast.dao"/>
</bean>
2. 在AccountDao接口中添加@Repository注解
3. 在service中注入dao对象,进行测试
package cn.itcast.dao;
import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import cn.itcast.domain.Account;
@Repository
public interface AccountDao {
@Insert(value="insert into account (name,money) values (#{name},#{money})")
public void saveAccount(Account account);
@Select("select * from account")
public List<Account> findAll();
}
package cn.itcast.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.itcast.dao.AccountDao;
import cn.itcast.domain.Account;
import cn.itcast.service.AccountService;
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
public void saveAccount(Account account) {
} p
ublic List<Account> findAll() {
System.out.println("业务层:查询所有账户...");
return accountDao.findAll();
}
}
AccountController类:
package cn.itcast.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.itcast.domain.Account;
import cn.itcast.service.AccountService;
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accoutService;
/**
* 查询所有的数据
* @return
*/
@RequestMapping("/findAll")
public String findAll() {
System.out.println("表现层:查询所有账户...");
List<Account> list = accoutService.findAll();
for (Account account : list) {
System.out.println(account);
} r
eturn "list";
}
}
整合完成。
页面点击链接,首先调用controller的类(AccountController类组合了AccountService(自动注入)),再调用service的类(AccountServiceImpl),然后调用dao的类(AccountServiceImpl类组合了AccountDao(自动注入))。
更多推荐
所有评论(0)