SSM整合小Demo

SSM整合小Demo

开发工具

  1. IDEA2020
  2. Maven3.6.3
  3. MySql

准备环境 配置文件 jar包

我的pom.xml里面这次用到的一些jar包的坐标如下

 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <spring.version>4.1.1.RELEASE</spring.version> //控制Jar包的版本
  </properties>
<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>


  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${spring.version}</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</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-aop</artifactId>
    <version>${spring.version}</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${spring.version}</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</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-tx</artifactId>
    <version>${spring.version}</version>
  </dependency>
  <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.0</version>
  </dependency>
  <dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
  </dependency>
  <dependency>
    <groupId>org.apache.taglibs</groupId>
    <artifactId>taglibs-standard-impl</artifactId>
    <version>1.2.5</version>
  </dependency>
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.48</version>
  </dependency>
  <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.4</version>
  </dependency>
  <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.4</version>
  </dependency>
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.6</version>
  </dependency>
  <dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.9.1</version>
  </dependency>
  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
  </dependency>
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.25</version>
  </dependency>

  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.8</version>
  </dependency>
  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.8</version>
  </dependency>
  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.0</version>
  </dependency>

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

</dependencies>

最最最基本的数据库配置文件

jdbc.driver=com.mysql.jdbc.Driver    
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=ok

这两个基本的配置文件完成之后,开始正式进我小Demo的开发,苦笑哈哈。

今天的这个SSM整合小Demo很简单,一个对图书的查询,一个对图书的添加。非常的简单基础,主要是练习一下

SSM的整合。、

后台代码实现

首先我们从实体类开始

这里很简单四个属性 三个注解把它搞定,我来解释这三个注解

  1. @Data 封装属性 让所有的属性生成Getter Setter方法方法
  2. @NoArgsConstructor 生成无参的构造方法
  3. @AllArgsConstructor 生成带参的构造方法

使用这三个注解 我们需要导入lombok的jar包坐标,上面pom.xml里面有,并且IDEA需要安装Lombok的插件

package com.it.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book {

    private  int bid;
    private String bname;
    private int bcount;
    private int bmoney;
    private String bauthor;

}

Dao层AndMapper

dao层就简单的两个方法

mapper也是对应的映射

package com.it.dao;
import com.it.entity.Book;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository   //使这个类被spring去管理
public interface BookDao {
     List<Book>  findListBook();
     void  addBook(Book book);
}
//Mapper.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.it.dao.BookDao">
    <resultMap id="BookReslut" type="Book">
        <result property="bid" column="bid" ></result>
        <result property="bname" column="bname" ></result>
        <result property="bcount" column="bcount" ></result>
        <result property="bmoney" column="bmoney" ></result>
        <result property="bauthor" column="bauthor" ></result>
    </resultMap>
	<!--查询-->
     <select id="findListBook" resultMap="BookReslut">
        select *from Book
     </select>
     <!--添加-->
    <insert id="addBook" parameterType="Book">
        insert into book values(#{bid},#{bname},#{bcount},#{bmoney},#{bauthor})
    </insert>
</mapper>
  

Service And Impl

额,这里也没什么好说的。哭笑哭笑

package com.it.service;
import com.it.entity.Book;
import java.util.List;
public interface BookService {
    List<Book> findListBook();
    void  addBook(Book book);
}
//实现类
package com.it.service.impl;
import com.it.dao.BookDao;
import com.it.entity.Book;
import com.it.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    BookDao bookDao;    
    @Override
    public List<Book> findListBook() {
        System.out.println("这里是Service");
        return bookDao.findListBook();
    }
    @Transactional   //基本的事务注解
    @Override
    public void addBook(Book book) {
        bookDao.addBook(book);
    }
}

Controller层

package com.it.controller;
import com.it.entity.Book;
import com.it.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class BookController {
     @Autowired
     BookService bookService;
     @RequestMapping("QueryListBook")
     public  String  QuerylistBook(ModelMap model)
     {
         System.out.println("这里是Controller");
         List<Book> bookList= bookService.findListBook();
         model.put("bookList",bookList);
         return  "list";
     }
     //添加学生
     @RequestMapping("AddBook")
     public  String AddBook(Book book)
     {
         bookService.addBook(book);
         return  "redirect:/QueryListBook"; //添加成功  重定向到查询页面
     }
	 }

那么后台实现就先到这里,下面来配置文件。

配置文件

**先来看Dao层数据源方面的配置 **

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
">
    <context:property-placeholder location="classpath:database.properties"/>
    <!--开启注解支持-->
     <mvc:annotation-driven></mvc:annotation-driven>
    <!--扫描dao层的包-->
     <context:component-scan base-package="com.it.dao">
     </context:component-scan>
    <!--配置数据源-->
     <bean id="datadource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
          <property name="driverClass" value="${jdbc.driver}"/>
          <property name="jdbcUrl" value="${jdbc.url}"></property>
          <property name="user" value="${jdbc.username}" ></property>
          <property name="password" value="${jdbc.password}"></property>
      </bean>
    <!--配置工厂-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean" >
        <property name="dataSource" ref="datadource"/>
        <property name="typeAliasesPackage" value="com.it.entity" />
    </bean>
    <!--Dao接口的扫描包-->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean" ></property>
        <property name="basePackage" value="com.it.dao"></property>
   </bean>
</beans>

Service层的配置文件

这里就很简单 对service业务层添加注解的支持 包的扫描 和事务的注解

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
">
   
     <!--开启注解支持-->
     <mvc:annotation-driven></mvc:annotation-driven>
     <context:component-scan base-package="com.it.service"/>
    <!--增加事务-->
         
          <!--这里需要用到datasource数据源   但是数据源配置在spring-dao的xml里 所以我们需要引入-->
         
         <!--引入spring -dao的xml-->
    <import resource="classpath:spring-dao.xml"></import>
         
    <bean id="transactionManager" 				      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="datadource"></property>
    </bean>
</beans>

Spring MVC层的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
">
	 <!--Controller层的扫描->
     <mvc:annotation-driven></mvc:annotation-driven>
     <context:component-scan base-package="com.it.controller"></context:component-scan>
     
      <!--视图解析器的配置-->
     <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix" value="/WEB-INF/pages/" ></property>
        <property name="suffix" value=".jsp"></property>
     </bean>
</beans>

还有一个application.xml的配置文件就不放出来了,

那我我做的事情就是 引入刚才的所有的配置就OK了,然 后在容器在应用启动时就加载并初始化这个servlet

加载这个文件就行了,那么看下web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
    
    <!--配置字符编码过滤器-->
  <filter>
    <filter-name>charecter</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>charecter</filter-name>
     <url-pattern>/*</url-pattern>
   </filter-mapping>
  <!--*/配置前端控制器-->
  <servlet>
    <servlet-name>Dispathcer</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>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Dispathcer</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

ok 这个小Demo到这里就差不多结束啦。

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页