SSM框架整合(Spring+SpringMVC+MyBatis)

目录

一、创建MAVEN工程

 二、导入pom依赖

三、创建XML文件

3.1创建database.properties文件

3.2创建applicationContext.xml文件

四、创建controller,dao,pojo,service,test文件夹

 五、根据需求实现代码

5.1、根据数据库内表的名称以及表的内容 创建pojo目录class文件

 5.2、创建dao层,实现客户需要(增删改查)

 5.3、service层调用dao层,实现业务逻辑

 5.4、controller控制层,通过new Service层接口对象,调用方法,实现web界面增删改查

 六、webapp目录web.xml添加配置文件

七、测试类目录,测试

八、使用tomcat运行


一、创建MAVEN工程

 二、导入pom依赖

<dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.25</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.2.6.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.2.6.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.2.6.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.2.6.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>4.2.6.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.2.6.RELEASE</version>
      <!--<scope>test</scope>-->
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.2.6.RELEASE</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.aspectj/com.springsource.org.aspectj.weaver -->
    <!--<dependency>-->
    <!--<groupId>org.aspectj</groupId>-->
    <!--<artifactId>com.springsource.org.aspectj.weaver</artifactId>-->
    <!--<version>1.6.8.RELEASE</version>-->
    <!--</dependency>-->

    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.6.8</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.2.6.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.2.6.RELEASE</version>
    </dependency>



    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.2.6.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.2.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.2.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.2.1</version>
      <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/taglibs/standard -->
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.1.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-servlet-api -->
    <dependency>
      <groupId>org.apache.tomcat</groupId>
      <artifactId>tomcat-servlet-api</artifactId>
      <version>7.0.78</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.1.5</version>
    </dependency>

三、创建XML文件

3.1创建database.properties文件

mysqldriver=com.mysql.cj.jdbc.Driver
mysqlurl=jdbc:mysql://192.168.91.130:3306/exam?allowMultiQueries=true
mysqluser=root
mysqlpwd=root
oracleDriver=oracle.jdbc.driver.OracleDriver
oracleUrl=jdbc:oracle:thin:@192.168.91.150:1521:prod
oracleuser=scott
oraclepwd=scott

3.2创建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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.1.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
    <!--包的注解扫描 -->
    <context:component-scan base-package="nj.zb.kb21"></context:component-scan>

    <!--数据库配置信息 -->
    <context:property-placeholder location="classpath:database.properties"></context:property-placeholder>

    <!--加载数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${mysqlurl}"/>
        <property name="driverClass" value="${mysqldriver}"/>
        <property name="user" value="${mysqluser}"/>
        <property name="password" value="${mysqlpwd}"/>
    </bean>

    <!--配置SqlSessionFactory工厂对象-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--加载数据源-->
        <property name="dataSource" ref="dataSource"></property>
        <!--扫描实体类包pojo-->
        <property name="typeAliasesPackage" value="nj.zb.kb21.pojo"></property>
        <!--扫描SQL的配置文件(dao接口的实现xml文件)-->
        <property name="mapperLocations" value="classpath:mappers/ExamDao.xml"></property>

    </bean>

    <!--完成映射类-->
    <bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
        <property name="basePackage" value="nj.zb.kb21.dao"></property>
    </bean>

    <!--事务管理-->
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"></property>
   </bean>

    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

    <mvc:annotation-driven/>
</beans>

3.3创建mappers目录,映射dao层接口 (这个步骤要放在dao层对象接口完成后)

四、创建controller,dao,pojo,service,test文件夹

 五、根据需求实现代码

图:文件夹结构图

5.1、根据数据库内表的名称以及表的内容 创建pojo目录class文件

图:数据库student表

 图:pojo层student 对象 (无参,有参构造,get,set方法,tostring 需要添加)

 5.2、创建dao层,实现客户需要(增删改查)

 5.3、service层调用dao层,实现业务逻辑

先简单来讲下Dao层,和Service层的概念:

Service是业务层,Dao是数据访问层,这样的分层是基于MVC架构来说的。

Dao层:全称为data access object,属于一种比较底层,比较基础的操作,具体到对于某个表、某个实体的增删改查

Service层:被称为服务,肯定是相比之下比较高层次的一层结构,相当于将几种操作封装起来。

具体起来,Dao的作用是封装对数据库的访问:增删改查,不涉及业务逻辑,只是达到按某个条件获得指定数据的要求;
而Service,则是专注业务逻辑,对于其中需要的数据库操作,都通过Dao去实现。

简单来说就是Dao层是底层,具体到某一张表的增删查改,可以理解为有多少张表就有多少个Dao层,而Service成可以理解为是Dao层的 “上司”,通过Service层来控制Dao层。

一个Service层控制一个或者多个Dao层。

通过Service层去调用Dao层,不仅可以解耦合,而且可以增加代码的拓展性。

图:service层接口方法(来自于dao层)

 图:通过new dao层接口,创建对象,来实现接管dao层

@Autowired可以标注在属性上、方法上和构造器上,来完成自动装配

它可以标注在属性上、方法上和构造器上,那有什么区别吗?简单来说因为类成员的初始化顺序不同,静态成员 ——> 变量初始化为默认值——>构造器——>为变量赋值。如果标注在属性上,则在构造器中就不能使用这个属性(对象)的属性和方法。

当标注的属性是接口时,其实注入的是这个接口的实现类, 如果这个接口有多个实现类,只使用@Autowired就会报错,因为它默认是根据类型找,然后就会找到多个实现类bean,所有就不知道要注入哪个。然后它就会根据属性名去找。所以如果有多个实现类可以配合@Qualifier(value=“类名”)来使用

 5.4、controller控制层,通过new Service层接口对象,调用方法,实现web界面增删改查

RequestMapping注解有六个属性,下面我们把她分成三类进行说明。
  1、 value, method;

               value: 指定请求的实际地址,指定的地址可以是URI Template 模式;
    method: 指定请求的method类型, GET、POST、PUT、DELETE等;
  2、 consumes,produces;
    consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
    produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
  3、 params,headers;
    params: 指定request中必须包含某些参数值是,才让该方法处理。
    headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。

 @ResponseBody//将方法返回的对象转换成JSON字符串

 六、webapp目录web.xml添加配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

  <display-name>Archetype Created Web Application</display-name>
  <welcome-file-list>
    <welcome-file>index1.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>DispatcherServlet</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>
  </servlet>
  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

七、测试类目录,测试

 代码:

    @Test
    public  void testGetAllStudent(){
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println(context);
        ExamDao examDao = (ExamDao) context.getBean("examDao");
        System.out.println(examDao);
        List<Student> allStudent = examDao.getAllStudent();
        for (Student st:allStudent
             ) {
            System.out.println(st);
        }
        List<Classes> allClasses = examDao.getAllClasses();
        for (Classes cl:allClasses
        ) {
            System.out.println(cl);
        }
    }

八、使用tomcat运行

运行需要主要:

http://192.168.1.101:8080/loadstu.do
http://192.168.1.101:8080/loadcla.do
http://192.168.1.101:8080/savestu.doname=zhh&age=21&gender=1&telephone=12345532222&email=zhh@qq.com&classid=1

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值