ssm项目整合(spring、springmvc、mybatis)

ssm项目整合(spring、springmvc、mybatis)

开环境

  1. jdk1.8
  2. mysql5.7
  3. idea(编译器)
  4. maven 3.6(项目构建软件)  
  • 第一步:创建一个数据库
    create database ssm;
    use ssm
    create table account(
        id int primary key auto_increment,
        name varchar(100),
        money double(7,2),
    );

     

  • 第二步:构建一个maven项目
  1. 创建项目选择maven构建,勾选maven的web项目模板。点击下一步下一步完成创建(操作如下图)。

    构建完成后如下图

  2. 导入项目依赖
    <!-- spring -->
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>1.6.8</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
          <scope>compile</scope>
        </dependency>
    
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>${mysql.version}</version>
        </dependency>
    
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
          <version>2.5</version>
          <scope>provided</scope>
        </dependency>
    
        <dependency>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>jsp-api</artifactId>
          <version>2.0</version>
          <scope>provided</scope>
        </dependency>
    
        <dependency>
          <groupId>jstl</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
        </dependency>
    
        <!-- log start -->
        <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>${log4j.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>${slf4j.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>${slf4j.version}</version>
        </dependency>
    
        <!-- log end -->
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>${mybatis.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>1.3.0</version>
        </dependency>
    
        <dependency>
          <groupId>c3p0</groupId>
          <artifactId>c3p0</artifactId>
          <version>0.9.1.2</version>
          <type>jar</type>
          <scope>compile</scope>
        </dependency>

     

  3. 创建相应的包
  • 创建相应的类
  1. Account类
    package com.lx.domain;
    
    public class Account {
        
        private Integer id;
        private String name;
        private Double money;
    
        public Account() {
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Double getMoney() {
            return money;
        }
    
        public void setMoney(Double money) {
            this.money = money;
        }
    
        @Override
        public String toString() {
            return "Account{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", money=" + money +
                    '}';
        }
    }
    

     

  2. AccountDao类
    package com.lx.dao;
    
    import com.lx.domain.Account;
    
    import java.util.List;
    
    public interface AccountDao {
    
        /**
         * 查询所用用户
         * @return
         */
        public List<Account> findAll();
    
        /**
         * 保存一个Account
         * @param account
         */
        public void saveAccount(Account account);
    }
    

     

  3. AccountService类
    package com.lx.service;
    
    import com.lx.domain.Account;
    
    import java.util.List;
    
    public interface AccountService {
    
        /**
         * 查询所用的Account 
         * @return
         */
        public List<Account> findAll();
    
        /**
         * 保存一个Account
         * @param account
         */
        public void saveAccount(Account account);
    }
    

     

  4. AccountServiceImpl类
    package com.lx.service.impl;
    
    import com.lx.domain.Account;
    import com.lx.service.AccountService;
    
    import java.util.List;
    
    public class AccountServiceImpl implements AccountService {
        
        @Override
        public List<Account> findAll() {
            System.out.println("查询所有");
            return null;
        }
    
        @Override
        public void saveAccount(Account account) {
            System.out.println("保存所有");
        }
    }
    

     

  5. AccountController类
    package com.lx.controller;
    
    public class AccountController {
    
        public void findAll() {
            System.out.println("查询搜有controller");
        }
    }
    

     

  6. 以上几点已经完成了一个项目三次框架类的书写。

  • 创建spring的配置文件
  1. 在resource目录下创建一个配置文件(文件名:applicationContext.xml)
  2. 在配置文件中加入约束
    <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:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context.xsd
    	http://www.springframework.org/schema/aop
    	http://www.springframework.org/schema/aop/spring-aop.xsd
    	http://www.springframework.org/schema/tx
    	http://www.springframework.org/schema/tx/spring-tx.xsd">

     

  3. 开启spring的直接扫描
    <!--开启spring的注解扫描 ,只处理dao,domain的注解,排除controller-->
        <context:component-scan base-package="com.lx">
            <!--排除controller的注解-->
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>

     

  • 在AccountServiceImpl的类上加上spring相关的注解
  • 现在spring已经注入,现在测试一下spring是是否配置成功。

在test包中创建一个测试类

import com.lx.service.AccountService;
import com.lx.service.impl.AccountServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpingTest {

    @Test
    public void springTest(){

        //读取配置文件
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        //获取对象
        AccountServiceImpl accountSerivce = ac.getBean("accountService", AccountServiceImpl.class);
        //执行对象的方法
        accountSerivce.findAll();

    }
}
  • 接下来我们来整合springMVC
  1. 在resource目录先创建并编写springMVC的配置文件,并导入约束
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
           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
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
       
    </beans>

     

  2. 在文件中添加配置形象
     <!--开启注解扫描,只扫描Controller的类-->
        <context:component-scan base-package="com.lx">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    
        <!--配置视图解析器-->
        <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!--要扫描的包-->
            <property name="prefix" value="/WEB-INF/pages/"/>
            <!--要扫描的文件类型-->
            <property name="suffix" value=".jsp"/>
        </bean>
    
        <!--不拦截的资源设置-->
        <mvc:resources location="/css/" mapping="/css/**" />
        <mvc:resources location="/images/" mapping="/images/**" />
        <mvc:resources location="/js/" mapping="/js/**" />
    
        <!--开启SpringMVC注解的支持-->
        <mvc:annotation-driven/>

     

  3. 在web.xml的web项目的配置文件中添加到前端控制器代码如下
  4. 在web.xml的文件中加入过滤器解决中文乱码问题
    <!--解决中文乱码问题-->
      <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>

  5. 完善AccountController类,给类中加入Controller的注解和RequestMapping(效果如下)
    package com.lx.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @RequestMapping("/ssm")
    public class AccountController {
    
        @RequestMapping("/account")
        public String  findAll() {
            return "hello";
        }
    }
    

     

接下来我们来测试是否成功配置了springmvc,首先我们编写一个index.jsp的文件,在里面写入一个a标签跳转到AccountController的findAll方法上,让findAll方法跳转到pages下的hello.jsp界面,并在界面显示helloworld。如果跳转成功说明,sprigmvc配置成功。

<%--
  Created by IntelliJ IDEA.
  User: lixing
  Date: 2020/11/12 0012
  Time: 0:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <a href="account/findAll">查找所有</a>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: lixing
  Date: 2020/11/14 0014
  Time: 8:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>hello world</h1>
</body>
</html>
  • ​​​​​​​接下来开始配置整合mybatis
  1. 在aplicationgContext.xml文件中配置mybatis
     <!--Spring整合MyBatis框架-->
        <!--配置连接池-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"/>
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm"/>
            <property name="user" value="root"/>
            <property name="password" value="123456"/>
        </bean>
    
        <!--配置SqlSessionFactory工厂-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
        </bean>
    
        <!--配置持久层接口所在包-->
        <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.lx.dao"/>
        </bean>
    
    
        <!--配置Spring框架声明式事务管理-->
        <!--配置事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
    
        <!--配置事务通知-->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="find*" read-only="true"/>
                <tx:method name="*" isolation="DEFAULT"/>
            </tx:attributes>
        </tx:advice>
    
        <!--配置AOP增强-->
        <aop:config>
            <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.lx.service.impl.*ServiceImpl.*(..))"/>
        </aop:config>

     

  2. 在AccountDao类中编写sql语句
    package com.lx.dao;
    
    import com.lx.domain.Account;
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Select;
    import org.springframework.stereotype.Repository;
    
    import java.util.List;
    
    @Repository
    public interface AccountDao {
    
        /**
         * 查询所用用户
         * @return
         */
        @Select("select * from account")
        public List<Account> findAll();
    
        /**
         * 保存一个Account
         * @param account
         */
        @Insert("insert into account(name,money) values (#{name},#{money})")
        public void saveAccount(Account account);
    }
    

     

  3. 在AccountServiceImpl中用ioc的方式引入AccountDao,在AccountController类中引入AccountServiceImpl类
  4. 最后测试成功。

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值