SSM框架整合

导入依赖

<!--依赖-->
<dependencies>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
<!--数据库连接池c3p0-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<!--Servlet-jsp-->
<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.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.12</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.32</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.23</version>
</dependency>
<!--aop织入包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.19</version>
</dependency>
</dependencies>
<!--静态资源过滤问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.xx.pojo"/>
</typeAliases>
<mappers>
<package name="com.xx.mapper"/>
</mappers>
</configuration>

mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper>

</mapper>

jdbc.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/blog?useSSL=true&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root

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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
">
<!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">-->
<!-- <property name="location" value="classpath:jdbc.properties"/>-->
<!-- </bean>-->
<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}"/>

<!--c3p0连接池私有属性-->
<!--设置最大连接池大小-->
<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"/>
</bean>
<!--mapper层自动接入sqlSessionFactory-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--需要注入sqlSessionFactory的包-->
<property name="basePackage" value="com.xx.mapper"/>
</bean>
</beans>

spring-service.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
">
<!--自动扫描包-->
<context:component-scan base-package="com.xx.service"/>
<!--bean装配-->
<bean id="articleServiceImpl" class="com.xx.service.ArticleServiceImpl">
<!--注入dao-->
<property name="articleMapper" ref="articleMapper"/>
</bean>


<!--创建事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--添加事务-->
<tx:advice id="interceptor" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--aop横切-->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.xx.mapper.ArticleMapper.*(..))"/>
<aop:advisor advice-ref="interceptor" pointcut-ref="pointcut"/>
</aop:config>
</beans>

springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!--注解驱动-->
<!--容器自动注册HandlerMapping和HandlerAdapter-->
<mvc:annotation-driven/>
<!--扫描包-->
<context:component-scan base-package="com.xx.controller"/>
<!--静态资源过滤-->
<mvc:default-servlet-handler/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

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

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">
    <!--DispatcherServlet-->
    <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-->
    <session-config>
        <session-timeout>10</session-timeout>
    </session-config>
</web-app>

 实体类

 mapper

service接口

 业务实现类

 控制层

 页面一:

 页面二:

 效果一:

效果二:

 

添加博客文章

界面

 链接

接口

mapper

控制层

删除博客文章

链接

 接口

 mapper

 控制层

 修改博客(注意点:添加隐藏域来获取博客id

链接

界面

 

 接口

 mapper

 控制层

 

整合代码

实体类

public class Article {
private int id;
private String title;
private String author;
}

mapper接口

public interface ArticleMapper {

    //查询全部博客文章
    List<Article> getAllArticle();
    //根据ID查询博客文章
    Article getArticleById(int id);
    //分页查询博客文章
    List<Article> getAllArticleLimit(Map<String,Object> map);
    //添加博客文章
    int addArticle(Article article);
    //修改博客文章
    int updateArticle(Article article);
    //根据id删除文章
    int deleteArticle(int id);

}

业务接口

public interface ArticleService {

    List<Article> getAllArticle();

    Article getArticleById(int id);

    List<Article> getAllArticleLimit(Map<String,Object> map);

    int addArticle(Article article);

    int updateArticle(Article article);

    int deleteArticle(int id);
}

业务实现类

public interface ArticleService {

    List<Article> getAllArticle();

    Article getArticleById(int id);

    List<Article> getAllArticleLimit(Map<String,Object> map);

    int addArticle(Article article);

    int updateArticle(Article article);

    int deleteArticle(int id);
}

mapper.xml

<select id="getAllArticle" resultType="Article">
        select * from Article
    </select>

    <select id="getArticleById" resultType="Article" parameterType="_int">
        select * from Article where id=#{id}
    </select>

    <select id="getAllArticleLimit" resultType="Article" parameterType="map">
        select * from Article limit #{pageNow},#{pageSize}
    </select>

    <insert id="addArticle" parameterType="Article">
        insert into article(title,author) values(#{title},#{author})
    </insert>

    <update id="updateArticle" parameterType="Article">
        update article set title=#{title},author=#{author} where id=#{id}
    </update>

    <delete id="deleteArticle" parameterType="_int">
        delete from article where id=#{id}
    </delete>

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
<style>
a{
text-decoration: none;
color: black;
font-size: 36px;
}
h3{
margin:auto;
padding: 10px;
text-align: center;
line-height: 38px;
background: skyblue;
width: 180px;
height: 40px;
border-radius: 8px;
}
</style>
</head>
<body>
<h3>
<a href="${pageContext.request.contextPath}/Article/xx">博客</a>
</h3>
</body>
</html>

Article.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>

<div class="container">
    <div class="row">
        <div  class="col-md-4 column">
            <!--指定到servlet地址-->
            <a class="btn btn-info" href="${pageContext.request.contextPath}/Article/addArticle">新增书籍</a>
        </div>
    </div>
    <table class="table table-bordered table-hover">
        <thead>
        <tr>
            <th>博客ID</th>
            <th>标题</th>
            <th>作者</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach var="articleList" items="${articleList}">
            <tr>
                <td>${articleList.id}</td>
                <td>${articleList.title}</td>
                <td>${articleList.author}</td>
                <td>
                    <!--传入id参数-->
                    <a  class="btn btn-info" href="${pageContext.request.contextPath}/Article/updateArticle/${articleList.id}">修改</a>
                         
                    <a class="btn btn-danger" href="${pageContext.request.contextPath}/Article/deleteArticle/${articleList.id}">删除</a>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</div>

</body>
</html>

addArticle.jsp

<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加博客</title>
</head>
<body>
<div class="container">
    <!--action指定servlet地址-->
    <form action="${pageContext.request.contextPath}/Article/add" method="post">
        <div class="form-group">
            <label>标题:</label>
            <!--表单name属性跟实体类名需相同-->
            <input type="text" class="form-control"  placeholder="标题" name="title" required>
        </div>
        <div class="form-group">
            <label>作者名称:</label>
            <input type="text" class="form-control"  placeholder="作者名称" name="author" required>
        </div>
        <div class="form-group">
            <input type="submit" class="form-control btn btn-primary" value="添加">
        </div>
    </form>
</div>
</body>
</html>

updateArticle.jsp

<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>修改</title>
</head>
<body>

<div class="container">
  <form action="${pageContext.request.contextPath}/Article/update" method="post">
    <!--隐藏域input//传入id属性值,根据id修改博客内容-->
    <input type="hidden" name="id" value="${article.id}">
    <div class="form-group">
      <label>标题:</label>
      <!--el表达式获取后端数据-->
      <input type="text" class="form-control"  placeholder="标题" name="title" value="${article.title}" required>
    </div>
    <div class="form-group">
      <label>作者名称:</label>
      <input type="text" class="form-control"  placeholder="作者名称" name="author" value="${article.author}" required>
    </div>r
    <div class="form-goup">
      <input type="submit" class="form-control btn btn-primary" value="修改">
    </div>
  </form>
</div>
</body>
</html>

 

posted @ 2023-04-24 21:16  xiaoxing~  阅读(18)  评论(0)    收藏  举报