【学习笔记】SSM整合篇(三)

简介: 【学习笔记】SSM整合篇(三)

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);
    }


相关文章
java202304java学习笔记第六十五天-ssm-声明式控制-事务参数的配置1
java202304java学习笔记第六十五天-ssm-声明式控制-事务参数的配置1
35 0
java202304java学习笔记第六十六天-ssm-动态sql-一对一的配置实现1
java202304java学习笔记第六十六天-ssm-动态sql-一对一的配置实现1
59 0
java202304java学习笔记第六十七天-ssm-动态sql-多对多注解开发之1
java202304java学习笔记第六十七天-ssm-动态sql-多对多注解开发之1
58 0
|
6月前
|
前端开发 Java 数据库连接
【1024程序员节】RestFul,mvc拦截器,SSM基本整合-学习笔记
【1024程序员节】RestFul,mvc拦截器,SSM基本整合-学习笔记
72 1
java202304java学习笔记第六十七天-ssm-动态sql-一对一的配置实现2
java202304java学习笔记第六十七天-ssm-动态sql-一对一的配置实现2
60 0
java202304java学习笔记第六十七天-ssm-动态sql-一对一的配置实现2
java202304java学习笔记第六十七天-ssm-动态sql-多对多注解开发之2
java202304java学习笔记第六十七天-ssm-动态sql-多对多注解开发之2
60 0
java202304java学习笔记第六十七天-ssm-动态sql-多对多注解开发之2
java202304java学习笔记第六十七天-ssm-动态sql-一对多得注解开发1
java202304java学习笔记第六十七天-ssm-动态sql-一对多得注解开发1
59 0
java202304java学习笔记第六十七天-ssm-动态sql-一对多得注解开发1
java202304java学习笔记第六十六天-ssm-mybatis的dao层实现1
java202304java学习笔记第六十六天-ssm-mybatis的dao层实现1
35 0
java202304java学习笔记第六十六天-ssm-mybatis中dao层实现-动态sql-foreach之2
java202304java学习笔记第六十六天-ssm-mybatis中dao层实现-动态sql-foreach之2
55 0
java202304java学习笔记第六十六天-ssm-mybatis中dao层实现-动态sql-if之1
java202304java学习笔记第六十六天-ssm-mybatis中dao层实现-动态sql-if之1
37 0