SSM整合

SSM整合

整合的配置文件

  1. 导入需要的依赖(数据库驱动,mybatis,mybatis-spring,junit,servlet,jsp,spring,aop织入)
<!--数据库驱动-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.23</version>
</dependency>
<!--mybatis-->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.6</version>
</dependency>
<!--mybatis整和spring-->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.6</version>
</dependency>
<!--junit-->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
</dependency>
<!-- servlet jsp-->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.2</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

<!--spring-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.6</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.3.4</version>
</dependency>

<!--aop织入-->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.4</version>
</dependency>
  1. maven的静态资源导出问题,在pom.xml文件中添加如下:
<!-- 静态资源导出-->
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>
  1. 整体的结构

在这里插入图片描述

  1. 编写database.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
  1. mybatis的配置
<?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>
    <!-- 这里整合spring 数据源的配置就交给了spring
    mybatis负责配置别名和mappers和settings即可
    -->

    <typeAliases>
        <package name="com.yxx.pojo"/>
    </typeAliases>

    <mappers>

    </mappers>
</configuration>
  1. mybatis-config配置
<?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>
    <!-- 这里整合spring 数据源的配置就交给了spring
    mybatis负责配置别名和mappers和settings即可
    -->

    <typeAliases>
        <package name="com.yxx.pojo"/>
    </typeAliases>

    <mappers>
        <mapper class="com.yxx.dao.StudentDao"/>
    </mappers>
</configuration>
  1. applicationContext.xml的配置 分为两个部分 整合mybatis(dao层)和整合service(事务)
  • 整合mybatis
<!-- 前四步整合mybatis-->
<!--1.关联数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"/>

<!--2.配置dataSource 连接池
    dbcp,c3p0,druid,hikari
    我这直接使用spring提供的
 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

<!--3.配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <!--绑定mybatis配置文件 -->
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

<!--4.配置dao接口扫描包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 需要扫描的包-->
    <property name="basePackage" value="com.yxx.dao"/>
</bean>
  • 整合service(事务)
<!--整合service-->
<!--1. 扫描service的注解-->
<context:component-scan base-package="com.yxx.service"/>

<!--2.声明式事务配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!--注入数据源 -->
    <property name="dataSource" ref="dataSource"/>
</bean>

<!--3.aop事务 -->
  • 整体
<?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">

    <!-- 前四步整合mybatis-->
    <!--1.关联数据库配置文件-->
    <context:property-placeholder location="classpath:database.properties"/>

    <!--2.配置dataSource 连接池
        dbcp,c3p0,druid,hikari
        我这直接使用spring提供的
     -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--3.配置sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--绑定mybatis配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!--4.配置dao接口扫描包 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 需要扫描的包-->
        <property name="basePackage" value="com.yxx.dao"/>
    </bean>

    <!--整合service-->
    <!--1. 扫描service的注解-->
    <context:component-scan base-package="com.yxx.service"/>

    <!--2.声明式事务配置-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--3.aop事务 -->
</beans>
  1. springmvc配置
<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!--1.注解驱动
    并且 这个会为我们自动配置映射器和适配器
    RequestMappingHandlerMapping和RequestMappingHandlerAdapter
    -->
    <mvc:annotation-driven/>

    <!--2. 静态资源过滤-->
    <mvc:default-servlet-handler/>

    <!--3.试图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--4.扫描包 controller -->
    <context:component-scan base-package="com.yxx.controller"/>
</beans>
  1. web.xml的配置(配置DispatcherServlet,spring监听器,编码过滤。。。)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!-- 配置spring监听器 
	web项目启动时,自动将spring中的bean注入
-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--配置springmvc核心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:springmvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!--/ 处理除(.jsp)以外的所有
            /* 包括.jsp
         -->
        <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>
</web-app>

需要配置的东西就是上面这些,下面给一个例子来看效果,用的好久以前就有的数据库,就查询个所有信息就可以了

例子

  1. 数据库
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`  (
  `sid` int(255) NOT NULL AUTO_INCREMENT,
  `sname` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
  `sage` int(255) NULL DEFAULT NULL,
  `ssex` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
  `register` varchar(10) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
  `sphone` varchar(20) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
  PRIMARY KEY (`sid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2013 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (2001, '高升', 19, '男', '乐山', '1894625');
INSERT INTO `student` VALUES (2002, '林树香', 19, '女', '贵阳', '1873548');
INSERT INTO `student` VALUES (2003, '金娜', 20, '女', '攀枝花', '1859874');
INSERT INTO `student` VALUES (2004, '张林', 21, '男', '成都', '1897564');

SET FOREIGN_KEY_CHECKS = 1;
  1. 实体类 Student(省略 get set 构造 tostring)
public class Student {
    private int sid;
    private String sname;
    private int sage;
    private String ssex;
    private String register;
    private String sphone;
}
  1. dao层
public interface StudentDao {

    //获取所有
    public List<Student> getAll();
}
  1. dao层的xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yxx.dao.StudentDao">
    <select id="getAll" resultType="student">
        select * from student
    </select>
</mapper>
  1. service层
@Service
public class StudentService {

    @Autowired
    private StudentDao studentDao;

    public List<Student> getAll(){
        return studentDao.getAll();
    }
}
  1. controller层
@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/queryall")
    public String queryAll(Model model){
        List<Student> students = studentService.getAll();
        model.addAttribute("students",students);
        return "all";
    }
}
  1. 展示页面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <table>
        <thead>
            <tr>
                <td>sid</td>
                <td>sname</td>
                <td>ssage</td>
                <td>ssex</td>
                <td>register</td>
                <td>sphone</td>
            </tr>
        </thead>
        <tbody>
            <c:forEach items="${students}" var="list">
                <tr>
                    <td>${list.sid}</td>
                    <td>${list.sname}</td>
                    <td>${list.sage}</td>
                    <td>${list.ssex}</td>
                    <td>${list.register}</td>
                    <td>${list.sphone}</td>
                </tr>
            </c:forEach>
        </tbody>
    </table>
</body>
</html>
  1. 结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值