SpringBoot集成SSM校赤,實現(xiàn)增刪改查功能
1.點擊Maven -> 勾選Create from archetype -> 選擇 maven-archetype-quickstart
有時會需要點擊 自動導(dǎo)入
可以看到躁锡,這樣創(chuàng)建的模塊是相對干凈的午绳,需要我們手動的編寫程序啟動入口類、需要配置時還得創(chuàng)建配置文件映之。下一步見證拦焚。
添加依賴蜡坊,這種腳架添加模塊不會自動引入相關(guān)依賴。這里主要引入父級版本號和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文件夾螟够,在創(chuàng)建application.yml配置文件灾梦。
細心的同學(xué)可以發(fā)現(xiàn) resources 圖標沒有 黃色的橫線
點擊上圖,進入如圖妓笙,選擇Modules -> 選中模塊若河, 接著選中resources再點擊Mark as 中 帶黃色橫線的Resources即可,點擊ok
同時可見 yml后綴的配置文件的圖標也發(fā)生了改變
下面我們開始配置文件寞宫,yml 提供更加簡潔的編程方式萧福,可讀性也極高。如下辈赋,配置端口號 和 應(yīng)用名稱鲫忍。
server:
? port: 8090
spring:
? application:
? ? name: 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>
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省略
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包下創(chuàng)建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文件夾下創(chuàng)建mapping目錄用來存放mapper.xml文件,創(chuàng)建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后,比Spring下的SSM的確是方便的太多竭业,不僅省去了復(fù)雜的各種依賴智润,在配置方面也極大的得到簡化,這也是SpringBoot精妙之處未辆。
注意:@MapperScan("")是掃描mapper接口窟绷,而
mybatis:
mapper-locations:
- classpath:mapping/*.xml
是配置mapper.xml文件