【SpringMVC】SpringMVC基础教程(6)
前言:
本文内容:Fastjson使用、ssm整合Mybatis层、ssm整合Spring层
推荐免费SpringMVC基础教程视频:【狂神说Java】SpringMVC最新教程IDEA版通俗易懂_哔哩哔哩_bilibili
Fastjson使用
fastjson.jar是阿里巴巴开发的一款专门用于Java开发的包,可以方便的实现json对象与JavaBean对象的转换,实现JavaBean对象与json字符串的转换,实现json对象与json字符串的转换。
fastjson的pom.xml
依赖
1 | <dependency> |
fastjson三个主要的类:
JSONObject
代表json对象JSONObject
实现了Map接口JSONObject
对应的json对象,同构各种形式的get()
方法可以获取json对象中的数据,也可以利用size()
,isEmpty()
等方法获取"键:值"对的个数和判断是否为空。本质是通过实现Map接口并调用接口中的方法完成的
JSONArray
代表json对象数组- 内部是List接口中的方法来完成操作的。
- JSON代表
JSONObject
和JSONArray
的转化- JSON类源码分析与使用
- 观察这些方法,主要是实现json对象,json对象数组,javabean对象,json字符串之间的相互转化
简单测试
1 |
|
运行结果
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 | create database `ssmbuild`; |
基本环境搭建
-
新建Maven项目
ssmBuild
-
在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> -
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> -
建立基本结构和配置框架
-
com.jokerdig.pojo
-
com.jokerdig.controller
-
com.jokerdig.mapper
-
com.jokerdig.service
-
mybatis-config.xml
1
2
3
4
5
6
<configuration>
</configuration> -
applicationContext.xml
1
2
3
4
5
6
<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>
-
-
在IDEA中连接
ssmbuild
数据库 -
编写
database.properties
1
2
3
4jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&serverTimezone=UTC
jdbc.username=root
jdbc.password=123456 -
配置
mybatis-config.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<configuration>
<!-- 数据源配置 交给spring-->
<!-- 别名-->
<typeAliases>
<package name="com.jokerdig.pojo"/>
</typeAliases>
<!-- 绑定配置文件-->
<mappers>
<mapper class="com.jokerdig.mapper.BookMapper"/>
</mappers>
</configuration> -
新建实体类
Books.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19package com.jokerdig.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author Joker大雄
* @data 2022/6/11 - 15:50
**/
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
} -
新建接口
BookMapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23package 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(int id);
// 修改书籍
int updateBook(Books books);
// 查询单本书籍
Books getBookById(int id);
// 查询全部书籍
List<Books> queryAllBooks();
} -
新建
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
<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> -
新建接口
BookService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22package 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();
} -
新建接口实现类
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
45package 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;
}
public int addBook(Books books) {
return bookMapper.addBook(books);
}
public int deleteBook(int id) {
return bookMapper.deleteBook(id);
}
public int updateBook(Books books) {
return bookMapper.updateBook(books);
}
public Books getBookById(int id) {
return bookMapper.getBookById(id);
}
public List<Books> queryAllBooks() {
return bookMapper.queryAllBooks();
}
} -
Mybatis层整合完毕
ssm整合:Spring层
-
新建
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
<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> -
新建
spring-service.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<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> -
service层整合完毕
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Hey,Joker!
评论
ValineTwikoo