IDEA SSM简单整合

SSM整合相关步骤

  • 目录结构
    IDEA SSM简单整合_第1张图片
  • 第一步导入jar包
    下面是pom.xml中的配置:


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>

  <groupId>com.wbbgroupId>
  <artifactId>SsmartifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>warpackaging>

  <name>Ssm Maven Webappname>
  
  <url>http://www.example.comurl>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.7maven.compiler.source>
    <maven.compiler.target>1.7maven.compiler.target>
  properties>

  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.11version>
      <scope>testscope>
    dependency>

    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.37version>
    dependency>

    <dependency>
      <groupId>c3p0groupId>
      <artifactId>c3p0artifactId>
      <version>0.9.1.2version>
    dependency>
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>3.3.0version>
    dependency>
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>1.2.3version>
    dependency>
  <dependency>
    <groupId>javax.servletgroupId>
    <artifactId>javax.servlet-apiartifactId>
    <version>3.1.0version>
  dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.coregroupId>
    <artifactId>jackson-coreartifactId>
    <version>2.5.4version>
  dependency>
  <dependency>
    <groupId>com.fasterxml.jackson.coregroupId>
    <artifactId>jackson-annotationsartifactId>
    <version>2.5.0version>
  dependency>
  <dependency>
    <groupId>com.fasterxml.jackson.coregroupId>
    <artifactId>jackson-databindartifactId>
    <version>2.5.4version>
  dependency>
  
  
  <dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-coreartifactId>
    <version>4.1.7.RELEASEversion>
  dependency>
  <dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-beansartifactId>
    <version>4.1.7.RELEASEversion>
  dependency>
  <dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-contextartifactId>
    <version>4.1.7.RELEASEversion>
  dependency>
  
  <dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-jdbcartifactId>
    <version>4.1.7.RELEASEversion>
  dependency>
  <dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-txartifactId>
    <version>4.1.7.RELEASEversion>
  dependency>
  
  <dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-webartifactId>
    <version>4.1.7.RELEASEversion>
  dependency>
  <dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-webmvcartifactId>
    <version>4.1.7.RELEASEversion>
  dependency>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.12version>
      <scope>compilescope>
    dependency>
  dependencies>

  <build>
    <finalName>SsmfinalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-clean-pluginartifactId>
          <version>3.1.0version>
        plugin>
        
        <plugin>
          <artifactId>maven-resources-pluginartifactId>
          <version>3.0.2version>
        plugin>
        <plugin>
          <artifactId>maven-compiler-pluginartifactId>
          <version>3.8.0version>
        plugin>
        <plugin>
          <artifactId>maven-surefire-pluginartifactId>
          <version>2.22.1version>
        plugin>
        <plugin>
          <artifactId>maven-war-pluginartifactId>
          <version>3.2.2version>
        plugin>
        <plugin>
          <artifactId>maven-install-pluginartifactId>
          <version>2.5.2version>
        plugin>
        <plugin>
          <artifactId>maven-deploy-pluginartifactId>
          <version>2.8.2version>
        plugin>
      plugins>
    pluginManagement>
  build>
project>

注意其中:

javax.servlet
javax.servlet-api
3.1.0

这里servlet-api是3.1.0版本对应的web.xml也要修改
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_3_1.xsd"version="3.1" metadata-complete="true">
其中webapp后面就要用3.1

  • 然后先根据目录结构将要建立的包建好
  • 首先在main下新建一个java文件夹右击mark Directory as -> source root
    如图:
    IDEA SSM简单整合_第2张图片
    这里的java文件夹是存放java代码,
  • 在java文件内新建包wbb.entity , wbb.dao , wbb.service , wbb.service.impl , wbb.web
  • 在main下新建文件夹resources存放xml文件,同样要右击mark Directory as->resource root
  • 在resources文件夹下新建mapper和spring文件夹存放对数据库操作的配置和springmvc的配置
  • 然后编写配置文件web.xml
    <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_3_1.xsd"
    version="3.1" metadata-complete="true">
      <display-name>Archetype Created Web Applicationdisplay-name>
    
      
      
      <servlet>
        <servlet-name>DispatcherServletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        
        <init-param>
          <param-name>contextConfigLocationparam-name>
          <param-value>classpath:spring/spring-*.xmlparam-value>
        init-param>
      servlet>
      <servlet-mapping>
        <servlet-name>DispatcherServletservlet-name>
        
        <url-pattern>/url-pattern>
      servlet-mapping>
    
    web-app>
    
  • 在resources下新建mybatis-config.xml并配置以下内容
    
    
    <configuration>
        
        <settings>
          
            <setting name="logImpl" value="STDOUT_LOGGING" />
            <setting name="lazyLoadingEnabled" value="true" />
            <setting name="aggressiveLazyLoading" value="false" />
    
            
            <setting name="useGeneratedKeys" value="true" />
    
            
            <setting name="useColumnLabel" value="true" />
    
            
            <setting name="mapUnderscoreToCamelCase" value="true" />
        settings>
    configuration>
    
  • 然后在spring目录下新建spring-web.xml和spring-dao.xml
    spring-web.xml的内容:
    <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"
    xmlns:contex="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
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        
        
        
        <mvc:annotation-driven>mvc:annotation-driven>
        <mvc:default-servlet-handler>mvc:default-servlet-handler>
        
        
        <context:component-scan base-package="wbb.service"/>
        
        <context:component-scan base-package="wbb.dao"/>
        
        <context:component-scan base-package="wbb.service.impl"/>
        
        <context:component-scan base-package="wbb.web"/>
    
    beans>
    

