Service层
扫描service下的包
<context:component-scan base-package="com.xiaobao.service"/>
注入数据源:
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入数据源--> <property name="dataSource" ref="dataSource"/> </bean>
注入到Spring:
<bean id="UserServiceImpl" class="com.xiaobao.service.UserServiceImpl"> <property name="userMapper" ref="userMapper"/> </bean>
MVC层
web.xml配置
首先在 web.xml 文件中配置 DispatcherServlet.
<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> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
配置乱码过滤:
<filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
文件上传配置:
<filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/**</url-pattern> </filter-mapping>
设置session过期时间:
<!--session过期时间--> <session-config> <session-timeout>15</session-timeout> </session-config>
设置监听器启动工厂:
<!--启动spring工厂--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
Spring-mvc.xml文件的配置
扫描包:
<context:component-scan base-package="com.xiaobao.controller"/>
注解驱动:
<!--当SpringMVC中设置任何一个view-controller时,其他控制器中的请求映射将全部失效 此时需要在SpringMVC的核心配置文件中设置开启mvc注解驱动的标签:--> <mvc:annotation-driven/>
静态资源过滤:
<mvc:default-servlet-handler/>
thymeleaf视图解析器:
<!-- 配置thymeleaf--> <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="order" value="1"/> <property name="characterEncoding" value="UTF-8"/> <property name="templateEngine"> <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver"> <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <!-- 视图前缀 --> <property name="prefix" value="/WEB-INF/templates/"/> <!-- 视图后缀 --> <property name="suffix" value=".html"/> <property name="templateMode" value="HTML5"/> <property name="characterEncoding" value="UTF-8"/> </bean> </property> </bean> </property> </bean>
Spring整合到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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="classpath:spring-dao.xml"/> <import resource="classpath:spring-service.xml"/> <import resource="classpath:spring-mvc.xml"/> </beans>
Controller层的编写
UserController:
@RestController @RequestMapping("/user") public class UserController { @Autowired @Qualifier("UserServiceImpl") private UserService userService; @RequestMapping("/register") public Result<User> register(@RequestBody User user){ return userService.register(user.getUserName(),user.getPassWord()); } @RequestMapping("/hello") public String hello(){ return "s"; } @RequestMapping("/login") public Result<User> login(@RequestBody User user){ return userService.login(user.getUserName(),user.getPassWord()); } @RequestMapping("/update") public Result<User> update(@RequestBody User user){ return userService.updatePassWord(user.getUserName(),user.getPassWord()); } //用户头像图片上传 @RequestMapping(value = "/upload", method = RequestMethod.POST) public Result UploadPic(User user,@RequestParam("photo") MultipartFile file, HttpServletRequest request) throws IOException { System.out.println(user.getUserName()); //获取文件名 : file.getOriginalFilename(); String uploadFileName = file.getOriginalFilename(); //如果文件名为空,直接回到首页! if ("".equals(uploadFileName)) { return Result.error(1, "文件名为空", null); } //上传路径保存设置 String path = request.getSession().getServletContext().getRealPath("/upload"); //如果路径不存在,创建一个 File realPath = new File(path); if (!realPath.exists()) { realPath.mkdir(); } InputStream is = file.getInputStream(); //文件输入流 OutputStream os = new FileOutputStream(new File(realPath, uploadFileName)); //文件输出流 //读取写出 int len = 0; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1) { os.write(buffer, 0, len); os.flush(); } os.close(); is.close(); String URL = request.getRequestURL().subSequence(0, request.getRequestURL().length() - 11) + "upload/" + uploadFileName; return userService.saveUserImg(URL,user.getUserName()); // return Result.success(URL); }