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框架基本构建结束 –

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值