美文网首页
SSM整合 基础篇

SSM整合 基础篇

作者: 楊柯林 | 来源:发表于2019-07-11 10:29 被阅读0次

    SSM整合

    需求:通过SSM整合做出对前端基本查询的jsp响应

    1 数据库数据

    数据库数据如下

    Field Type
    id int(10)
    name varchar(20)
    price float(10,0)
    pic varchar(40)
    createtime datetime
    detail varchar(200)

    创建父工程ssm_parent 的maven工程 删掉src包
    gruopId=com.tencent
    导入父工程的pom.xml
    文件目录

    2 后台dao层配置

    2.1 新建domain层

    根据数据库构造实体类

    //Dao层  com.tencent.domain
    public class Item {
        private Integer id;
        private String name;
        private Double price;
        private String pic;
        private Date createtime;
        private String detail;
        //省略set和get方法
        
    
    2.1 新建dao层

    要求查询所有items
    dao层实现 (dao module)
    Module dao下
    创建ItemMapper接口

    //Dao层 com.tencent.itemMapper/
    public interface ItemMapper {
        @Select("select * from items")
        List<Item> findAllItem();
    
    }
    
    <!--Dao层 pom.xml-->
    <?xml version="1.0" encoding="UTF-8"?>
    <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">
        <parent>
            <artifactId>maven_highLevel_parent</artifactId>
            <groupId>com.tencent</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
        <!--添加packaging-->
        <packaging>jar</packaging>
    
        <artifactId>dao</artifactId>
    
    
    </project>
    
    

    写入配置文件

    <!--Web层 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:context="http://www.springframework.org/schema/context"
           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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    ">
    
    <!--写入dao层配置-->
    <!--配置数据源-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"/>
            <property name="jdbcUrl" value="jdbc:mysql:///test"/>
            <property name="user" value="root"/>
            <property name="password" value="123456"/>
        </bean>
    
    
         <!--生产sqlSession,做一个sqlSessionFactory工厂-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <!--sqlSessionFactory扫描mapper层在service层进行mapper注入-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.tencent.mapper"/>
        </bean>
    
        <!--dao层结束-->
    

    针对dao层的测试代码


    3 后台service层配置

    3.1 新建service层

    Module service下
    引入dao层依赖

     <dependencies>
            <dependency>
                <groupId>com.tencent</groupId>
                <artifactId>dao</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    

    创建ItemService接口

    //service层 com.tencent.service/
    public interface ItemService {
        List<Item> findAllItem();
    }
    

    创建ItmServiceImpl实现类

    //service层 com.tencent.service.impl/
    @Service
    @Transactional
    public class ItemServiceImpl implements ItemService {
        @Autowired
        private ItemMapper itemMapper;
    
        public List<Item> findAllItem() {
            List<Item> itemList = itemMapper.findAllItem();
            return itemList;
        }
    }
    
    3.2 service写入配置文件

    service写入配置文件

    <!--service层开始-->
        <context:component-scan base-package="com.tencent.service"/>
    
        <!--声明事务-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <!-- 注册驱动 -->
        <!-- 声明式事务 -->
        <!-- 在service上加上@transactional事务注解配置文件里 -->
        <tx:annotation-driven/>
    
    
    
        <!--service层结束-->
    

    针对service层的测试代码


    4 web层实现

    4.1 web层controller实现

    引入service层依赖

    <dependencies>
            <dependency>
                <groupId>com.tencent</groupId>
                <artifactId>service</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    

    Controller类实现

    @Controller
    @RequestMapping("/item")
    public class ItemController {
        @Autowired
        private ItemService itemService;
    
        @RequestMapping("/findAllItems")
        public String findAllItems(Model model){
            model.addAttribute("items",itemService.findAllItem());
            return "itemDetail";
        }
    }
    
    <!--Web层 springMvc.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:context="http://www.springframework.org/schema/context"
           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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    ">
    
    <!--controller扫描-->
        <context:component-scan base-package="com.tencent.controller"/>
    
    4.2 web层web三大组件实现
    <!--Web层 webapp/web-inf/web.xml-->
    <!-- 引入约束头 -->
    <?xml version="1.0" encoding="UTF-8"?>
    <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_4_0.xsd"
             version="4.0">
    
    <!-- 中文乱码过滤器 -->
    <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>
            <!-- 强力乱码过滤,不管以前编码格式是谁,现在起,都得是utf-8 -->
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>CharacterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <!--前端控制器-->
        <servlet>
            <servlet-name>DispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- 引入springMvc文件 -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-mvc.xml</param-value>
            </init-param>
            <!-- 服务器启动就创建 -->
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>DispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
    
        <!--监听器-->
        <!-- 将applicationContext设在servletContext域中 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
    
    
    4.3 springMvc组件补全
    <!--视图解析器-->
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/pages/"/>
            <property name="suffix" value=".jsp" />
        </bean>
    
    <!-- mvc注解驱动,看到controller -->
        <mvc:annotation-driven />
    <!-- 前端控制器静态资源放行 -->
        <mvc:default-servlet-handler/>
    
    <!--Web层 webapp/pages/itemDetail.jsp-->
    <!-- 引入jstl约束 -->
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    
    
    <form>
            <table width="100%" border=1>
                <c:forEach items="${items}" var="item">
                <tr>
                    <td>商品名称</td>
                    <td> ${item.name } </td>
                </tr>
                <tr>
                    <td>商品价格</td>
                    <td> ${item.price } </td>
                </tr>
                <tr>
                    <td>生成日期</td>
                    <td> <fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/> </td>
                </tr>
                <tr>
                    <td>商品简介</td>
                    <td>${item.detail} </td>
                </tr>
                </c:forEach>
            </table>
    
    <!-- index.jsp -->
    <jsp:forward page="item/findAllItems"></jsp:forward>
    

    启动tomcat 大功告成!

    logo.png

    相关文章

      网友评论

          本文标题:SSM整合 基础篇

          本文链接:https://www.haomeiwen.com/subject/urcrkctx.html