ssm整合配置

引入依赖

在中引入依赖

  <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!--  添加 servlet 依赖 -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>


    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spirng.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spirng.version}</version>
    </dependency>

    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.6.12</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.6.12</version>
    </dependency>


    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spirng.version}</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.71</version>
    </dependency>


    <!--spring mvc-json依赖-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.9</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.9</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.9.9</version>
    </dependency>



    <!--文件上传依赖-->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.3</version>
    </dependency>


    <!-- google 验证码 Kaptcha -->
    <dependency>
      <groupId>com.github.penggle</groupId>
      <artifactId>kaptcha</artifactId>
      <version>2.3.2</version>
      <exclusions>
        <exclusion>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <!--      ****************以下为mybatis 相关依赖*************************************             -->

    <!--连接池-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.16</version>
    </dependency>


    <!--mybatis-spring 依赖-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.0</version>
    </dependency>



    <!--mybatis 依赖-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.5</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.9</version>
    </dependency>

    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.12</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spirng.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.0.16.RELEASE</version>
    </dependency>
    <!--      ****************以上为mybatis 相关依赖*************************************             -->

    <!--配置分页  放在最下面-->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>4.1.4</version>
    </dependency>

2.在这个中

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    
    ------------添加下面这个东西------------
    
    <!--在当前pom 或者父类pom 中声明属性  -->
    <spirng.version>5.0.16.RELEASE</spirng.version>
    
    
</properties>

2.2.将mybatis.properties,log4j.properties(可以不靠被),mybatis-config.xml拷贝到resource文件下

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">



    <!-- 1.加载 mysql 配置-->
    <context:property-placeholder location="classpath:mybatis.properties"></context:property-placeholder>

    <!-- 2.配置druid 数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">

        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!-- 3.配置 mybatis SqlSessionFactory-->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 配置数据源-->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 配置 mybatis 配置文件路径  -->
        <property name="configLocation" value="classpath:mybatisConfig.xml"></property>
    </bean>

    <!-- 4.配置 mapper 扫描器器 -->

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.qfedu.dao"></property>
        <!-- 配置sqlSessionFactory  -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>

    </bean>


    <!--
         5.将service 加入到容器
    -->

    <context:component-scan base-package="com.qfedu.services"></context:component-scan>


</beans>

log4j.properties

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=mybatis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

mybatisConfig.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>



    <settings>
        <!-- 在这里设置日志,则日志打印在控制态,否则按照配置打印文件中 -->
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>


    <!--配置别名-->
    <typeAliases>
        <package name="com.qfedu.entity"/>
    </typeAliases>


        <plugins>

            <!-- 让mybatis  加载PageHelper -->
            <!-- com.github.pagehelper为PageHelper类所在包名 -->
            <plugin interceptor="com.github.pagehelper.PageHelper">
                <!-- 使用MySQL方言的分页 -->
                <property name="helperDialect" value="mysql"/><!--如果使用mysql,这里value为mysql-->
                <property name="pageSizeZero" value="true"/>
            </plugin>
        </plugins>

</configuration>

springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">




       <!-- 1.包扫描器 扫描controller-->
       <context:component-scan base-package="com.qfedu.controller"></context:component-scan>

       <!-- 2.配置  激活控制器相关注解-->
       <mvc:annotation-driven></mvc:annotation-driven>



        <!--3.配置静态资源-->
        <mvc:default-servlet-handler></mvc:default-servlet-handler>

        <!--4.配置jsp-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/jsp/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>


    <!--
      配置文件解析器 讲 post请求的文件 解析出来
       id="multipartResolver" 必须是multipartResolver 因为spring 会根据id  multipartResolver 去找找自动装配
  -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

        <property name="defaultEncoding" value="utf-8"></property>
        <property name="maxInMemorySize" value="1024000"></property>
    </bean>


</beans>

web.xml

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>


  <!-- 应用启动时 初始化 spring容器 参数 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>




  <!-- 配置编码格式-->
  <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>

  <!--
    1在应用启动时回调 监听ContextLoaderListener
    2.在监听 ContextLoaderListener 初始化 容器 (applicationContext.xml)
-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <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:springmvc-servlet.xml</param-value>
    </init-param>
  </servlet>


  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

mybatis.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/b?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
jdbc.username=root
jdbc.password=369852

注意


mybatis:resources 文件***下创建com/qfedu/文件夹***(一对一 一对多,resultMap,等问题)
		在resources下创建mybatis配置文件mybatis-config.xml(数据库连接,驼峰写		法,别名的配置等)
		在resource配置log4j.properties
mybatis.properties----连接数据库		

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0SSxyU1o-1604057768922)(C:\Users\1937959110\AppData\Roaming\Typora\typora-user-images\image-20201028001341641.png)]

mybatis.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/qf2006
jdbc.username=root
jdbc.password=123456

mybatis-config.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>




    <!--配置别名-->
    <typeAliases>
        <package name="com.qfedu.entity"/>
    </typeAliases>


</configuration>