spring-dao.xml内容:


<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
http://www.springframework.org/schema/context/spring-context.xsd">
    
    
    <context:property-placeholder location="classpath:jdbc.properties" />

    
    <bean id="dataSource" 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}" />

        
        <property name="maxPoolSize" value="30" />
        <property name="minPoolSize" value="10" />
        
        <property name="autoCommitOnClose" value="false" />
        
        <property name="checkoutTimeout" value="10000" />
        
        <property name="acquireRetryAttempts" value="2" />
    bean>


    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource" />
        
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        
        
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        
        <property name="basePackage" value="wbb.dao" />
bean>
beans>

其中可以配置jdbc.properties也可以写死在此配置中,这里在resources目录下新建jdbc.properties文件并配置一下内容
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=你的密码

  • 然后在entity中新建类例如Book类
    下面的内容只需要写好数据库中对应的属性然后alt+insert快捷生成构造方法就行了
    package wbb.entity;
    
    public class Book {
      //Book中的信息,跟数据库表中信息对应
        private String id;
        private String name;
        private String author;
        private String publisher;
    //下面可以通过idea自带的alt+insert选择getter+setter快捷生成
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getAuthor() {
            return author;
        }
    
        public void setAuthor(String author) {
            this.author = author;
        }
    
        public String getPublisher() {
            return publisher;
        }
    
        public void setPublisher(String publisher) {
            this.publisher = publisher;
        }
    }
    
  • 然后写dao层,在dao目录下新建Bookdao接口类
    package wbb.dao;
    
    import org.springframework.stereotype.Component;
    import wbb.entity.Book;
    
    import java.util.List;
    //通过Component将BookDao内容注入spring容器
    @Component
    public interface BookDao {
      //先随便写个方法,获取全部信息要用List
        public List<Book> getAll();
    }
    
    • 然后写dao对应的mapper,在resources目录下的mapper目录下新建BookMapper.xml文件:
      
      
        
    
    select * from book ```
  • 在service目录下新建BookService接口类对应BookDao的方法
    package wbb.service;
    
    
    import wbb.entity.Book;
    
    import java.util.List;
    
    public interface BookService {
        /**
         * 获取所有的书
         * @return
         */
       public List<Book> getAllb();
    }
    
  • 然后在service.impl目录下新建service接口的实现类BookServiceImpl.xml
    package wbb.service.impl;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import wbb.dao.BookDao;
    import wbb.entity.Book;
    import wbb.service.BookService;
    
    import java.util.List;
    
    //通过下面注解自动注入spring容器
    @Service("bookService")
    public class BookServiceImpl implements BookService {
      //注入bookDao
        @Autowired
        private BookDao bookDao;
    
        @Override
        public List<Book> getAllb() {
          //用于测试service层是否注入成功
            System.out.println("查询");
           return bookDao.getAll();
        }
    }
    
  • 最后写web层或Controller,在web目录下新建BookController类
    package wbb.web;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import wbb.entity.Book;
    import wbb.service.BookService;
    
    import java.util.List;
    
    //下面注解也可以使用Controller,@RestController注解是@Controller和@ResponseBody注解的结合。表明这是一个控制器类
    @RestController
    public class BookController {
      //注入BookService
        @Autowired
        private BookService bookService;
    //RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
        @RequestMapping("/getAll")
        public List<Book> getAll(){
            return bookService.getAllb();
        }
    }
    

运行测试:
这里可以用maven也可以用tomcat
maven配置tomcat只需要像配置tomcat一样新建一个maven然后在Parameters栏里面的Command line:中输入tomcat:run
然后运行等自动下载配置完成就行
如图:
IDEA SSM简单整合_第3张图片
其中输出的是json类型的,如果没有以下jar包可能会报错

com.fasterxml.jackson.core
jackson-core
2.5.4


com.fasterxml.jackson.core
jackson-annotations
2.5.0


com.fasterxml.jackson.core
jackson-databind
2.5.4

你可能感兴趣的:(SSM相关学习)