一、需求分析

二、搭建数据库

  • 建立数据库 数据表 数据内容 自行添加

三、创建一个maven项目,添加web支持(直接创建webapp项目也可)

四、导入相关依赖包(pom.xml)

  • 分析可能需要有关junit、数据库、数据库连接池、servlet、jsp、spring、mybatis-spring等包
<dependencies>
    <!-- 测试依赖 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <!-- mysql连接依赖 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.16</version>
    </dependency>
    <!-- 数据库连接池依赖 -->
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>

    <!-- mybatis依赖 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.4</version>
    </dependency>
    <!-- mybatis整合spring依赖 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.2</version>
    </dependency>
    <!-- spring整合springmvc依赖 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>
    <!-- spring依赖 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>
    <!-- servlet依赖 -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>
    <!-- jsp依赖 -->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.3.1</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.12</version>
    </dependency>
  </dependencies>
  • 导入一些方便使用的包(要先下载lombok插件,自行在idea中下载即可)
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
</dependency>

五、解决静态资源导出问题(pom.xml)

  • 在pom.xml文件里面配置, 配置文件一般多在resources,但有时为了方便管理,不止在resources中写入配置文件,也可能在src文件里面进行配置文件的置入,而当我们将配置文件写入到src文件时则无法导出到target,而导致程序发生错误 。
<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

六、idea里面连接数据库

  • 注意:[使用idea连接数据库时如果报错 Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezon'
  • 原因: mysql数据库的时区设置错误
  • 解决办法:

①.Win+R 输入cmd 打开控制台

②.打开数据库, 输入:

mysql -uroot -p

③.查看数据库是否设置时区

show variables like '%time_zone%'; mysql> show variables like '%time_zone%';+------------------+--------+| Variable_name   | Value |+------------------+--------+| system_time_zone |     || time_zone     | SYSTEM |+------------------+--------+2 rows in set, 1 warning (0.00 sec)在 time_zone 中显示 SYSTEM 就是没有设置时区

④.给数据库设置正确的时区

set global time_zone = '+8:00'; flush

此时再查看时区

mysql> show variables like '%time_zone%'; +--------------------------+-------------+ | Variable_name  | Value | +-------------------------+-------------+ | system_time_zone |     | | time_zone    | +08:00 | +------------------------+--------------+ 2 rows in set, 1 warning (0.00 sec)

如果time_zone还是没有正确显示则重新启动mysql数据再查看一下

此时idea能成功连接数据库

七、建相关package

  • src/main/java/com.yj.pojo
  • src/main/java/com.yj.mapper(dao)
  • src/main/java/com.yj.service
  • src/main/java/com.yj.controller
  • src/main/resources/xxx.xml等

八、在包中建立相关的类

1.在pojo包内创建实体类

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

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {
    private int bookID;
    private String bookName;
    private int bookCount;
    private String detail;
}

2.在mapper中建立接口




package com.kuang.yj.mapper;

import com.kuang.yj.pojo.Books;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface BookMapper {
    //增加一本书
    int addBook(Books books);

    //删除一本书
    int deleteBookById(@Param("bookId") int id);

    //更新一本书
    int updateBook(Books books);

    //查询一本书
    Books queryBookById(@Param("bookId") int id);

    //查询全部的书
    List<Books> queryAllBook();
}




3.在mapper中建立(UserMapper.xml)文件写sql语句进行数据库的交互

<?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.kuang.yj.mapper.BookMapper">

    <insert id="addBook" parameterType="int">
        insert into ssm.books (bookName, bookCounts, detail)
        values (#{bookName},#{bookCounts},#{detail})
    </insert>

    <delete id="deleteBookById" parameterType="int">
        delete from ssm.books
        where bookID = #{bookId}
    </delete>

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

    <select id="queryBookById" resultType="Books">
        select * from ssm.books
        where bookID = #{bookId}
    </select>

    <select id="queryAllBook" resultType="books">
        select * from ssm.books
    </select>


</mapper>




4.建立service层接口

  • service层实现的业务和mapper(dao)层实现的业务没有本质区别.
package com.kuang.yj.service;

import com.kuang.yj.pojo.Books;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface BookService {
    //增加一本书
    int addBook(Books books);

    //删除一本书
    int deleteBookById(@Param("bookId") int id);

    //更新一本书
    int updateBook(Books books);

    //查询一本书
    Books queryBookById(@Param("bookId") int id);

    //查询全部的书
    List<Books> queryAllBook();
}
  • service的实现类
package com.kuang.yj.service.impl;

import com.kuang.yj.mapper.BookMapper;
import com.kuang.yj.pojo.Books;
import com.kuang.yj.service.BookService;

import java.awt.print.Book;
import java.util.List;

public class BookServiceImpl implements BookService {
    //service调mapper(dao)层
    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 queryBookById(int id) {
        return bookMapper.queryBookById(id);
    }

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

九、整合Mybatis

  • 建立mybatis配置文件
<?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>
<!--    配置数据源,交给spring去做-->
<!--    起别名-->
    <typeAliases>
        <package name="com.kuang.yj.pojo"/>
    </typeAliases>

<!--    绑定mapper 如果mapper文件的名字和.xml文件的名字不一样 用resources定位.xml文件就可以了-->
    <mappers>
        <mapper class="com.kuang.yj.mapper"/>
    </mappers>

</configuration>
  • 配置db.properties数据库连接信息
mysql.driver=com.mysql.cj.jdbc.Driver
#如果使用的是mysql8.0+的话就要增加这个时区的配置serverTimezone=UTC
mysql.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
mysql.username=root
mysql.password=123456

十、整合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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

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

</beans>
  • 整合dao层--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">
<!--1.关联数据库配置文件 import只可以导spring配置文件 例如:applicationContext.xml-->
    <context:property-placeholder location="classpath:db.properties"/>
<!--    2.连接池使用c3p0
        dbcp:半自动化操作,不能自动连接
        c3p0:自动化操作(自动化的加载配置文件,并且可以自动设置到对象中!)
        druid:
        hikari:
-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${mysql.driver}"/>
        <property name="jdbcUrl" value="${mysql.url}"/>
        <property name="user" value="${mysql.username}"/>
        <property name="password" value="${mysql.password}"/>
    </bean>
<!--    3.sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
<!--        绑定Mybatis的配置文件-->
        <property name="configLocation" value="mybatis-config.xml"/>
    </bean>

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

</beans>
  • 整合service层--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"
       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">

<!--    1.扫描service下的包-->
    <context:component-scan base-package="com.kuang.yj.service"/>
<!--    2.将我们的所有业务类注入到spring,可以通过配置,或者通过注解实现-->
    <bean id="BookServiceImpl" class="com.kuang.yj.service.impl.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    </bean>

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

<!--    4.aop事务支持-->
</beans>

十一、整合springMVC

  • 配置web.xml文件
<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
<!--  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:spring-mvc.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>15</session-timeout>
  </session-config>

</web-app>
  • 配置spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

<!--1.注解驱动-->
    <mvc:annotation-driven/>
<!--    2.静态资源过滤-->
    <mvc:default-servlet-handler/>
<!--    3.扫描包:controller-->
    <context:component-scan base-package="com.kuang.yj.controller"/>
<!--    4.视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

十二、完结撒花

至此,SSM框架底层全部整合完毕,可以在此基础上进行操作了。