前言:

本文内容:Fastjson使用、ssm整合Mybatis层、ssm整合Spring层

推荐免费SpringMVC基础教程视频:【狂神说Java】SpringMVC最新教程IDEA版通俗易懂_哔哩哔哩_bilibili

Fastjson使用

fastjson.jar是阿里巴巴开发的一款专门用于Java开发的包,可以方便的实现json对象与JavaBean对象的转换,实现JavaBean对象与json字符串的转换,实现json对象与json字符串的转换。

fastjson的pom.xml依赖

1
2
3
4
5
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>

fastjson三个主要的类:

  • JSONObject代表json对象
    • JSONObject实现了Map接口
    • JSONObject对应的json对象,同构各种形式的get()方法可以获取json对象中的数据,也可以利用size(),isEmpty()等方法获取"键:值"对的个数和判断是否为空。本质是通过实现Map接口并调用接口中的方法完成的
  • JSONArray代表json对象数组
    • 内部是List接口中的方法来完成操作的。
  • JSON代表JSONObjectJSONArray的转化
    • JSON类源码分析与使用
    • 观察这些方法,主要是实现json对象,json对象数组,javabean对象,json字符串之间的相互转化

简单测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@RequestMapping("/j5")
@ResponseBody // 加这个注解就不会走视图解析器,会直接返回一个字符串
public String json5(){


List<User> list = new ArrayList<>();

User user1 = new User("小王1",18,"男");
User user2 = new User("小王2",18,"女");
User user3 = new User("小王3",18,"男");

list.add(user1);
list.add(user2);
list.add(user3);

String str = JSON.toJSONString(list);

return str;
}

运行结果

1
[{"age":18,"name":"小王1","sex":"男"},{"age":18,"name":"小王2","sex":"女"},{"age":18,"name":"小王3","sex":"男"}]

ssm整合:Mybatis层

环境要求

  • IDEA
  • MySQL 8.0.22
  • Tomcat 8.5
  • Maven 3.6

要求熟练掌握MySQL数据库,Spring,JavaWeb以及Mybatis,简单前端知识

数据库环境

创建书籍数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
create database `ssmbuild`;

use `ssmbuild`

drop table if exists `books`;

create table `books`(
`bookID` int(10) not null auto_increment comment '书本id',
`bookName` varchar(100) not null comment '书名',
`bookCounts` int(11) not null comment '数量',
`detail` varchar(200) not null comment '描述',
key `bookID`(`bookID`)
)ENGINE=INNODB DEFAULT CHARSET=utf8;

insert into `books`(`bookID`,`bookName`,`bookCounts`,`detail`)values(1,'Java',1,'从入门到板砖'),(2,'MySQL',10,'从入门到跑路'),(3,'Linux',6,'从入门到落网')

