码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot整合SSM项目

时间:2019-01-22 10:54:15      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:bat   username   日期   can   原因   1.4   png   dep   add   

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

 

1.1.12    RunApplication.java 

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
// 扫描该包下的所有内容,什么mapperscan都不用管,约定大于配置
@ComponentScan(basePackages = {"com.example.demo"})
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}

1.2         拓展:约定大于配置

1.2.1      在哪里约定的?

各组件都有注解形式的自动配置类,可以点进去看看,Spring帮我们定义了一个约定,遵守这个约定可以少好多事情,而且还不容易出错。

spring-boot-autoconfigure-1.5.4.RELEASE.jar

mybatis-spring-boot-autoconfigure-1.3.0.jar

pagehelper-spring-boot-autoconfigure-1.2.2.jar

1.3         实现CRUD操作

1.3.1      注解方式接口

剩下的CRUD方法,你们可以自行完善一下当做练手。 

1.3.2      UserController

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);
}

@RequestMapping("/update")
public String update(User user) {
return userService.update(user);
}

// delete不做物理删除,只是标记为删除
@RequestMapping("/delete")
public Boolean finddeleteAll(User user) {
return userService.delete(user);
}

@RequestMapping("/add")
public User add(User user) {
return userService.add(user);
}

 

SpringBoot整合SSM项目

标签:bat   username   日期   can   原因   1.4   png   dep   add   

原文地址:https://www.cnblogs.com/wood-life/p/10302041.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!