我们不会去使用自增长的id,在现阶段的互联网开发过程中,自增长的id是已经不适用了。在未来随着系统版本的迭代,用户数量的递增,肯定会做分库分表,去做一些相应的切分。在这个时候我们就需要有一个唯一的id。项目里面会有一个唯一id的插件去使用。所有的主键id都会使用一个字符串varchar。小头像是预览过程中会使用到的。聊天页面使用的是小头像。qrcode是扫码的。cid是client id,它是针对我们每一台手机设备,每一台手机设备都会有一个唯一的cid,这个cid可以通过plus(设备的H5+)相应的API去获取,获取之后cid会用于做消息的推送。
users是用户表,还有一个用户请求表friends_request。
my_friends表。用户和朋友之间会有一个关联关系。通过好友的时候,my_friends表是插入两条记录,因为我的朋友和我自己是可以有一个逆向的。
聊天记录表chat_msg。任何的聊天记录在服务端都会保存的。sign_flag是签收的状态。我发送消息给我的朋友,我的朋友收到消息之后他没有去读,这个时候他的状态其实是未读。如果说他的用户接收之后,他可以自动地再发送一条签收的状态到我们的服务端,就可以证明我们当前这条消息是签收了。这个我们在netty里面我们会去做,我们会做签收的状态的更改的相应的代码实现。
一个真正的聊天软件,一个IM软件的话,其实它肯定是不止这四张表的。因为使用的是mybatis,把所有的数据库表逆向生成实体类pojo。pom.xml里面其实是包含了mybatis以及是数据源一些相应的依赖,另外mybatis逆向生成的工具的依赖。
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-log4j -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
https://mvnrepository.com/artifact/com.alibaba/druid/1.1.10
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter/1.1.10
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.14
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.14</version>
</dependency>
https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter/1.3.1
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
https://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter/1.2.4
<!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter/1.2.3
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core/1.3.2
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
pom.xml
<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>
<groupId>com.imooc</groupId>
<artifactId>imooc-muxin-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 引入log4j日志依赖 -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-log4j -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<!-- 阿里开源数据源 -->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.14</version>
</dependency>
<!-- mybatis -->
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!-- mapper -->
<!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
<!-- pagehelper -->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<!-- mybatis 逆向生成工具 -->
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</project>
generatorConfig.xml是用于生成相应的配置。http://www.mybatis.org/generator/configreference/xmlconfig.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="COM.ibm.db2.jdbc.app.DB2Driver"
connectionURL="jdbc:db2:TEST"
userId="db2admin"
password="db2admin">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="test.model" targetProject="\MBGTestProject\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="test.xml" targetProject="\MBGTestProject\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="\MBGTestProject\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table>
</context>
</generatorConfiguration>
/imooc-muxin-mybatis/src/main/java/com/imooc/utils/MyMapper.java
package com.imooc.utils;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
/**
* Copyright © 2018Nathan.Lee.Salvatore. All rights reserved.
*
* @Title: MyMapper.java
* @Prject: imooc-muxin-mybatis
* @Package:
* @Description: TODO
* @author: ZHONGZHENHUA
* @date: 2018年11月21日 下午6:47:29
* @version: V1.0
*/
/**
* 继承自己的MyMapper
*
* @author ZHONGZHENHUA
* @since 2018-11-21 18:48
*/
public interface MyMapper<T> extends Mapper<T>,MySqlMapper<T> {
//TODO
//FIXME 特别注意,该接口不能被扫描到,否则会出错
}
/imooc-muxin-mybatis/generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="MySqlTables" targetRuntime="MyBatis3">
<!-- 处理sql中的`符号 -->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!-- 添加自定义的继承接口 -->
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.imooc.utils.MyMapper"/>
</plugin>
<!-- 数据源配置 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/muxin-dev"
userId="root"
password="root">
</jdbcConnection>
<!-- 对于生成的pojo所在包 -->
<javaModelGenerator targetPackage="com.imooc.pojo" targetProject="src/main/java">
</javaModelGenerator>
<!-- 对于生成的mapper所在目录 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
</sqlMapGenerator>
<!-- 配置mapper对应的java映射 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.imooc.mapper" targetProject="src/main/java">
</javaClientGenerator>
<table tableName="chat_msg"></table>
<table tableName="friends_request"></table>
<table tableName="my_friends"></table>
<table tableName="users"></table>
</context>
/imooc-muxin-mybatis/src/main/java/com/imooc/mybatis/utils/GeneratorDisplay.java
package com.imooc.mybatis.utils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
public class GeneratorDisplay {
public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定 逆向工程配置文件
File configFile = new File("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
public static void main(String[] args) {
try {
GeneratorDisplay generatorSqlmap = new GeneratorDisplay();
generatorSqlmap.generator();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
修改/imooc-muxin-mybatis/generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="MySqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<!-- 处理sql中的`符号 -->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!-- 添加自定义的继承接口 -->
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.imooc.utils.MyMapper"/>
</plugin>
<!-- 数据源配置 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/muxin-dev"
userId="root"
password="root">
</jdbcConnection>
<!-- 对于生成的pojo所在包 -->
<javaModelGenerator targetPackage="com.imooc.pojo" targetProject="src/main/java">
</javaModelGenerator>
<!-- 对于生成的mapper所在目录 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
</sqlMapGenerator>
<!-- 配置mapper对应的java映射 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.imooc.mapper" targetProject="src/main/java">
</javaClientGenerator>
<table tableName="chat_msg"></table>
<table tableName="friends_request"></table>
<table tableName="my_friends"></table>
<table tableName="users"></table>
</context>
</generatorConfiguration>
做开发的时候,数据库表可能一直会去做相应的更改。这一节就是对mybatis做一个逆向生成。接下来需要搭建架构,把工程进行初始化,在里面做相应的开发。框架会使用SpringBoot2.0。https://spring.io/projects/spring-boot#learn,目前SpringBoot最稳定的版本是2.1.0。
接下来创建项目工程。Group Id是组织名,Artifact Id 是项目名称,一般会加上企业的名字-产品名-基本的技术。
parent参考https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/,
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
propertis是指java的版本,另外Encoding就是字符集的设置,UTF-8,JDK是1.8,参考https://maven.apache.org/pom.html
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
dependencies参考https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/,
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
这三个是SpringBoot基本的依赖。下面三个是apache的工具类。
<!-- apache工具类 -->
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
pom工程有了,接下来就需要有启动类/imooc-muxin-netty/src/main/java/com/imooc/Application.java
package com.imooc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
// 扫描 所有需要的包, 包含一些自用的工具类包 所在的路径
@ComponentScan(basePackages= {"com.imooc"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
还需要一个application.properties/application.yml。
#关闭缓存, 即时刷新
#spring.freemarker.cache=false
spring.thymeleaf.cache=true
#热部署生效
spring.devtools.restart.enabled=true
#设置重启的目录,添加那个目录的文件需要restart
spring.devtools.restart.additional-paths=src/main/java
# 为mybatis设置,生产环境可删除
restart.include.mapper=/mapper-[\\w-\\.]+jar
restart.include.pagehelper=/pagehelper-[\\w-\\.]+jar
#排除那个目录的文件不需要restart
#spring.devtools.restart.exclude=static/**,public/**
#classpath目录下的WEB-INF文件夹内容修改不重启
###########################################
#
# 配置日志相关
#
###########################################
# 系统默认info级别, 切换为debug则设置为true
#debug=false
# 设置某个包下的日志级别, 如下则打印所有 com.leecx.controller 这个包下的debug的日志
#logging.level.com.leecx.controllere=DEBUG
#logging.level.com.leecx.service=WARN
##########################################
#
# 配置数据源相关 使用阿里巴巴的druid 数据源
#
##########################################
spring.datasource.url=jdbc:mysql://localhost:3306/leecx
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Drivers
spring.datasource.druid.initial-size=1
spring.datasource.druid.min-idle=1
spring.datasource.druid.max-active=20
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.stat-view-servlet.allow=true
############################################
#
# mybatis 配置
#
############################################
# mybatis 配置
mybatis.type-aliases-package=com.leecx.pojo
mybatis.mapper-locations=classpath:mapper/*.xml
# 通用Mapper 配置
mapper.mappers=com.imooc.utils.MyMapper
mapper.not-empty=false
mapper.identity=MYSQL
# 分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
##########################################
#
# REDIS 配置
#
##########################################
# Redis数据库索引(默认为0)
spring.redis.database=1
# Redis服务器地址
spring.redis.host=192.168.1.191
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=1000
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=10
# 连接池中的最小空闲连接
spring.redis.timeout=0
##########################################
#
# 配置i18n 资源文件, 供thymeleaf 读取
#
##########################################
spring.messages.basename=i18n/messages
#spring.messages.cache-seconds=3600
spring.messages.encoding=UTF-8
#设定静态文件路径,js,css等
spring.mvc.static-path-pattern=/static/**
#########################################
#
# freemarker 静态资源配置
#
#########################################
#设定ftl文件路径
spring.freemarker.template-loader-path=classpath:/templates
# 关闭缓存, 即时刷新, 上线生产环境需要改为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl
###################################################
#
# thymeleaf 静态资源配置
#
#
###################################################
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
# 关闭缓存, 即时刷新, 上线生产环境需要改为true
#spring.thymeleaf.cache=false
########################################
#
# Server 服务端相关配置
#
########################################
# 配置api端口号
#server.port=8088
server.port=8080
# 配置context-path, 一般来说这个配置在正式发布的时候不配置
#server.context-path=/LeeCX
# 错误页,指定发生错误时,跳转的URL --> BasicErrorController
#server.error.path=/error
# session最大超时时间(分钟),默认为30分钟
server.servlet.session.timeout=60
# 该服务绑定IP地址,启动服务器时如本机不是该IP地址则抛出异常启动失败, 只有特殊需求的情况下才配置,
#server.address=192.168.1.8
###########################################
# Server - tomcat 相关常用配置
###########################################
# tomcat最大线程数, 默认为200
#server.tomcat.max-threads=250
# tomcat的URI编码
server.tomcat.uri-encoding=UTF-8
# 存放Tomcat的日志、Dump等文件的临时文件夹,默认为系统的tmp文件夹(如:C:\Users\ZHONGZHENHUA\AppData\
#server.tomcat.basedir=H:/springboot-tomcat-tmp
# 打开Tomcat的Access日志,并可以设置日志格式的方法:
#server.tomcat.access-log-enabled=true
#server.tomcat.access-log-pattern=
# accesslog目录,默认在basedir/logs
#server.tomcat.accesslog.directory=
# 日志文件目录
#logging.path=H:/springboot-tomcat-tmp
# 日志文件名称,默认为spring.log
#logging.file=myapp.log
删去没用的内容,application.properties为:
########################################
#
# Server 服务端相关配置
#
########################################
# 配置api端口号
#server.port=8088
server.port=8080
###########################################
# Server - tomcat 相关常用配置
###########################################
# tomcat的URI编码
server.tomcat.uri-encoding=UTF-8
现在基本的配置都已经是有了,有了之后我们来创建一个HelloController,来做一个测试看一下我们的SpringBoot有没有搭建成功。由于我们的项目一开始刚刚初始化,Problems会有一个问题。
右键Maven,Update project。