SSM的整合

SSM的整合

1.先创建数据库

SSM的整合
数据自行插入

2.导入JAR包

SSM的整合
这里是用jar包,也可以用maven导包那样更方便
注意out目录下也到导包
SSM的整合
不然整个程序运行不了

3.创建包

SSM的整合

4.创建实体类

在pojo包里面创建Books类

package com.atguigu.ssm.pojo;

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

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {

   private int bookID;
   private  String bookName;
   private int bookCounts;
   private  String detail;
}

5.编写dao层

1.创建接口BookMapper
在dao包里面创建接口BookMapper

public interface BookMapper {
    int addBook(Books books);//   //增加一个Book

    int deleteBookByID(@Param("bookID")int id);根据id删除一个Book

    int updateBook(Books books);  //更新Book

    Books queryBooksByID(@Param("bookID")int id);根据id查询,返回一个Book

    List<Books> queryAllBook();//   //查询全部Book,返回list集合

}

2.编写对应的mapper文件
在dao包里面创建BookMapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.ssm.dao.BookMapper">


    <insert id="addBook" parameterType="com.atguigu.ssm.pojo.Books">
        insert  into books(bookName,bookCounts,detail)
        values (#{bookName},#{bookCounts},#{detail})
        where bookID = #{bookID}
    </insert>

    <delete id="deleteBookByID" parameterType="com.atguigu.ssm.pojo.Books">
        delete from books where bookID=#{bookID}
    </delete>

    <update id="updateBook" parameterType="com.atguigu.ssm.pojo.Books">
        update books set bookName={bookName}, bookCounts=#{bookCounts}, detail=#{detail}
        where bookID=#{bookID}
    </update>

    <select id="queryBooksByID" resultType="com.atguigu.ssm.pojo.Books">
        select * from books where BookID =#{BookID}
    </select>

    <select id="queryAllBook" resultType="com.atguigu.ssm.pojo.Books">
        select * from books;
    </select>
</mapper>

6.编写service层

1.在service包里面创建BookService接口

public interface BookService {
    int addBook(Books books);

    int deleteBookByID(int id);

    int updateBook(Books books);

    Books queryBooksByID(int id);

    List<Books> queryAllBook();
}

2.实现接口
在service包里面创建BookServiceImpl类实现接口

public class BookServiceImpl implements BookService {
    private BookMapper bookMapper;

    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }

    @Override
    public int addBook(Books books) {
        return bookMapper.addBook(books);
    }

    @Override
    public int deleteBookByID(int id) {
        return bookMapper.deleteBookByID(id);
    }

    @Override
    public int updateBook(Books books) {
        return bookMapper.updateBook(books);
    }

    @Override
    public Books queryBooksByID(int id) {
        return bookMapper.queryBooksByID(id);
    }

    @Override
    public List<Books> queryAllBook() {
        return bookMapper.queryAllBook();
    }
}

7.编写spring层
可以在src目录下创建先创建五个xml文件
SSM的整合

7.编写mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>


    <mappers>
        <mapper resource="com/atguigu/ssm/dao/BookMapper.xml"/>
    </mappers>

</configuration>

8.spring整合mybatis

spring-dao.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">

<!--这里配置可能和你的不太一样-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/test"/>
        <property name="username" value="root"/>
        <property name="password" value="a123123"/>
    </bean>

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

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

</beans>

9.spring整合service层

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">

 <context:component-scan base-package="com.atguigu.ssm.service"/>

   <bean id="BookServiceImpl" class="com.atguigu.ssm.service.BookServiceImpl">
       <property name="bookMapper" ref="bookMapper"/>
   </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

</beans>

10.编写spring-mvc.xml文件

spring-mvc:

<?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:mvc="http://www.springframework.org/schema/mvc"
       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
   https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven/>  <!-- 1.开启SpringMVC注解驱动 -->
    <mvc:default-servlet-handler/>   <!-- 2.静态资源默认servlet配置-->
    <!-- 3.配置jsp 显示ViewResolver视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    
    <context:component-scan base-package="com.atguigu.ssm.controller"/>
    
</beans>

11.Spring配置整合

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"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="spring-dao.xml"/>
    <import resource="spring-service"/>
    <import resource="spring-mvc.xml"/>
</beans>

12.编写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">

<servlet>
    <servlet-name>springMvc</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>springMvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

<session-config>
    <session-timeout>15</session-timeout>
</session-config>
</web-app>

14.编写controller与视图层

1.在controller包里面编写Bookcontroller类查询全部书籍:

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    @RequestMapping("/allBook")
    public String list(Model model){
        List<Books> list = bookService.queryAllBook();
        model.addAttribute("list",list);
        return "allBook";
    }
}

2.在/WEB-INF/jsp/目录下编写allBook.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${list}
</body>
</html>

3.编写index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>首页</title>
      <style>
          a{
              text-decoration: none;
              color: black;
              font-size: 18px;
          }
          h3{
              width: 180px;
              height: 38px;
              margin: 100px auto;
              text-align: center;
              line-height: 38px;
              background: deepskyblue;
              border-radius: 10px;
          }
      </style>
  </head>
  <body>
 <h3>
     <a href="${pageContext.request.contextPath}/book/allBook">进入书籍</a>
 </h3>
  </body>
</html>

15.运行结果

SSM的整合
SSM的整合

总结

根据ssm运行流程图可以加深理解
SSM的整合

上一篇:华为手机如何在电视上投屏


下一篇:c语言结构体学习-----简记(2)