基本环境搭建

  1. 新建Maven项目ssmBuild

  2. 在pom引入依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59

    <!-- 引入依赖-->
    <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    </dependency>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.22</version>
    </dependency>
    <dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.5</version>
    </dependency>
    <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.1</version>
    </dependency>
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    </dependency>
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.2</version>
    </dependency>
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.8</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.19</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.3.8</version>
    </dependency>
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.24</version>
    </dependency>
    </dependencies>
  3. Maven静态资源导出

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <!--    静态资源导出-->
    <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>
  4. 建立基本结构和配置框架

    • com.jokerdig.pojo

    • com.jokerdig.controller

    • com.jokerdig.mapper

    • com.jokerdig.service

    • mybatis-config.xml

      1
      2
      3
      4
      5
      6
      <?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>

      </configuration>
    • applicationContext.xml

      1
      2
      3
      4
      5
      6
      <?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">

      </beans>
  5. 在IDEA中连接ssmbuild数据库

  6. 编写database.properties

    1
    2
    3
    4
    jdbc.driver=com.mysql.cj.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&serverTimezone=UTC
    jdbc.username=root
    jdbc.password=123456
  7. 配置mybatis-config.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <?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.jokerdig.pojo"/>
    </typeAliases>

    <!-- 绑定配置文件-->
    <mappers>
    <mapper class="com.jokerdig.mapper.BookMapper"/>
    </mappers>

    </configuration>
  8. 新建实体类Books.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    package com.jokerdig.pojo;

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

    /**
    * @author Joker大雄
    * @data 2022/6/11 - 15:50
    **/
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Books {
    private int bookID;
    private String bookName;
    private int bookCounts;
    private String detail;
    }
  9. 新建接口BookMapper.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    package com.jokerdig.mapper;

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

    import java.util.List;

    /**
    * @author Joker大雄
    * @data 2022/6/11 - 15:55
    **/
    public interface BookMapper {
    // 增加书籍
    int addBook(Books books);
    // 删除书籍
    int deleteBook(@Param("bookId") int id);
    // 修改书籍
    int updateBook(Books books);
    // 查询单本书籍
    Books getBookById(@Param("bookId") int id);
    // 查询全部书籍
    List<Books> queryAllBooks();
    }
  10. 新建BookMapper.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    <?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.jokerdig.mapper.BookMapper">
    <!-- 增删改查-->
    <insert id="addBook" parameterType="Books">
    insert into books(bookName,bookCounts,detail) values(#{bookName},#{bookCounts},#{detail})
    </insert>

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

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

    <select id="getBookById" parameterType="int" resultType="Books">
    select * from books where bookID=#{bookId}
    </select>

    <select id="queryAllBooks" resultType="Books">
    select * from books
    </select>
    </mapper>
  11. 新建接口BookService.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package com.jokerdig.service;

    import com.jokerdig.pojo.Books;

    import java.util.List;

    /**
    * @author Joker大雄
    * @data 2022/6/11 - 16:12
    **/
    public interface BookService {
    // 增加书籍
    int addBook(Books books);
    // 删除书籍
    int deleteBook(int id);
    // 修改书籍
    int updateBook(Books books);
    // 查询单本书籍
    Books getBookById(int id);
    // 查询全部书籍
    List<Books> queryAllBooks();
    }
  12. 新建接口实现类BookServiceImpl.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    package com.jokerdig.service;

    import com.jokerdig.mapper.BookMapper;
    import com.jokerdig.pojo.Books;

    import java.util.List;

    /**
    * @author Joker大雄
    * @data 2022/6/11 - 16:13
    **/
    public class BookServiceImpl implements BookService {

    // mapper set注入
    private BookMapper bookMapper;

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

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

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

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

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

    @Override
    public List<Books> queryAllBooks() {
    return bookMapper.queryAllBooks();
    }
    }
  13. Mybatis层整合完毕

ssm整合:Spring层

  1. 新建spring-mapper.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    <?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:property-placeholder location="classpath:database.properties"/>
    <!-- 连接池
    dbcp:半自动化操作 不能自动连接
    c3p0:自动化的加载配置文件
    druid hikari
    -->
    <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"/>
    </bean>
    <!-- SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <!-- 绑定mybatis配置文件-->
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!-- 配置扫描包-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 注入sqlSessionFactory-->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    <!-- 扫描包-->
    <property name="basePackage" value="com.jokerdig.mapper"/>
    </bean>
    </beans>
  2. 新建spring-service.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    <?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">

    <!-- 整合service-->
    <!-- 扫描service下的包-->
    <context:component-scan base-package="com.jokerdig.service"/>
    <!-- 将所有业务类注入spring 可以通过配置或者注解实现-->
    <bean id="BookServiceImpl" class="com.jokerdig.service.BookServiceImpl">
    <property name="bookMapper" ref="bookMapper"/>
    </bean>
    <!-- 声明式事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!-- 注入数据源-->
    <property name="dataSource" ref="dataSource"/>
    </bean>
    </beans>
  3. service层整合完毕