3.配置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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">



       <!-- 1.加载 mysql 配置-->
       <context:property-placeholder location="classpath:mysql.properties"></context:property-placeholder>

        <!-- 2.配置druid 数据源-->
       <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">

           <property name="driverClassName" value="${jdbc.driver}"></property>
           <property name="url" value="${jdbc.url}"></property>
           <property name="username" value="${jdbc.username}"></property>
           <property name="password" value="${jdbc.password}"></property>
       </bean>

       <!-- 3.配置 mybatis SqlSessionFactory-->

       <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
           <!-- 配置数据源-->
            <property name="dataSource" ref="dataSource"></property>
           <!-- 配置 mybatis 配置文件路径  -->
            <property name="configLocation" value="classpath:mybatisConfig.xml"></property>
       </bean>

       <!-- 4.配置 mapper 扫描器器 -->

        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
                <property name="basePackage" value="com.qfedu.dao"></property>
            <!-- 配置sqlSessionFactory  -->
                <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>

        </bean>


        <!--
             5.将service 加入到容器
        -->

        <context:component-scan base-package="com.qfedu.service"></context:component-scan>


</beans>

4.创建对应的dao 和mapper

写dao层以及resources对应的。xml层

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qfedu.dao.StudentDao">
    <select id="findStudentById" resultType="Student">
         select * from  student where id = #{id}
    </select>
</mapper>

以上文件进行适当地修改

注意!mybatis的xml包头直接创建复制粘贴 和spring的xml不一样

5.测试

public class SpringMybatisTest {

    public static void main(String[] args) {


        ApplicationContext applicationContext =  new ClassPathXmlApplicationContext("applicationContext.xml");


        //从容器获取
       StudentService studentService =  applicationContext.getBean(StudentService.class);

        StudentTB studentTB = studentService.findStudentById(1);
        System.out.println("studentTB:"+studentTB);

    }
}

重点:一定要测试spring 和mybatis

1.在resources下面创建springmvc-servlet.xml(这是自己写的与后面的web。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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">




       <!-- 1.包扫描器 扫描controller-->
       <context:component-scan base-package="com.qfedu.controller"></context:component-scan>

       <!-- 2.配置  激活控制器相关注解-->
       <mvc:annotation-driven></mvc:annotation-driven>



        <!--3.配置静态资源-->
        <mvc:default-servlet-handler></mvc:default-servlet-handler>

        <!--4.配置jsp-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/jsp/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>



</beans>

2.将spring配置和springmvc相结合 web.xml配置

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <!-- 应用启动时 初始化 spring容器 参数 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>


  <!--
      1在应用启动时回调 监听ContextLoaderListener
      2.在监听 ContextLoaderListener 初始化 容器 (applicationContext.xml)
  -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <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:springmvc-servlet.xml</param-value>
    </init-param>
  </servlet>


  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>



</web-app>

controller 引入

注意注解的使用

@RestController// 当前控制器下所有请求都 将对象转 json
public class StudentTbController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/findStudentTBById")
    @ResponseBody
    public StudentTB findStudentTBById(int id){

        StudentTB studentTB =   studentService.findStudentById(id);

        return studentTB;
    }

}

测试http://localhost:8080/findStudentTBById?id=1

param-value>classpath:springmvc-servlet.xml

springmvc /

controller 引入

#### 注意注解的使用  

@RestController// 当前控制器下所有请求都 将对象转 json
public class StudentTbController {

@Autowired
private StudentService studentService;

@RequestMapping("/findStudentTBById")
@ResponseBody
public StudentTB findStudentTBById(int id){

    StudentTB studentTB =   studentService.findStudentById(id);

    return studentTB;
}

}


测试http://localhost:8080/findStudentTBById?id=1

![image-20201026183419458](https://img-blog.csdnimg.cn/img_convert/908ee327001a002f450ab3b3d611025c.png)
一、简介 通过这个课程带大家从零开发一款功能全面的后台管理系统,包括项目搭建、功能实现到最后的Linux系统部署全过程。本课程使用SpringMVC + Spring + Mybatis作为主体框架,使用AdminLTE作为前端框架,使用主流关系型数据库Mysql作为存储数据库,使用非关系型数据库Redis作为缓存数据库,并集成SpringSecuriy安全框架做权限的动态管理,集成Swagger2自动生成接口文档,集成Druid连接池进行SQL性能监控,集成ActiveMQ消息中间件进行异步解耦,提高性能。最后使用linux系统进行服务部署,并搭建nginx反向代理服务器提高网站性能。 二、学习目标 通过本课程的学习带大家掌握SSM框架的开发流程,并熟练使用SpringSecurity做为安全框架进行权限管理,整合相关优秀的开源框架进行功能开发。还在项目中带大家学习前端相关的Jquery、Bootstrap等知识。课程结束之后希望大家能做到独立进行开发项目的目的,增强解决问题的能力,具备功能落地实现的能力。 三、课程涉及知识点 SpringMVC源码分析 Mybatis源码分析 通用Mapper Mysql数据库 Redis缓存实现 ActiveMQ消息中间件 SpringSecurity鉴权 Swagger2接口文档生成 自定义注解 AOP切面编程 自定义过滤器 Logback日志整合 Druid性能监控 Linux系统 Nginx反向代理 Ajax异步请求技术 Jquery基本使用 AdminLTE前端框架 Chart图表-线状图和饼状图 百度地图定位城市 BootStrap前端框架 BootStrap-Table插件 BootStrap-Treeview插件 Markdown编辑器 403、404、500错误页面配置 数据库事务 消息提示插件toastr.js 图片上传插件bootstrap fileinput 数字滚动效果 pv/uv流量统计 ... 四、课程部分内容截图如下 1、首页 2、菜单管理 3、图床管理 4、图标管理 5、留言反馈管理 6、druid监控 7、登录日志
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页