SpringBoot
SpringBoot是由Pivotal团队在2013年开始研发、2014年4月发布第一个版本的全新开源的轻量级框架。它基于Spring4.0设计,不仅继承了Spring框架原有的优秀特性,而且还通过简化配置来进一步简化了Spring应用的整个搭建和开发过程。另外SpringBoot通过集成大量的框架使得依赖包的版本冲突,以及引用的不稳定性等问题得到了很好的解决。
Maven多模块
Maven多模块项目,适用于一些比较大的项目,通过合理的模块拆分,实现代码的复用,便于维护和管理。尤其是一些开源框架,也是采用多模块的方式,提供插件集成,用户可以根据需要配置指定的模块。
有时会需要点击 自动导入
可以看到,这样创建的模块是相对干净的,需要我们手动的编写程序启动入口类、需要配置时还得创建配置文件。下一步见证。
添加依赖,这种脚架添加模块不会自动引入相关依赖。这里主要引入父级版本号和spring-boot-starter依赖。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>xyz.java1024</groupId> <artifactId>springboot-ssm</artifactId> <version>1.0-SNAPSHOT</version>
<name>springboot-ssm</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
> 修改启动类,添加注解和修改main启动方法
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class,args);
}
}
> 添加配置文件,先添加resouces文件夹,在创建application.yml配置文件。
![创建配置](https://img-blog.csdnimg.cn/2019121817494781.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTI1NDI1Mg==,size_16,color_FFFFFF,t_70)
![application.yml配置](https://img-blog.csdnimg.cn/20191218175010580.png)
> 细心的同学可以发现 resources 图标没有 黄色的横线
![小问题解决](https://img-blog.csdnimg.cn/20191218175031301.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTI1NDI1Mg==,size_16,color_FFFFFF,t_70)
> 点击上图,进入如图,选择Modules -> 选中模块, 接着选中resources再点击Mark as 中 带黄色横线的Resources即可,点击ok
![选定配置](https://img-blog.csdnimg.cn/20191218175059719.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTI1NDI1Mg==,size_16,color_FFFFFF,t_70)
![选择配置路径](https://img-blog.csdnimg.cn/20191218175130973.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTI1NDI1Mg==,size_16,color_FFFFFF,t_70)
![选择好了](https://img-blog.csdnimg.cn/20191218175156283.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTI1NDI1Mg==,size_16,color_FFFFFF,t_70)
> 同时可见 yml后缀的配置文件的图标也发生了改变
![完美选中](https://img-blog.csdnimg.cn/20191218175220371.png)
> 下面我们开始配置文件,yml 提供更加简洁的编程方式,可读性也极高。如下,配置端口号 和 应用名称。
server:
port: 8090
spring:
application:
name: springboot-ssm
### 4 改造完成,启动入口类查看效果。
![自动成功](https://img-blog.csdnimg.cn/20191218175248529.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTI1NDI1Mg==,size_16,color_FFFFFF,t_70)
## 二.SpringBoot 集成SSM
### 1.引入依赖 connector,druid,mybatis
<!-- mysql mybatis -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.18</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
### 2.创建数据库 表
CREATE TABLE user(
id int (11) NOT NULL AUTO_INCREMENT,
username varchar(64) NOT NULL,
tel VARCHAR(16) NOT NULL,
password VARCHAR(250) NOT NULL,
status TINYINT NOT NULL default 1,
created_at timestamp not null default CURRENT_TIMESTAMP ,
updated_at timestamp not null default CURRENT_TIMESTAMP ,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
package xyz.java1024.vo;
import java.io.Serializable;
import java.sql.Timestamp;
public class User implements Serializable {
private int id;
private String username;
private String tel;
private String password;
private int status;
private Timestamp createdAt;
private Timestamp updatedAt;
//get set省略
### 3.配置
server:
port: 8090
mybatis:
mapper-locations:
### 3.mapper接口,在mapper包下创建UserMapper接口
package xyz.java1024.mapper;
import xyz.java1024.vo.User;
import java.util.List;
public interface UserMapper {
int deleteByPrimaryKey(Integer id);
int insert(User record);
User selectByPrimaryKey(Integer id);
User selectByUsername(String username);
int updateByPrimaryKey(User record);
List<User> list();
}
### 4.在resources文件夹下创建mapping目录用来存放mapper.xml文件,创建UserMapper.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="xyz.java1024.mapper.UserMapper">
<resultMap id="BaseResultMap" type="xyz.java1024.vo.User"><!--@mbg.generated-->
br/><!--@mbg.generated--><br/id, username, tel, password
, status
, created_at, updated_at
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"><!--@mbg.generated-->
br/><!--@mbg.generated--><br/delete from user
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="xyz.java1024.vo.User" useGeneratedKeys="true"><!--@mbg.generated-->
br/><!--@mbg.generated--><br/update user
set username = #{username,jdbcType=VARCHAR},
tel = #{tel,jdbcType=VARCHAR},password
= #{password,jdbcType=VARCHAR},status
= #{status,jdbcType=TINYINT},
created_at = #{createdAt,jdbcType=TIMESTAMP},
updated_at = #{updatedAt,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="list" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from user
</select>
### 5.扫描mapper接口,即在启动类加@MapperScan注解
@SpringBootApplication@MapperScan("xyz.java1024.mapper")
br/>@RequestMapping("/user")<br/public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/listByUser")
public List<User> listByUser() {
return userMapper.list();
}
@RequestMapping("/getOneUser")
public User getOneUser(int id) {
return userMapper.selectByPrimaryKey(id);
}
@RequestMapping("/addUser")
public int addUser(User user) {
return userMapper.insert(user);
}
@RequestMapping("/deleteUser")
public int deleteUser(int id) {
return userMapper.deleteByPrimaryKey(id);
}
}
### 7.启动工程测试,浏览器输入localhost:8090/user/getOneUser?id=1
![测试结果](https://img-blog.csdnimg.cn/20191218180946266.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTI1NDI1Mg==,size_16,color_FFFFFF,t_70)
## 四.总结
> 体验完SpringBoot 的SSM后,比Spring下的SSM的确是方便的太多,不仅省去了复杂的各种依赖,在配置方面也极大的得到简化,这也是SpringBoot精妙之处。
> 注意:@MapperScan("")是扫描mapper接口,而
mybatis:
mapper-locations:
- classpath:mapping/*.xml
是配置mapper.xml文件
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。