关于 SpringMVC+Spring+MyBatis 基于注解整合(3)

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

/**
 * 初始化servlet WebApplicationContext 相关
 *
 * @author chen.nie
 * @date 2017/12/28
 **/
public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{AppConfig.class};
    }

@Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{DispatcherServlet.class};
    }

@Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }


    /**
    * 添加过滤器
    *
    * @return
    */
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);
        return new Filter[]{characterEncodingFilter};
    }
}

在这里请大家关注一下这个类,这段代码的含义和配置SpringMVC的含义一样:

<web-app>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/root-context.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

5. application.properties

#数据库连接
spring.datasource.user=root
spring.datasource.password=root
spring.datasource.driver=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/MySchool?characterEncoding=utf-8&useSSL=false
#web设置相关
spring.web.view.prefix=/WEB-INF/jsp/
spring.web.view.suffix=.jsp
spring.web.static.handler=/assets/**
spring.web.static.resource=classpath:/assets/
spring.web.static.cache.period=360000
#mybatis设置相关
mybatis.type.alias.package=com.bdqn.lyrk.ssm.study.entity

6.创建MyBatis对应的mapper

package com.bdqn.lyrk.ssm.study.mapper;

import com.bdqn.lyrk.ssm.study.entity.StudentEntity;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface StudentMapper {

@Select("select * from Student")
    List<StudentEntity> selectAll();
}

7.创建业务逻辑

package com.bdqn.lyrk.ssm.study.service.impl;

import com.bdqn.lyrk.ssm.study.entity.StudentEntity;
import com.bdqn.lyrk.ssm.study.mapper.StudentMapper;
import com.bdqn.lyrk.ssm.study.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StudentServiceImpl implements IStudentService {
    @Autowired
    private StudentMapper studentMapper;


    @Override
    public List<StudentEntity> selectAll() {
        return studentMapper.selectAll();
    }

@Transactional
    @Override
    public int save(StudentEntity studentEntity) {
        return 0;
    }


}

8.创建Controller

package com.bdqn.lyrk.ssm.study.controller;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/3d7705eee9b36ce602f85539651659ac.html