SSM整合,超简单手把手教学

SSM整合

三大框架的整合步骤

  1. 加入依赖

     
        <dependency>
          <groupId>org.mybatisgroupId>
          <artifactId>mybatisartifactId>
          <version>3.4.6version>
        dependency>
        
        <dependency>
          <groupId>org.mybatisgroupId>
          <artifactId>mybatis-springartifactId>
          <version>1.3.2version>
        dependency>
        
        <dependency>
          <groupId>com.mchangegroupId>
          <artifactId>c3p0artifactId>
          <version>0.9.5.2version>
        dependency>
        
        <dependency>
          <groupId>mysqlgroupId>
          <artifactId>mysql-connector-javaartifactId>
          <version>5.1.45version>
        dependency>
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-jdbcartifactId>
          <version>5.0.8.RELEASEversion>
        dependency>
        
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-contextartifactId>
          <version>5.0.8.RELEASEversion>
        dependency>
        
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-txartifactId>
          <version>5.0.8.RELEASEversion>
        dependency>
        
        <dependency>
          <groupId>org.aspectjgroupId>
          <artifactId>aspectjweaverartifactId>
          <version>1.8.8version>
        dependency>
    	
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-webmvcartifactId>
          <version>5.0.8.RELEASEversion>
        dependency>
    	
          	<dependency>
                <groupId>com.mchangegroupId>
                <artifactId>c3p0artifactId>
                <version>0.9.5.2version>
            dependency>
    
  2. jdbc.properties文件的配置

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql:///db?useUnicode=true&characterEncoding=utf8
    jdbc.username=帐号
    jdbc.password=密码
    
  3. 把MyBatis的mybatis-config.xml配置的内容,交给Spring的applicationContext.xml配置(数据源…),applicationContext.xml配置如下表:

    
    <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:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:jee="http://www.springframework.org/schema/jee"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    
    
        
        <context:property-placeholder location="classpath:jdbc.properties" />
        
        <bean id="mydataSources" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driver}"/>
            <property name="jdbcUrl" value="${jdbc.url}"/>
            <property name="user" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        bean>
        
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="mydataSources"/>
        bean>
        
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            
            <property name="basePackage" value="com.mooc.dao"/>
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        bean>
        
        
        <context:component-scan base-package="com.mooc">
            <context:exclude-filter type="annotation" expression="com.mooc.controller"/>
        context:component-scan>
        
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="mydataSources"/>
         bean>
        
        <tx:annotation-driven transaction-manager="transactionManager"/>
    beans>
    
  4. 配置web.xml,在web加载的时候,把Spring配置文件也加载进去

    
    
    <web-app>
      <display-name>Archetype Created Web Applicationdisplay-name>
      
      <servlet>
        <servlet-name>spring-dispatcherservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
          <param-name>contextConfigLocationparam-name>
          <param-value>classpath:applicationContext.xmlparam-value>
        init-param>
      servlet>
      <servlet-mapping>
        <servlet-name>spring-dispatcherservlet-name>
        <url-pattern>/url-pattern>
      servlet-mapping>
    web-app>
    
    
  5. 置Spring-mvc.xml文件,配视图解析器,文件上传器、静态资源的处理等


<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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    
    
    <mvc:annotation-driven />
    <context:component-scan base-package="com.mooc.controller"/>

beans>
  1. 最后看一下工程目录

[外链图片转存失败(img-9nux4xh4-1568985856298)(D:\我的文件\Pictures\比特截图2019-09-20-21-08-58.png)]

最后我们来测试一下有没有配置正确

//controller层
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/findAll")
    @ResponseBody
    public String findAll(){
        List<User> list=userService.findAll();
        System.out.println(list);
        return list.toString();
    }
}
//service接口层
public interface UserService {
    public List<User> findAll();
}
//service实现层
@Service("userService")//别忘了在这里加注解
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }
}
//dao接口层,因为使用mapper动态代理,则不需要dao层接口的实现类
public interface UserDao {
    @Select("select * from user")
    List<User> findAll();
}

最后我们浏览器访问下

SSM整合,超简单手把手教学_第1张图片

能正常访问到数据库中的数据,说明此SSM整合正确!YES!

你可能感兴趣的:(SSM整合,超简单手把手教学)