IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> SSM基本框架搭建 -> 正文阅读

[开发测试]SSM基本框架搭建

完整项目可以参考:https://gitee.com/cary-zhai/ssmdemo.git
完整笔记:https://note.youdao.com/s/7MdA6JTL

一、数据库搭建

create database `ssmbuild`;

create table `books`(
	`bookID` int(10) not null auto_increment,
	`bookName` varchar(100) not null,
	`bookCounts` int(11) not null,
	`detail` varchar(200) not null,
	key `bookID`(`bookID`)
)engine = innodb default charset=utf8

insert into `books`(`bookID`, `bookName`, `bookCounts`, `detail`) values
(1, 'JavaSE', 100, '入门'),
(2, 'JavaWeb', 100, '进阶'),
(3, 'Spring', 50, '放弃');

二、依赖及问题

文件打包时可能没有静态资源,则需要添加以下配置

<!--静态资源导出-->
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.yml</include>
                <include>**/*.xml</include>
                <include>**/*.tld</include>
                <include>**/*.jsp</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.yml</include>
                <include>**/*.xml</include>
                <include>**/*.tld</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

依赖

<!--依赖-->
<dependencies>
    <!-- Junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.1</version>
        <scope>test</scope>
    </dependency>
    <!-- 数据库驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.21</version>
    </dependency>
    <!-- 数据库连接池:c3p0 -->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.5</version>
    </dependency>

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp.jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- MyBatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.6</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.6</version>
    </dependency>

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.18</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.3.18</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.2.8</version>
    </dependency>
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>
</dependencies>

三、代码

搭建框架 - pojo、dao、service、controller

在pojo包下新建实体类(注意:字段名一致、类名与表名一致)

package com.ssmdemo.pojo;

public class Books {
    private int bookID;
    private String bookName;
    private int bookCounts;
    private String detail;

    public int getBookID() {
        return bookID;
    }

    public void setBookID(int bookID) {
        this.bookID = bookID;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public int getBookCounts() {
        return bookCounts;
    }

    public void setBookCounts(int bookCounts) {
        this.bookCounts = bookCounts;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }
}

在resources下新建applicationContext.xml、spring-dao.xml、spring-service.xml、springmvc-servlet.xml、mybatis-config.xml、jdbc.properties

applicationContext.xml作为容器,导入有关配置文件spring-dao.xml、spring-service.xml、springmvc-servlet.xml

<?xml version="1.0" encoding="UTF8"?>
<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>
    <import resource="spring-service.xml"></import>
    <import resource="springmvc-servlet.xml"></import>

</beans>

spring-dao.xml

<?xml version="1.0" encoding="UTF8"?>
<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:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

    <!-- 连接池 Druid -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!-- sqlsessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--绑定数据源-->
        <property name="dataSource" ref="dataSource"></property>
        <!--绑定MyBatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>

    <!-- 配置dao接口扫描包,动态的实现了dao接口可以注入到Spring容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <!--扫描dao包-->
        <property name="basePackage" value="com.ssmdemo.dao"></property>
    </bean>

</beans>

BookMapper.interface

package com.ssmdemo.dao;

import com.ssmdemo.pojo.Books;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface BookMapper {

    //查询全部书籍
    public List<Books> QueryBooks();

    //查询一本书籍
    public Books QueryBooksById(@Param("bookID") int id);

    //新增一本书籍
    public int InsertBooks(Books books);

    //删除一本书籍
    public int DeleteBooksById(@Param("bookID") int id);

    //修改一本书籍
    public int UpdateBooks(Books books);

}

BookMapper.xml(每次进行新的映射文件创建时,就要及时的再mybatis-config.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.ssmdemo.dao.BookMapper">
    <select id="QueryBooks" resultType="books">
        select * from `books`;
    </select>

    <select id="QueryBooksById" resultType="books">
        select * from `books` where bookID=#{bookID};
    </select>

    <insert id="InsertBooks" parameterType="books">
        insert into `books`(bookID, bookName, bookCounts, detail) values(#{bookID}, #{bookName}, #{bookCounts}, #{detail});
    </insert>

    <delete id="DeleteBooksById" parameterType="int">
        delete from `books` where bookID=#{bookID};
    </delete>

    <update id="UpdateBooks" parameterType="books">
        update `books` set bookName=#{bookName}, bookCounts=#{bookCounts}, detail=#{detail} where bookID=#{bookID};
    </update>

</mapper>

mybatis-config.xml

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

<configuration>

<!--    <settings>-->
<!--        &lt;!&ndash;标准的日志工厂&ndash;&gt;-->
<!--        <setting name="logImpl" value="STDOUT_LOGGING"></setting>-->
<!--    </settings>-->

    <!-- 扫描包并自动生成别名 -->
    <typeAliases>
        <!--<typeAlias type="com.area.pojo.User" alias="User"></typeAlias>-->
        <package name="com.ssmdemo.pojo"/><!--扫描并生成别名映射(pojo类名小写))-->
    </typeAliases>

    <mappers>
<!--        <mapper resource="com/area/dao/UserMapper.xml"/>&lt;!&ndash;映射文件位置,相对于最初包名&ndash;&gt;-->
        <mapper resource="com/ssmdemo/dao/BookMapper.xml"></mapper>
    </mappers>
</configuration>

BooksService.interface

package com.ssmdemo.service;

import com.ssmdemo.pojo.Books;

import java.util.List;

public interface BooksService {
    //查询全部书籍
    public List<Books> QueryBooks();

    //查询一本书籍
    public Books QueryBooksById(int id);

    //新增一本书籍
    public int InsertBooks(Books books);

    //删除一本书籍
    public int DeleteBooksById(int id);

    //修改一本书籍
    public int UpdateBooks(Books books);
}

BookServiceImpl.class

package com.ssmdemo.service;

import com.ssmdemo.dao.BookMapper;
import com.ssmdemo.pojo.Books;

import java.util.List;

public class BookServiceImpl implements BooksService {

    //service调dao层:组合dao层
    private BookMapper bookMapper;

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

    public List<Books> QueryBooks() {
        return bookMapper.QueryBooks();
    }

    public Books QueryBooksById(int id) {
        return bookMapper.QueryBooksById(id);
    }

    public int InsertBooks(Books books) {
        return bookMapper.InsertBooks(books);
    }

    public int DeleteBooksById(int id) {
        return bookMapper.DeleteBooksById(id);
    }

    public int UpdateBooks(Books books) {
        return bookMapper.UpdateBooks(books);
    }
}

spring-service.xml(将业务类注入到Spring中时,需要注意spring-dao.xml配置文件中的扫描dao包这一步骤;注入数据源时,也叶要注意到绑定数据源的步骤)

<?xml version="1.0" encoding="UTF8"?>
<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">

    <!-- 扫描service下的包 -->
    <context:component-scan base-package="com.ssmdemo.service"></context:component-scan>

    <!-- 将所有业务类注入到Spring中 -->
    <bean id="bookServiceImpl" class="com.ssmdemo.service.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"></property>
    </bean>

    <!-- 声明式事务 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入数据源 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- aop事务支持 -->


</beans>

springmvc-servlet.xml(配置文件建设完之后根据文件前缀创建文件夹,后缀是文件根据文件类型创建)

<?xml version="1.0" encoding="UTF8"?>
<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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--自动扫描包(让指定包下的注解生效,由IOC容器统一管理)-->
    <context:component-scan base-package="com.ssmdemo.controller"></context:component-scan>
    <!--让SpringMVC不处理静态资源 .css .js .html .mp4 .mp3 etc. (防止走视图解析器时报错)-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
    <!--
    支持mvc注解驱动
    在Spring中一般采用@RequestMapping注解完成映射关系,要想使@RequestMapping注解生效,
    必须向上下文中注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter实例,
    这两个实例分别在类级别和方法级别处理,而annotation-driver配置帮助我们自动完成上述两个实例的注入。
    -->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--    &lt;!&ndash;处理器映射器&ndash;&gt;-->
    <!--    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>-->
    <!--    &lt;!&ndash;处理器适配器&ndash;&gt;-->
    <!--    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>-->
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <!--路径前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <!--路径后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

Controller.class

package com.ssmdemo.controller;

import com.ssmdemo.pojo.Books;
import com.ssmdemo.service.BookServiceImpl;
import com.ssmdemo.service.BooksService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/bookController")
public class BookController {

    //controller调service层
    @Autowired
    @Qualifier("bookServiceImpl")
    private BooksService booksService;
    public void setBookController(BooksService booksService) {
        this.booksService = booksService;
    }

    //查询全部书籍,并且返回到书籍展示页面
    @RequestMapping("/queryAllBook")
    public String list(Model model){
        Books book = booksService.QueryBooksById(1);

        model.addAttribute("book", book.getBookName());

        return "allBook";
    }

}

新建allBook.jsp页面

配置Tomcat
如果没有lib文件夹则需要手动添加并导入包

– 至此SSM框架基本构建结束 –

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2022-04-15 00:30:46  更:2022-04-15 00:33:18 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/18 0:16:19-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码