SpringBoot整合SSM项目

Posted 一只特立独行的程序猿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot整合SSM项目相关的知识,希望对你有一定的参考价值。

更多内容参见个人技术博客,无广告欢迎关注

1.1.1      系统架构图

 

 

1.1.2      创建数据库表

 

创建mybatisdb数据库,设置utf8字符集。

创建user表:  


DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT COMMENT \'用户id\',
`user_name` varchar(100) COLLATE utf8mb4_bin NOT NULL DEFAULT \'\' COMMENT \'用户名\',
`birthday` bigint(20) NOT NULL DEFAULT \'0\' COMMENT \'生日\',
`address` varchar(200) COLLATE utf8mb4_bin NOT NULL DEFAULT \'\' COMMENT \'住址\',
`create_time` bigint(20) NOT NULL DEFAULT \'0\' COMMENT \'创建时间\',
`update_time` bigint(20) NOT NULL DEFAULT \'0\' COMMENT \'更新时间\',
`create_by` varchar(64) COLLATE utf8mb4_bin NOT NULL DEFAULT \'\' COMMENT \'创建者ID或名称\',
`update_by` varchar(64) COLLATE utf8mb4_bin NOT NULL DEFAULT \'\' COMMENT \'更新者ID或名称\',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=56 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT=\'商品品牌\';

insert into `user`(`user_name`,`birthday`,`address`,`create_time`,`create_by`) values
(\'张三\',1546393586074,\'桂州村\', 1546393586074, \'admin\'),
(\'李四\',1546393586074,\'王府井\', 1546393586074, \'admin\');

 

1.1.3     创建SpringBoot工程,创建simple project,类型为jar

 

 

 

 

1.1.4      pom.xml

额外需要的jar,还得自己依赖,例如:mysql驱动包,阿里的数据源druid

 pasting

<?xml version="1.0" encoding="UTF-8"?>
<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.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Spring Boot整合SSM</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mybatis-plus.version>2.1.9</mybatis-plus.version>
<druid.version>1.1.5</druid.version>
</properties>

<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--数据库连接为mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--durid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

1.1.5      User.java

package com.example.demo.entity;

import lombok.Data;

import java.io.Serializable;

@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;

private Integer id;
private String name;
// 不用Date类型做日期,因为实际开发中数据库表不会用Date类型
// @DateTimeFormat(pattern="yyyy-MM-dd")
// private Date birthday;
private String birthday;
private String address;
}

 

1.1.6      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="com.staritgp.platform.demo.mapper.SysDeptMapper">

<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.example.demo.entity.User">
<id column="user_id" property="userId" />
<result column="user_name" property="userName" />
<result column="birthday" property="birthday" />
<result column="address" property="address" />
<result column="create_time" property="createTime" />
<result column="update_time" property="updateTime" />
<result column="create_by" property="createBy" />
<result column="update_by" property="updateBy" />
</resultMap>

<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
user_id AS userId, user_name AS userName, birthday AS birthday, address AS address, create_time AS createTime, update_time AS updateTime, create_by AS createBy, update_by AS updateBy
</sql>


<select id="findAll" resultType="com.example.demo.entity.User">
select
<include refid="Base_Column_List"></include>
from sys_dept where 1=1
<if test="userName != null and userName != \'\'">
<!-- 名称模糊查询 -->
AND user_name like CONCAT(\'%\',#{userName},\'%\')
</if>
<if test="userId != null and userId != \'\'">
AND user_id = #{userId}
</if>
<if test="createTime != null">
AND create_time = #{createTime}
</if>
<if test="updateTime != null">
AND update_time = #{updateTime}
</if>
<if test="createBy != null and createBy != \'\'">
AND create_by = #{createBy}
</if>
<if test="updateBy != null and updateBy != \'\'">
AND update_by = #{updateBy}
</if>
</select>

</mapper>
 

 

1.1.7      UserMapper.java 接口

package com.example.demo.mapper;

import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserMapper {

/**
* 自定义分页sql,调用xml方式
* @param user
* @return
*/
List<User> findAll(User user);

// 调用注解方式
@Select("select * from user where user_id=#{id}")
public User findById(Integer id);
}

1.1.8      UserService.java

package com.example.demo.service.impl;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserMapper userMapper;

@Override public User findById(Integer id) {
return userMapper.findById(id);
}

@Override public List<User> findAll(User user) {
return userMapper.findAll(user);
}
}

1.1.9      UserServiceImpl.java

package com.example.demo.service.impl;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserMapper userMapper;

@Override public User findById(Integer id) {
return userMapper.findById(id);
}

@Override public List<User> findAll(User user) {
return userMapper.findAll(user);
}

 

1.1.10    UserController.java

使用@RestController替代@Controller@ResponseBody(返回json串)

package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;

@RequestMapping("/find")
public List<User> findAll(User user) {
return userService.findAll(user);
}

@RequestMapping("/get/{id}")
public User findById(@PathVariable Integer id){
return userService.findById(id);
}


}

 

1.1.11    application.yml

全局配置文件,yml为新的配置文件方式,注意其中格式为空格,不能有tab

配置端口,配置数据源,配置mybatis全局配置。

注意:如果端口,启动时日志显示8080,说明此文件未加载。检查原因一般是文件名或者路径不正确。

# 服务端口
server:
port: 8081
# 数据连接池配置,druid 比 c3p0 还牛逼
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mybatisdb
username: root
password: root
# com.example.demo 的子目录中的内容都会打印日志
logging:
level:
com.example.demo: debug

 

Springboot整合ssm

SpringBoot整合SSM项目

SpringBoot完成SSM整合之SpringBoot整合junit

SpringBoot整合SSM三大框架源码剖析之SpringBoot源码剖析

SpringBoot整合SSM(代码实现Demo)

SpringBoot整合ssm