SpringBoot基于Spring4.0设计,该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。那springboot怎么改成ssm?下面来我们就来给大家讲解一下springboot改成ssm方法。
一.先介绍创建模块的另一种方式
1.点击MAVEN -> 勾选CREATE FROM ARCHETYPE -> 选择 MAVEN-ARCHETYPE-QUICKSTART
有时会需要点击 自动导入
2.工程目录
可以看到,这样创建的模块是相对干净的,需要我们手动的编写程序启动入口类、需要配置时还得创建配置文件。下一步见证。
3.完善模块
添加依赖,这种脚架添加模块不会自动引入相关依赖。这里主要引入父级版本号和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配置文件。
细心的同学可以发现 resources 图标没有 黄色的横线
点击上图,进入如图,选择Modules -> 选中模块, 接着选中resources再点击Mark as 中 带黄色横线的Resources即可,点击ok
同时可见 yml后缀的配置文件的图标也发生了改变
下面我们开始配置文件,yml 提供更加简洁的编程方式,可读性也极高。如下,配置端口号 和 应用名称。
server: port: 8090 spring: application: name: springboot - ssm
4 改造完成,启动入口类查看效果。
二.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.创建数据库 表
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# 扫描mapper.xml文件 mybatis: mapper - locations: -classpath: mapping /*.xml spring: application: name: springboot-ssm datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/springboot?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123zxc
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--> <id column="id" jdbcType="INTEGER" property="id" /> <result column="username" jdbcType="VARCHAR" property="username" /> <result column="tel" jdbcType="VARCHAR" property="tel" /> <result column="password" jdbcType="VARCHAR" property="password" /> <result column="status" jdbcType="TINYINT" property="status" /> <result column="created_at" jdbcType="TIMESTAMP" property="createdAt" /> <result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt" /> </resultMap> <sql id="Base_Column_List"> <!--@mbg.generated--> id, username, tel, `password`, `status`, created_at, updated_at </sql> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> <!--@mbg.generated--> select <include refid="Base_Column_List" /> from user where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> <!--@mbg.generated--> 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--> insert into user (username, tel, `password`, `status`, created_at, updated_at ) values (#{username,jdbcType=VARCHAR}, #{tel,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{status,jdbcType=TINYINT}, #{createdAt,jdbcType=TIMESTAMP}, #{updatedAt,jdbcType=TIMESTAMP} ) </insert> <update id="updateByPrimaryKey" parameterType="xyz.java1024.vo.User"> <!--@mbg.generated--> 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") public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
6.编写USERCONTROLLER,省略SERVICE
@RestController @RequestMapping("/user") 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
这样我们就完成了SpringBoot 改成SSM的方法,SpringBoot 极大的简化了各种复杂依赖,简化开发,这就是SpringBoot精妙之处,最后大家如果想要了解更多java架构师知识,敬请关注奇Q工具网。
推荐阅读: