diff --git a/GitManual.md b/GitManual.md new file mode 100644 index 0000000000000000000000000000000000000000..34d3dc07b2139addddf31c324a3acb564bd80348 --- /dev/null +++ b/GitManual.md @@ -0,0 +1,135 @@ +# Git手册 + +## 远程仓库基本使用 + +```shell +#添加远程仓库 +git remote +git remote -v +git remote add pb git://github.com/user/project.git +git remote show origin +#重命名 +git remote rename pb pbmnew + +#从现有仓库克隆 +git clone git://github.com/user/project.git +git clone git://github.com/user/project.git projectnewname + +#从远程仓库抓取数据 +git fetch [remote-name] + +#拉取代码 +git pull origin master + +#拉取代码,解决仓库不一致问题 fatal: refusing to merge unrelated histories +git pull origin master --allow-unrelated-histories + +#提交代码 +git push <远程主机名> <本地分支名>:<远程分支名> +git push origin master:develop +``` + +## 设置信息 + +```shell +#设置信息 +git config --global user.name "userName" +git config --global user.email "email" +``` + +## 获取帮助 + +```shell +#获取帮助 +git help +git --help +man git- +git help config +``` + +## 基本使用 + +```shell +#初始化新仓库 +git init + +#添加文件 进行跟踪 +git add *.c +git add README.md + +#移除文件 +git rm README.md + +#移动文件 +git mv file_from file_to + +#检查文件状态 +git status + +#检查差异 +git diff + +#设置提交信息 +git commit -m 'init project' +``` + +## 提交历史 + +```shell +#查看提交历史 +git log +git log -p -2 +git log --stat +``` + +## 标签 + +```shell +#打标签 +##列出标签 +git tag +git tag -l 'v1.0.0.*' +#含标注的标签 +git tag -a v1.4 -m 'version 1.4' +#查看标签版本信息 +git show v1.4 + +#推送标签 +git push origin [tagname] +git push origin v1.5 +#一次性推送多个标签 +git push origin --tags +``` + +## 分支 + +```shell +#列出分支 +git branch +#创建分支 +git branch dev +#选择分支 +git checkout dev +#新建并选择分支 +git checkout -b iss53 +#合并子分支代码到主分支 +git checkout master +git merge iss53 +#删除分支 +git branch -d iss53 +#推送本地分支 +git push (远程仓库名) (分支名) +git push origin serverfix +#根据远程分支创建对应的本地分支 +git checkout -b serverfix origin/serverfix +#跟踪远程分支 +git checkout -b [分支名] [远程名]/[分支名] +git checkout --track origin/serverfix +#删除远程分支 +git push origin :serverfix +#衍合 把在一个分支里提交的改变移到另一个分支里重放一遍(一旦分支中的提交对象发布到公共仓库,就千万不要对该分支进行衍合操作。) +git checkout experiment +git rebase master +``` + +## \ No newline at end of file diff --git a/README.en.md b/README.en.md index 346ae086a14717d86f09e1aea664db9e3db141b9..f45fbe94ae5e43d4c9b24ece5f8097af7f4c33e4 100644 --- a/README.en.md +++ b/README.en.md @@ -1,24 +1,24 @@ # java-demo -#### Description +## Description spring-boot框架的java项目 -#### Software Architecture +## Software Architecture Software architecture description -#### Installation +## Installation 1. xxxx 2. xxxx 3. xxxx -#### Instructions +## Instructions 1. xxxx 2. xxxx 3. xxxx -#### Contribution +## Contribution 1. Fork the repository 2. Create Feat_xxx branch @@ -26,7 +26,7 @@ Software architecture description 4. Create Pull Request -#### Gitee Feature +## Gitee Feature 1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md 2. Gitee blog [blog.gitee.com](https://blog.gitee.com) diff --git a/README.md b/README.md index 6222dea609bef74104eb26398a402f0c451a3355..6c0e3850b7436511c4a9d80cf8db54b2e4e162f1 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,25 @@ # java-demo -#### 介绍 +## 介绍 spring-boot框架的java项目 -#### 软件架构 +## 软件架构 软件架构说明 -#### 安装教程 +## 安装教程 1. xxxx 2. xxxx 3. xxxx -#### 使用说明 +## 使用说明 1. xxxx 2. xxxx 3. xxxx -#### 参与贡献 +## 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 @@ -27,7 +27,7 @@ spring-boot框架的java项目 4. 新建 Pull Request -#### 特技 +## 特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) diff --git a/java-dal/pom.xml b/java-dal/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..1dd7e0c8db9440ce4692f111c95ea28c9c1ceec0 --- /dev/null +++ b/java-dal/pom.xml @@ -0,0 +1,23 @@ + + + + java-demo + com.study + 1.0-SNAPSHOT + + 4.0.0 + + java-dal + ${project.parent.version} + + + + org.projectlombok + lombok + compile + + + + \ No newline at end of file diff --git a/java-dal/src/main/java/com/study/java/dal/dto/ResponseCode.java b/java-dal/src/main/java/com/study/java/dal/dto/ResponseCode.java new file mode 100644 index 0000000000000000000000000000000000000000..9b6633ec8ae1eabead8a93bb9871d45d0aea6bb6 --- /dev/null +++ b/java-dal/src/main/java/com/study/java/dal/dto/ResponseCode.java @@ -0,0 +1,12 @@ +package com.study.java.dal.dto; + +/** + * @author: wangmy + * @date: 2023/05/13 + * @email: wangmywork@163.com + * @description: 响应码 + */ +public class ResponseCode { + public static final int SUCCESS_CODE = 200; + public static final int FAILED_CODE = 400; +} diff --git a/java-dal/src/main/java/com/study/java/dal/dto/ResponseDto.java b/java-dal/src/main/java/com/study/java/dal/dto/ResponseDto.java new file mode 100644 index 0000000000000000000000000000000000000000..d5f1c66ec9ab1ea65c1c1eb21cd78c07caefcf05 --- /dev/null +++ b/java-dal/src/main/java/com/study/java/dal/dto/ResponseDto.java @@ -0,0 +1,45 @@ +package com.study.java.dal.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + * @author: wangmy + * @date: 2023/05/13 + * @email: wangmywork@163.com + * @description: 响应信息数据传输对象 + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class ResponseDto implements Serializable { + public static long serialVersionUID = 1L; + private static final String SUCCESS = "success"; + + private Integer code; + private String msg; + private T data; + + public static ResponseDto success(T data) { + return new ResponseDto<>(ResponseCode.SUCCESS_CODE, SUCCESS, data); + } + + public static ResponseDto failed(String msg) { + return new ResponseDto<>(ResponseCode.FAILED_CODE, msg, null); + } + + public static ResponseDto failed(String msg, String... args) { + return new ResponseDto<>(ResponseCode.FAILED_CODE, String.format(msg, args), null); + } + + public static ResponseDto failed(int code, String msg) { + return new ResponseDto<>(code, msg, null); + } + + public static ResponseDto failed(int code, String msg, T data) { + return new ResponseDto<>(code, msg, data); + } +} diff --git a/java-dal/src/main/java/com/study/java/dal/entity/User.java b/java-dal/src/main/java/com/study/java/dal/entity/User.java new file mode 100644 index 0000000000000000000000000000000000000000..09ee862e7f32b5719b0ead2933deee1b62e4ceab --- /dev/null +++ b/java-dal/src/main/java/com/study/java/dal/entity/User.java @@ -0,0 +1,22 @@ +package com.study.java.dal.entity; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @author: wangmy + * @date: 2023/05/13 + * @email: wangmywork@163.com + * @description: 用户实体类 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class User { + private Long id; + private String userName; + private String password; +} diff --git a/java-dao/pom.xml b/java-dao/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..ec06496531aa8eaf31a451588e8e9848e636b34a --- /dev/null +++ b/java-dao/pom.xml @@ -0,0 +1,34 @@ + + + + java-demo + com.study + 1.0-SNAPSHOT + + 4.0.0 + + java-dao + ${project.parent.version} + + + + com.study + java-dal + ${project.parent.version} + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + compile + + + mysql + mysql-connector-java + compile + + + + \ No newline at end of file diff --git a/java-dao/src/main/java/com/study/java/dao/config/PrimaryDataSourceConfig.java b/java-dao/src/main/java/com/study/java/dao/config/PrimaryDataSourceConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..3f3bb5447d1d76f7272aafc6553a894863528061 --- /dev/null +++ b/java-dao/src/main/java/com/study/java/dao/config/PrimaryDataSourceConfig.java @@ -0,0 +1,98 @@ +package com.study.java.dao.config; + +import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionFactoryBean; +import org.mybatis.spring.SqlSessionTemplate; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.jdbc.DataSourceBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; + +import javax.sql.DataSource; + + +/** + * @author: wangmy + * @date: 2023/05/13 + * @email: wangmywork@163.com + * @description: 首要数据源配置 + */ +@Configuration +@MapperScan(basePackages = PrimaryDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "PrimarySqlSessionFactory") +public class PrimaryDataSourceConfig { + // 精确到 master 目录,以便跟其他数据源隔离 + static final String PACKAGE = "com.study.java.dao.primary"; + static final String MAPPER_LOCATION = "classpath*:mapper/primary/*.xml"; + + + @Value("${spring.datasource.primary.url}") + private String url; + + @Value("${spring.datasource.primary.username}") + private String user; + + @Value("${spring.datasource.primary.password}") + private String password; + + @Value("${spring.datasource.primary.driverClassName}") + private String driverClass; + + /** + * 首要数据源 + * + * @return + * @Primary 表示这个数据源是默认数据源, 这个一定要加,如果两个数据源都没有@Primary会报错 + * @ConfigurationProperties 配置文件中的前缀 + */ + @Bean(name = "PrimaryDataSource") + @Primary + // @ConfigurationProperties(prefix = "spring.datasource.primary") + public DataSource getPrimaryDateSource() { + return DataSourceBuilder.create() + .url(url) + .driverClassName(driverClass) + .username(user) + .password(password) + .build(); + } + + + /** + * 首要sql会话工厂 + * + * @param datasource + * @return + * @throws Exception + */ + @Bean(name = "PrimarySqlSessionFactory") + @Primary + public SqlSessionFactory primarySqlSessionFactory(@Qualifier("PrimaryDataSource") DataSource datasource) + throws Exception { + SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); + bean.setDataSource(datasource); + // 设置mybatis的xml所在位置 + bean.setMapperLocations( + new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION)); + return bean.getObject(); + } + + + /** + * 首要sql会话模板 + * + * @param sqlSessionFactory + * @return + * @Primary 表示这个数据源是默认数据源, 这个一定要加,如果两个数据源都没有@Primary会报错 + */ + @Bean("PrimarySqlSessionTemplate") + @Primary + public SqlSessionTemplate primarySqlSessionTemplate( + @Qualifier("PrimarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) { + return new SqlSessionTemplate(sqlSessionFactory); + } + +} diff --git a/java-dao/src/main/java/com/study/java/dao/config/SecondaryDataSourceConfig.java b/java-dao/src/main/java/com/study/java/dao/config/SecondaryDataSourceConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..cf000493c6c82cddcc14cb453b5f3ef09621c1b4 --- /dev/null +++ b/java-dao/src/main/java/com/study/java/dao/config/SecondaryDataSourceConfig.java @@ -0,0 +1,68 @@ +package com.study.java.dao.config; + +import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionFactoryBean; +import org.mybatis.spring.SqlSessionTemplate; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.jdbc.DataSourceBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; + +import javax.sql.DataSource; + +/** + * @author: wangmy + * @date: 2023/05/13 + * @email: wangmywork@163.com + * @description: 次要数据源配置 + */ +@Configuration +@MapperScan(basePackages = SecondaryDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "SecondarySqlSessionFactory") +public class SecondaryDataSourceConfig { + // 精确到 master 目录,以便跟其他数据源隔离 + static final String PACKAGE = "com.study.java.dao.secondary"; + static final String MAPPER_LOCATION = "classpath*:mapper/secondary/*.xml"; + + @Value("${spring.datasource.secondary.url}") + private String url; + + @Value("${spring.datasource.secondary.username}") + private String user; + + @Value("${spring.datasource.secondary.password}") + private String password; + + @Value("${spring.datasource.secondary.driverClassName}") + private String driverClass; + + @Bean(name = "SecondaryDataSource") + // @ConfigurationProperties(prefix = "spring.datasource.secondary") + public DataSource getSecondaryDataSource() { + return DataSourceBuilder.create() + .url(url) + .driverClassName(driverClass) + .username(user) + .password(password) + .build(); + } + + @Bean(name = "SecondarySqlSessionFactory") + public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("SecondaryDataSource") DataSource datasource) + throws Exception { + SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); + bean.setDataSource(datasource); + // 设置mybatis的xml所在位置 + bean.setMapperLocations( + new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION)); + return bean.getObject(); + } + + @Bean("SecondarySqlSessionTemplate") + public SqlSessionTemplate secondarySqlSessionTemplate( + @Qualifier("SecondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) { + return new SqlSessionTemplate(sqlSessionFactory); + } +} \ No newline at end of file diff --git a/java-dao/src/main/java/com/study/java/dao/primary/PrimaryUserDao.java b/java-dao/src/main/java/com/study/java/dao/primary/PrimaryUserDao.java new file mode 100644 index 0000000000000000000000000000000000000000..8fa3a1f1f8dab8ad6171eac682c9de76fa016db6 --- /dev/null +++ b/java-dao/src/main/java/com/study/java/dao/primary/PrimaryUserDao.java @@ -0,0 +1,74 @@ +package com.study.java.dao.primary; + +import com.study.java.dal.entity.User; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +/** + * @author: wangmy + * @date: 2023/05/13 + * @email: wangmywork@163.com + * @description: (数据源1 - User)表数据库访问层 + */ +public interface PrimaryUserDao { + + /** + * 通过ID查询单条数据 + * + * @param id 主键 + * @return 实例对象 + */ + User queryById(Long id); + + /** + * 统计总行数 + * + * @param user 查询条件 + * @return 总行数 + */ + long count(User user); + + /** + * 新增数据 + * + * @param user 实例对象 + * @return 影响行数 + */ + int insert(User user); + + /** + * 批量新增数据(MyBatis原生foreach方法) + * + * @param entities List 实例对象列表 + * @return 影响行数 + */ + int insertBatch(@Param("entities") List entities); + + /** + * 批量新增或按主键更新数据(MyBatis原生foreach方法) + * + * @param entities List 实例对象列表 + * @return 影响行数 + * @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常,请自行校验入参 + */ + int insertOrUpdateBatch(@Param("entities") List entities); + + /** + * 修改数据 + * + * @param user 实例对象 + * @return 影响行数 + */ + int update(User user); + + /** + * 通过主键删除数据 + * + * @param id 主键 + * @return 影响行数 + */ + int deleteById(Long id); + +} + diff --git a/java-dao/src/main/java/com/study/java/dao/secondary/SecondaryUserDao.java b/java-dao/src/main/java/com/study/java/dao/secondary/SecondaryUserDao.java new file mode 100644 index 0000000000000000000000000000000000000000..5f6bbebe8ae34645059c474d58b96d85be9150da --- /dev/null +++ b/java-dao/src/main/java/com/study/java/dao/secondary/SecondaryUserDao.java @@ -0,0 +1,74 @@ +package com.study.java.dao.secondary; + +import com.study.java.dal.entity.User; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +/** + * @author: wangmy + * @date: 2023/05/13 + * @email: wangmywork@163.com + * @description: (数据源2 - User)表数据库访问层 + */ +public interface SecondaryUserDao { + + /** + * 通过ID查询单条数据 + * + * @param id 主键 + * @return 实例对象 + */ + User queryById(Long id); + + /** + * 统计总行数 + * + * @param user 查询条件 + * @return 总行数 + */ + long count(User user); + + /** + * 新增数据 + * + * @param user 实例对象 + * @return 影响行数 + */ + int insert(User user); + + /** + * 批量新增数据(MyBatis原生foreach方法) + * + * @param entities List 实例对象列表 + * @return 影响行数 + */ + int insertBatch(@Param("entities") List entities); + + /** + * 批量新增或按主键更新数据(MyBatis原生foreach方法) + * + * @param entities List 实例对象列表 + * @return 影响行数 + * @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常,请自行校验入参 + */ + int insertOrUpdateBatch(@Param("entities") List entities); + + /** + * 修改数据 + * + * @param user 实例对象 + * @return 影响行数 + */ + int update(User user); + + /** + * 通过主键删除数据 + * + * @param id 主键 + * @return 影响行数 + */ + int deleteById(Long id); + +} + diff --git a/java-dao/src/main/resources/mapper/primary/PrimaryUserMapper.xml b/java-dao/src/main/resources/mapper/primary/PrimaryUserMapper.xml new file mode 100644 index 0000000000000000000000000000000000000000..cd0ffa6c890881ee81a2667a912f5729bea1dc48 --- /dev/null +++ b/java-dao/src/main/resources/mapper/primary/PrimaryUserMapper.xml @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + + + + + + + insert into user(user_name, password) + values (#{userName}, #{password}) + + + + insert into user(user_name, password) + values + + (#{entity.userName}, #{entity.password}) + + + + + insert into user(user_name, password) + values + + (#{entity.userName}, #{entity.password}) + + on duplicate key update + user_name = values(user_name), + password = values(password) + + + + + update user + + + user_name = #{userName}, + + + password = #{password}, + + + where id = #{id} + + + + + delete from user where id = #{id} + + + + diff --git a/java-dao/src/main/resources/mapper/secondary/SecondaryUserMapper.xml b/java-dao/src/main/resources/mapper/secondary/SecondaryUserMapper.xml new file mode 100644 index 0000000000000000000000000000000000000000..8286d710c8a5b0431d6f5c039d81125d14642ef8 --- /dev/null +++ b/java-dao/src/main/resources/mapper/secondary/SecondaryUserMapper.xml @@ -0,0 +1,81 @@ + + + + + + + + + + + + + + + + + + + insert into user(user_name, password) + values (#{userName}, #{password}) + + + + insert into user(user_name, password) + values + + (#{entity.userName}, #{entity.password}) + + + + + insert into user(user_name, password) + values + + (#{entity.userName}, #{entity.password}) + + on duplicate key update + user_name = values(user_name), + password = values(password) + + + + + update user + + + user_name = #{userName}, + + + password = #{password}, + + + where id = #{id} + + + + + delete from user where id = #{id} + + + + diff --git a/java-service/pom.xml b/java-service/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..de89af3579daf680956c8194be561ef7ee47892e --- /dev/null +++ b/java-service/pom.xml @@ -0,0 +1,28 @@ + + + + java-demo + com.study + 1.0-SNAPSHOT + + 4.0.0 + + java-service + ${project.parent.version} + + + + com.study + java-util + ${project.parent.version} + + + com.study + java-dao + ${project.parent.version} + + + + \ No newline at end of file diff --git a/java-service/src/main/java/com/study/java/service/UserService.java b/java-service/src/main/java/com/study/java/service/UserService.java new file mode 100644 index 0000000000000000000000000000000000000000..a98ab64768d88fe879c39f6c1be121f6986c4079 --- /dev/null +++ b/java-service/src/main/java/com/study/java/service/UserService.java @@ -0,0 +1,40 @@ +package com.study.java.service; + +import com.study.java.dal.entity.User; + +/** + * @author: wangmy + * @date: 2023/05/13/1:16 + * @email: wangmywork@163.com + * @description: 用户服务 + */ +public interface UserService { + /** + * 新增用户 + * @param user + * @return + */ + int addUser(User user); + + /** + * 更新用户 + * @param user + * @return + */ + int updateUser(User user); + + /** + * 删除用户 + * @param id + * @return + */ + Integer removeUser(Long id); + + /** + * 获取用户 + * @param id + * @return + */ + User getUser(Long id); + +} diff --git a/java-service/src/main/java/com/study/java/service/impl/PrimaryUserService.java b/java-service/src/main/java/com/study/java/service/impl/PrimaryUserService.java new file mode 100644 index 0000000000000000000000000000000000000000..79c6f215ef5d2b9eb39a30e4625c60b3f13a6bc5 --- /dev/null +++ b/java-service/src/main/java/com/study/java/service/impl/PrimaryUserService.java @@ -0,0 +1,63 @@ +package com.study.java.service.impl; + +import com.study.java.dal.entity.User; +import com.study.java.dao.primary.PrimaryUserDao; +import com.study.java.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * @author: wangmy + * @date: 2023/05/13 + * @email: wangmywork@163.com + * @description: 首要用户服务 + */ +@Service +public class PrimaryUserService implements UserService { + @Autowired + private PrimaryUserDao primaryUserDao; + + /** + * 新增用户 + * + * @param user + * @return + */ + @Override + public int addUser(User user) { + return primaryUserDao.insert(user); + } + + /** + * 更新用户 + * + * @param user + * @return + */ + @Override + public int updateUser(User user) { + return primaryUserDao.update(user); + } + + /** + * 删除用户 + * + * @param id + * @return + */ + @Override + public Integer removeUser(Long id) { + return primaryUserDao.deleteById(id); + } + + /** + * 获取用户 + * + * @param id + * @return + */ + @Override + public User getUser(Long id) { + return primaryUserDao.queryById(id); + } +} diff --git a/java-service/src/main/java/com/study/java/service/impl/SecondaryUserService.java b/java-service/src/main/java/com/study/java/service/impl/SecondaryUserService.java new file mode 100644 index 0000000000000000000000000000000000000000..24c5ebd1a2a4ef7f274f7a603bea1df0a8f57405 --- /dev/null +++ b/java-service/src/main/java/com/study/java/service/impl/SecondaryUserService.java @@ -0,0 +1,64 @@ +package com.study.java.service.impl; + +import com.study.java.dal.entity.User; +import com.study.java.dao.secondary.SecondaryUserDao; +import com.study.java.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * @author: wangmy + * @date: 2023/05/13 + * @email: wangmywork@163.com + * @description: 次要用户服务 + */ +@Service +public class SecondaryUserService implements UserService { + @Autowired + private SecondaryUserDao secondaryUserDao; + + /** + * 新增用户 + * + * @param user + * @return + */ + @Override + public int addUser(User user) { + return secondaryUserDao.insert(user); + } + + /** + * 更新用户 + * + * @param user + * @return + */ + @Override + public int updateUser(User user) { + return secondaryUserDao.update(user); + } + + /** + * 删除用户 + * + * @param id + * @return + */ + @Override + public Integer removeUser(Long id) { + return secondaryUserDao.deleteById(id); + } + + /** + * 获取用户 + * + * @param id + * @return + */ + @Override + public User getUser(Long id) { + return secondaryUserDao.queryById(id); + } + +} diff --git a/java-util/pom.xml b/java-util/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..c3576a06f6034e36d52a4fad1cbd72f99fbf1554 --- /dev/null +++ b/java-util/pom.xml @@ -0,0 +1,23 @@ + + + + java-demo + com.study + 1.0-SNAPSHOT + + 4.0.0 + + java-util + ${project.parent.version} + + + + org.slf4j + slf4j-api + + + + + \ No newline at end of file diff --git a/java-web/pom.xml b/java-web/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..cc4ee6f5a3d1042af6d27602bb6ced716aacfe65 --- /dev/null +++ b/java-web/pom.xml @@ -0,0 +1,165 @@ + + + + java-demo + com.study + 1.0-SNAPSHOT + + 4.0.0 + + java-web + ${project.parent.version} + + + com.study.java.AppMain + + + + + com.study + java-service + ${project.parent.version} + + + org.springframework.boot + spring-boot-starter-web + compile + + + org.springframework.boot + spring-boot-starter-tomcat + compile + + + org.springframework + spring-test + compile + + + org.springframework.boot + spring-boot-test + compile + + + spring-boot + org.springframework.boot + + + + + junit + junit + test + + + com.google.guava + guava + compile + + + org.projectlombok + lombok + compile + + + + + io.springfox + springfox-swagger2 + compile + + + + io.springfox + springfox-swagger-ui + compile + + + + com.github.xiaoymin + swagger-bootstrap-ui + compile + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot-mvn-version} + + ${app.main.class} + ${project.parent.name} + + + + + repackage + + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + ${maven-assembly-version} + + + src/main/resources/assembly.xml + + + + + + java-demo + false + + make-assembly + package + + single + + + + + + + + + + + src/lib + BOOT-INF/lib + + **/*.jar + + + + + src/main/resources + + assembly.xml + control.sh + *.sql + + + + + src/main/resources + BOOT-INF/classes + + assembly.xml + control.sh + *.sql + + + + + \ No newline at end of file diff --git a/java-web/src/main/java/com/study/java/AppMain.java b/java-web/src/main/java/com/study/java/AppMain.java new file mode 100644 index 0000000000000000000000000000000000000000..7b90477a7847321ca20d3ef1f25dda7824193ea7 --- /dev/null +++ b/java-web/src/main/java/com/study/java/AppMain.java @@ -0,0 +1,48 @@ +package com.study.java; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; + +import java.util.Properties; + +/** + * @author: wangmy + * @date: 2023/05/13 + * @email: wangmywork@163.com + * @description: app启动入口 + */ +@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) +@Slf4j +public class AppMain { + + public static final String ENV = "env"; + public static final String DEFAULT_ENV = "test"; + public static final String SPRING_CONFIG_NAME = "spring.config.name"; + public static final String DEFAULT_SPRING_CONFIG_NAME = "app"; + public static final String SPRING_PROFILES_ACTIVE = "spring.profiles.active"; + + /** + * java -Denv=test -jar java-demo.jar + * + * @param args + */ + public static void main(String[] args) { + SpringApplication application = new SpringApplication(AppMain.class); + String env = System.getProperty(ENV); + if (env == null || "".equals(env)) { + env = DEFAULT_ENV; + log.info("env is null,use default env:{}", env); + } else { + log.info("env is not null,env:{}", env); + } + + Properties props = new Properties(); + props.setProperty(SPRING_CONFIG_NAME, DEFAULT_SPRING_CONFIG_NAME); + props.setProperty(SPRING_PROFILES_ACTIVE, env); + + application.setDefaultProperties(props); + application.run(args); + } +} diff --git a/java-web/src/main/java/com/study/java/config/GlobalExceptionHandlerConfig.java b/java-web/src/main/java/com/study/java/config/GlobalExceptionHandlerConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..207bf3858d9e2f9c98e564c07dd4dbaa8711e385 --- /dev/null +++ b/java-web/src/main/java/com/study/java/config/GlobalExceptionHandlerConfig.java @@ -0,0 +1,148 @@ +package com.study.java.config; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.study.java.dal.dto.ResponseDto; +import com.study.java.util.ErrorLogUtils; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.validation.BindException; +import org.springframework.validation.BindingResult; +import org.springframework.validation.FieldError; +import org.springframework.validation.ObjectError; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.MissingServletRequestParameterException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.util.NestedServletException; + +import javax.servlet.http.HttpServletResponse; +import java.util.LinkedHashMap; +import java.util.List; + +/** + * @author: wangmy + * @date: 2023/05/13 + * @email: wangmywork@163.com + * @description: 全局异常处理类 + */ +@ControllerAdvice({"com.study.java"}) +@Slf4j +public class GlobalExceptionHandlerConfig { + private static ObjectMapper objectMapper = new ObjectMapper(); + + @ExceptionHandler(IllegalArgumentException.class) + @ResponseBody + public ResponseDto handleException(IllegalArgumentException e) { + this.printException(e); + return ResponseDto.failed("非法参数异常!错误信息:%s", e.getMessage()); + } + + @ExceptionHandler(MissingServletRequestParameterException.class) + @ResponseBody + public ResponseDto handleException(MissingServletRequestParameterException e) { + this.printException(e); + return ResponseDto.failed("参数缺失异常!错误信息:%s", e.getMessage()); + } + + @ExceptionHandler(HttpMessageNotReadableException.class) + @ResponseBody + public ResponseDto handleException(HttpMessageNotReadableException e) { + this.printException(e); + return ResponseDto.failed("参数缺失解析失败异常!错误信息:%s", e.getMessage()); + } + + @ExceptionHandler(BindException.class) + @ResponseBody + public ResponseDto handleException(BindException e) { + this.printException(e); + return ResponseDto.failed(getBindExceptionMessage(e.getBindingResult())); + } + + @ExceptionHandler(MethodArgumentNotValidException.class) + @ResponseBody + public ResponseDto handleException(MethodArgumentNotValidException e) { + this.printException(e); + return ResponseDto.failed(getBindExceptionMessage(e.getBindingResult())); + } + + @ExceptionHandler(NullPointerException.class) + @ResponseBody + public ResponseDto handleException(NullPointerException e) { + this.printException(e); + return ResponseDto.failed("空指针异常!错误信息:%s", e.getMessage()); + } + + /** + * 处理未被扫描到的controller嵌套异常 + * + * @param e 异常信息 + * @return 提示信息 + */ + @ExceptionHandler(NestedServletException.class) + @ResponseBody + public ResponseDto handleException(NestedServletException e) { + Throwable rootCause = e.getRootCause(); + this.printException(rootCause); + return ResponseDto.failed("controller嵌套异常!错误信息:%s", rootCause.getMessage()); + } + + @ExceptionHandler(RuntimeException.class) + @ResponseBody + public ResponseDto handleException(RuntimeException e) { + this.printException(e); + return ResponseDto.failed("运行时异常!错误信息:%s", e.getMessage()); + } + + @ExceptionHandler(Exception.class) + @ResponseBody + public ResponseDto handleException(Exception e) { + this.printException(e); + return ResponseDto.failed("检查型异常!错误信息:%s", e.getMessage()); + } + + @ExceptionHandler(Throwable.class) + @ResponseBody + public ResponseDto handleException(Throwable e) { + this.printException(e); + return ResponseDto.failed("通用型异常!错误信息:%s", e.getMessage()); + } + + private void printException(Throwable throwable) { + ErrorLogUtils.print(throwable); + } + + private String getBindExceptionMessage(BindingResult bindingResult) { + StringBuilder stringBuilder = new StringBuilder("参数错误"); + if (bindingResult.hasErrors()) { + stringBuilder.append(",错误信息:"); + List allErrors = bindingResult.getAllErrors(); + if (allErrors.size() > 0) { + allErrors.forEach(e -> { + FieldError fieldError = (FieldError) e; + stringBuilder.append(fieldError.getDefaultMessage()).append(";"); + }); + } + } + return stringBuilder.toString(); + } + + private ModelAndView doNotLoginResponse(HttpServletResponse resp, + String redirect) { + if (resp.isCommitted()) { + return new ModelAndView(); + } + LinkedHashMap model = new LinkedHashMap<>(); + model.put("return_code", "not_login"); + model.put("message", "未登录"); + model.put("redirect", redirect); + try { + resp.setHeader("Content-Type", "application/json;charset=UTF-8"); + resp.getWriter().write(objectMapper.writeValueAsString(model)); + } catch (Exception e) { + log.error("doNotLoginResponse exception", e); + } + return new ModelAndView(); + } +} diff --git a/java-web/src/main/java/com/study/java/config/Swagger2Config.java b/java-web/src/main/java/com/study/java/config/Swagger2Config.java new file mode 100644 index 0000000000000000000000000000000000000000..4ed984654bf3878c1f2fe80b4414e0acf2b26a3e --- /dev/null +++ b/java-web/src/main/java/com/study/java/config/Swagger2Config.java @@ -0,0 +1,65 @@ +package com.study.java.config; + +import com.google.common.base.Predicates; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +/** + * @author: wangmy + * @date: 2023/05/13 + * @email: wangmywork@163.com + * @description: swagger2配置类 + * @ApiModelProperty(value="创建时间",example="2020-01-01 00:00:00") 实体类描述注解 + * @Api(tags="java模型") 类描述注解 + * @ApiOperation("创建用户") 方法描述注解 + * @ApiOperation(value="创建用户",notes="创建用户") 方法描述注解 + * @ApiParam(value="用户名",required=true,example="tom") 参数描述注解 + * @Configuration 配置类 + * @EnableSwagger2 开启swagger2 + * @EnableWebMvc 兼容spring2.6.0及之后版本 + */ +@Configuration +@EnableSwagger2 +@EnableWebMvc +public class Swagger2Config implements WebMvcConfigurer { + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/**").addResourceLocations( + "classpath:/static/"); + registry.addResourceHandler("swagger-ui.html").addResourceLocations( + "classpath:/META-INF/resources/"); + registry.addResourceHandler("/webjars/**").addResourceLocations( + "classpath:/META-INF/resources/webjars/"); + WebMvcConfigurer.super.addResourceHandlers(registry); + } + + @Bean + public Docket apiConfig() { + return new Docket(DocumentationType.SWAGGER_2) + .groupName("api") + .apiInfo(apiInfo()) + .select() + // 只显示/java/demo路径下的页面 + .paths(Predicates.and(PathSelectors.regex("/java/demo/.*"))) + .build(); + } + + private ApiInfo apiInfo() { + return new ApiInfoBuilder() + .title("java演示项目") + .description("java演示项目api") + .version("1.0") + // .contact(new Contact("name","url","email")) + .build(); + } +} diff --git a/java-web/src/main/java/com/study/java/controller/UserController.java b/java-web/src/main/java/com/study/java/controller/UserController.java new file mode 100644 index 0000000000000000000000000000000000000000..f62d40a831cf45b5d2a4a075e28f2349ea621efe --- /dev/null +++ b/java-web/src/main/java/com/study/java/controller/UserController.java @@ -0,0 +1,42 @@ +package com.study.java.controller; + +import com.study.java.dal.dto.ResponseDto; +import com.study.java.dal.entity.User; +import com.study.java.service.UserService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author: wangmy + * @date: 2023/05/13 + * @email: wangmywork@163.com + * @description: 用户控制层 + */ +@Api(tags = "用户管理") +@RestController +@RequestMapping(value = "/java/demo/api/user") +public class UserController { + + @Autowired + @Qualifier(value = "primaryUserService") + private UserService userService; + + /** + * 获取用户 + * + * @param id 用户id + * @return 用户信息 + */ + @ApiOperation(value = "获取用户详情") + @GetMapping + public ResponseDto getUser(@RequestParam("id") Long id) { + return ResponseDto.success(userService.getUser(id)); + } + +} diff --git a/java-web/src/main/java/com/study/java/util/ErrorLogUtils.java b/java-web/src/main/java/com/study/java/util/ErrorLogUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..32c0de4900d6dfab08ecac1a7d8a179b53b62330 --- /dev/null +++ b/java-web/src/main/java/com/study/java/util/ErrorLogUtils.java @@ -0,0 +1,35 @@ +package com.study.java.util; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.MissingServletRequestParameterException; +import org.springframework.web.util.NestedServletException; + +import java.net.BindException; + +/** + * @author: wangmy + * @date: 2023/05/13 + * @email: wangmywork@163.com + * @description: 错误日志工具 + */ +public class ErrorLogUtils { + private static final Logger logger = LoggerFactory.getLogger(ErrorLogUtils.class); + + public static void print(Throwable e) { + if (e instanceof NestedServletException) { + e = ((NestedServletException) e).getRootCause(); + } + if (e instanceof IllegalArgumentException + || e instanceof MissingServletRequestParameterException + || e instanceof HttpMessageNotReadableException + || e instanceof BindException + || e instanceof MethodArgumentNotValidException) { + logger.warn(String.format("[参数错误异常],错误信息:%s", e.getMessage()), e); + return; + } + logger.error(String.format("[未定义异常],错误信息:%s", e.getMessage()), e); + } +} diff --git a/java-web/src/main/resources/app-pre.properties b/java-web/src/main/resources/app-pre.properties new file mode 100644 index 0000000000000000000000000000000000000000..61f79800cd6aa1a10ba4013b940dad710b35a6d1 --- /dev/null +++ b/java-web/src/main/resources/app-pre.properties @@ -0,0 +1,115 @@ +spring.application.name=java-demo +server.port=8080 +#õǰĿʱתȥҳ档 +#server.error.path= +server.servlet.session.timeout=3600m +server.servlet.context-path=/ +server.tomcat.max-threads=1024 +# Tomcat ־ʱļĿ¼ãĬʹϵͳʱĿ¼ +server.tomcat.basedir=tomcat +#κθʱܺʹ +server.tomcat.max-connections=9120 +#ȴег +server.tomcat.accept-count=100 +#ڸʱܽܵ +server.tomcat.threads.max=200 +#С߳ +server.tomcat.threads.min-spare=10 +# URI ַ +server.tomcat.uri-encoding=UTF-8 +#κ HTTP POSTбݵС +server.tomcat.max-http-form-post-size=20MB +#ĵĴС +server.tomcat.max-swallow-size=20MB +#ڽӺ󽫵ȴ URI еʱ +server.tomcat.connection-timeout=1m +#ӹر֮ǰȴһ HTTP ʱ +server.tomcat.keep-alive-timeout=2m +#ӹر֮ǰͨܵ HTTP +server.tomcat.max-keep-alive-requests=100 +# Ƿ񻺳ʹˢ¡ +server.tomcat.accesslog.buffered=true +# Ƿ־ļǷڣԱⲿʱ´ +server.tomcat.accesslog.check-exists=false +# ǷڡServletRequest.getAttribute(conditionIf) null ʱ־¼ +server.tomcat.accesslog.condition-if=String +# ǷڡServletRequest.getAttribute(conditionUnless) null ʱ¼ +server.tomcat.accesslog.condition-unless=String +# ÷־ +server.tomcat.accesslog.enabled=true +# ־ļĿ¼ ǾԵĻ Tomcat Ŀ¼ +server.tomcat.accesslog.directory=logs +# ־ļʹõַ ĬΪϵͳĬַ +#server.tomcat.accesslog.encoding=String +# ־ļеڸʽ +server.tomcat.accesslog.file-date-format=.yyyy-MM-dd +# Ƿʹ RFC 5952 IPv6 淶ʾʽ +server.tomcat.accesslog.ipv6-canonical=false +# ڸʽ־Ŀ־ļ׺еʱԻ ĬΪ Java ̵ĬԻ +server.tomcat.accesslog.locale=String +# ɾ־ļ֮ǰ +server.tomcat.accesslog.max-days=-7 +# ־ĸʽģʽ +server.tomcat.accesslog.pattern=common +# ־ļǰ׺ +server.tomcat.accesslog.prefix=access_log +# ǷƳļаڴֱֻʱ䡣 +server.tomcat.accesslog.rename-on-rotate=false +# Ϊʹõ IP ַЭͶ˿ԡ +server.tomcat.accesslog.request-attributes-enabled=false +# Ƿ÷־ֻ +server.tomcat.accesslog.rotate=true +# ־ļ׺ +server.tomcat.accesslog.suffix=.log +# ŷָĸģʽбƥ jar Ժ TLD ɨ衣 ġ '*' ַģʽʹãԷֱƥһһַԼַ +server.tomcat.additional-tld-skip-patterns=List +# backgroundProcess ֮ӳ١ δָʱ׺ʹ롣 +server.tomcat.background-processor-delay=10s +# ǷӦ Tomcat MBean Registry +server.tomcat.mbeanregistry.enabled=false +# ڻвںõд Ϊ -1 ʱ潫ƣϵС +server.tomcat.processor-cache=200 +# ǷӦͨ / ӵ·ضĸ ʹڴֹ SSL ʱӦΪ false +server.tomcat.redirect-context-root=true +# URI ·ӦδַĶŷָб ֻʹá< > [ \ ] ^ ` { | } +#server.tomcat.relaxed-path-chars=List +# URI ѯַӦδַĶŷָб ֻʹá< > [ \ ] ^ ` { | } +#server.tomcat.relaxed-query-chars=List +# ȡԶ HTTP ͷơ +server.tomcat.remoteip.host-header=X-Forwarded-Host +# ƥҪεĴʽ +server.tomcat.remoteip.internal-proxies=10\.\d{1,3}\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3}|169\.254\.\d{1,3}\.\d{1,3}|127\.\d{1,3}\.\d{1,3}\.\d{1,3}|172\.1[6-9]{1}\.\d{1,3}\.\d{1,3}|172\.2[0-9]{1}\.\d{1,3}\.\d{1,3}|172\.3[0-1]{1}\.\d{1,3}\.\d{1,3}|0=0=0=0=0=0=0=1|==1 +# ڸԭʼ˿ֵ HTTP ͷơ +server.tomcat.remoteip.port-header=X-Forwarded-Port +# ЭıͷͨΪX-Forwarded-Proto +server.tomcat.remoteip.protocol-header=X-Forwarded-Proto +# ָʾǷʹ SSL Эͷֵ. +server.tomcat.remoteip.protocol-header-https-value=https +# ȡԶ IP HTTP ͷơ 磬`X-FORWARDED-FOR` +server.tomcat.remoteip.remote-ip-header=X-FORWARDED-FOR +# Web ӦóǷ̬Դ档 +server.tomcat.resource.allow-caching=true +# ̬Դʱ䡣 +server.tomcat.resource.cache-ttl=-1 +# ͨ sendRedirect ɵ HTTP 1.1 ͸߰汾λñͷʹضǾض +server.tomcat.use-relative-redirects=false +spring.servlet.multipart.max-file-size=200MB +spring.servlet.multipart.max-request-size=200MB +logging.level.root=info +logging.file.path=tomcat/logs +logging.file.max-history=10 +logging.file.max-size=30MB +logging.pattern.console=%d{yyyy/MM/dd-HH:mm:ss.SSS} [%thread] %-5level %logger- %msg%n +logging.pattern.file=%d{yyyy/MM/dd-HH:mm:ss.SSS} [%thread] %-5level %logger- %msg%n +logging.pattern.dateformat=yyyy-MM-dd HH:mm:ss.SSS + +#ݿ +spring.datasource.primary.url=jdbc:mysql://localhost:3306/java_demo_pre?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false +spring.datasource.primary.username=root +spring.datasource.primary.password=root +spring.datasource.primary.driverClassName=com.mysql.jdbc.Driver +##ôݿ +spring.datasource.secondary.url=jdbc:mysql://localhost:3306/java_demo2_pre?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false +spring.datasource.secondary.username=root +spring.datasource.secondary.password=root +spring.datasource.secondary.driverClassName=com.mysql.jdbc.Driver diff --git a/java-web/src/main/resources/app-test.properties b/java-web/src/main/resources/app-test.properties new file mode 100644 index 0000000000000000000000000000000000000000..7ac6e95bd08e60cf277d47123b997876133c60e0 --- /dev/null +++ b/java-web/src/main/resources/app-test.properties @@ -0,0 +1,115 @@ +spring.application.name=java-demo +server.port=8080 +#õǰĿʱתȥҳ档 +#server.error.path= +server.servlet.session.timeout=3600m +server.servlet.context-path=/ +server.tomcat.max-threads=1024 +# Tomcat ־ʱļĿ¼ãĬʹϵͳʱĿ¼ +server.tomcat.basedir=tomcat +#κθʱܺʹ +server.tomcat.max-connections=9120 +#ȴег +server.tomcat.accept-count=100 +#ڸʱܽܵ +server.tomcat.threads.max=200 +#С߳ +server.tomcat.threads.min-spare=10 +# URI ַ +server.tomcat.uri-encoding=UTF-8 +#κ HTTP POSTбݵС +server.tomcat.max-http-form-post-size=20MB +#ĵĴС +server.tomcat.max-swallow-size=20MB +#ڽӺ󽫵ȴ URI еʱ +server.tomcat.connection-timeout=1m +#ӹر֮ǰȴһ HTTP ʱ +server.tomcat.keep-alive-timeout=2m +#ӹر֮ǰͨܵ HTTP +server.tomcat.max-keep-alive-requests=100 +# Ƿ񻺳ʹˢ¡ +server.tomcat.accesslog.buffered=true +# Ƿ־ļǷڣԱⲿʱ´ +server.tomcat.accesslog.check-exists=false +# ǷڡServletRequest.getAttribute(conditionIf) null ʱ־¼ +server.tomcat.accesslog.condition-if=String +# ǷڡServletRequest.getAttribute(conditionUnless) null ʱ¼ +server.tomcat.accesslog.condition-unless=String +# ÷־ +server.tomcat.accesslog.enabled=true +# ־ļĿ¼ ǾԵĻ Tomcat Ŀ¼ +server.tomcat.accesslog.directory=logs +# ־ļʹõַ ĬΪϵͳĬַ +#server.tomcat.accesslog.encoding=String +# ־ļеڸʽ +server.tomcat.accesslog.file-date-format=.yyyy-MM-dd +# Ƿʹ RFC 5952 IPv6 淶ʾʽ +server.tomcat.accesslog.ipv6-canonical=false +# ڸʽ־Ŀ־ļ׺еʱԻ ĬΪ Java ̵ĬԻ +server.tomcat.accesslog.locale=String +# ɾ־ļ֮ǰ +server.tomcat.accesslog.max-days=-7 +# ־ĸʽģʽ +server.tomcat.accesslog.pattern=common +# ־ļǰ׺ +server.tomcat.accesslog.prefix=access_log +# ǷƳļаڴֱֻʱ䡣 +server.tomcat.accesslog.rename-on-rotate=false +# Ϊʹõ IP ַЭͶ˿ԡ +server.tomcat.accesslog.request-attributes-enabled=false +# Ƿ÷־ֻ +server.tomcat.accesslog.rotate=true +# ־ļ׺ +server.tomcat.accesslog.suffix=.log +# ŷָĸģʽбƥ jar Ժ TLD ɨ衣 ġ '*' ַģʽʹãԷֱƥһһַԼַ +server.tomcat.additional-tld-skip-patterns=List +# backgroundProcess ֮ӳ١ δָʱ׺ʹ롣 +server.tomcat.background-processor-delay=10s +# ǷӦ Tomcat MBean Registry +server.tomcat.mbeanregistry.enabled=false +# ڻвںõд Ϊ -1 ʱ潫ƣϵС +server.tomcat.processor-cache=200 +# ǷӦͨ / ӵ·ضĸ ʹڴֹ SSL ʱӦΪ false +server.tomcat.redirect-context-root=true +# URI ·ӦδַĶŷָб ֻʹá< > [ \ ] ^ ` { | } +#server.tomcat.relaxed-path-chars=List +# URI ѯַӦδַĶŷָб ֻʹá< > [ \ ] ^ ` { | } +#server.tomcat.relaxed-query-chars=List +# ȡԶ HTTP ͷơ +server.tomcat.remoteip.host-header=X-Forwarded-Host +# ƥҪεĴʽ +server.tomcat.remoteip.internal-proxies=10\.\d{1,3}\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3}|169\.254\.\d{1,3}\.\d{1,3}|127\.\d{1,3}\.\d{1,3}\.\d{1,3}|172\.1[6-9]{1}\.\d{1,3}\.\d{1,3}|172\.2[0-9]{1}\.\d{1,3}\.\d{1,3}|172\.3[0-1]{1}\.\d{1,3}\.\d{1,3}|0=0=0=0=0=0=0=1|==1 +# ڸԭʼ˿ֵ HTTP ͷơ +server.tomcat.remoteip.port-header=X-Forwarded-Port +# ЭıͷͨΪX-Forwarded-Proto +server.tomcat.remoteip.protocol-header=X-Forwarded-Proto +# ָʾǷʹ SSL Эͷֵ. +server.tomcat.remoteip.protocol-header-https-value=https +# ȡԶ IP HTTP ͷơ 磬`X-FORWARDED-FOR` +server.tomcat.remoteip.remote-ip-header=X-FORWARDED-FOR +# Web ӦóǷ̬Դ档 +server.tomcat.resource.allow-caching=true +# ̬Դʱ䡣 +server.tomcat.resource.cache-ttl=-1 +# ͨ sendRedirect ɵ HTTP 1.1 ͸߰汾λñͷʹضǾض +server.tomcat.use-relative-redirects=false +spring.servlet.multipart.max-file-size=200MB +spring.servlet.multipart.max-request-size=200MB +logging.level.root=info +logging.file.path=tomcat/logs +logging.file.max-history=10 +logging.file.max-size=30MB +logging.pattern.console=%d{yyyy/MM/dd-HH:mm:ss.SSS} [%thread] %-5level %logger- %msg%n +logging.pattern.file=%d{yyyy/MM/dd-HH:mm:ss.SSS} [%thread] %-5level %logger- %msg%n +logging.pattern.dateformat=yyyy-MM-dd HH:mm:ss.SSS + +#ݿ +spring.datasource.primary.url=jdbc:mysql://localhost:3306/java_demo_test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false +spring.datasource.primary.username=root +spring.datasource.primary.password=root +spring.datasource.primary.driverClassName=com.mysql.jdbc.Driver +##ôݿ +spring.datasource.secondary.url=jdbc:mysql://localhost:3306/java_demo2_test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false +spring.datasource.secondary.username=root +spring.datasource.secondary.password=root +spring.datasource.secondary.driverClassName=com.mysql.jdbc.Driver diff --git a/java-web/src/main/resources/assembly.xml b/java-web/src/main/resources/assembly.xml new file mode 100644 index 0000000000000000000000000000000000000000..6d8dcc17daf801e8ae4f255dbb50be7c8ad733a1 --- /dev/null +++ b/java-web/src/main/resources/assembly.xml @@ -0,0 +1,26 @@ + + package + + zip + + false + + + src/main/resources + + *.sh + + / + + + target + + *.jar + + / + + + \ No newline at end of file diff --git a/java-web/src/main/resources/control.sh b/java-web/src/main/resources/control.sh new file mode 100644 index 0000000000000000000000000000000000000000..930ad8e5fcac3a931fe636811832c70ce586987a --- /dev/null +++ b/java-web/src/main/resources/control.sh @@ -0,0 +1,102 @@ +#运行示例: +#sh -x control.sh \ +#-method start \ +#-env test + +while [ -n "$1" ]; do + case "$1" in + -method) + METHOD_NAME=$2 + echo "${METHOD_NAME}" + shift 2 + ;; + -env) + ENV=$2 + echo "${ENV}" + shift 2 + ;; + -help) + echo -e "OPTIONS:" + echo -e "method:执行方法" + echo -e "env:环境" + exit 1 + ;; + *) + echo "Invalid option: $1" + exit 1 + ;; + esac +done + +METHOD_NAME=${METHOD_NAME:=} +ENV=${ENV:=} + +if [[ -n "${METHOD_NAME}" ]] || [[ -n "${ENV}" ]]; then + echo "necessary arg not point" + exit 2 +fi + +XMX_MEMORY="2gb" +#解析内存大小 +parseMemorySize() { + MEMORY_SIZE=${XMX_MEMORY} + echo "MEMORY_SIZE=${MEMORY_SIZE}" + # 判断参数是否为空 + if [ -z ${MEMORY_SIZE} ]; then + echo "Usage: $0 " + exit 1 + fi + + # 获取参数中的数字部分和单位部分 + NUM=$(echo ${MEMORY_SIZE} | sed 's/[^0-9]*//g') + UNIT=$(echo ${MEMORY_SIZE} | sed 's/[0-9]*//g') + + # 将单位转换为小写字母 + UNIT=$(echo ${UNIT} | tr '[:upper:]' '[:lower:]') + + # 根据单位计算大小(MB) + case ${UNIT} in + "gb") + MEMORY_SIZE_MB=$(echo "${NUM} * 1024" | bc) + ;; + "mb") + MEMORY_SIZE_MB=${NUM} + ;; + "kb") + MEMORY_SIZE_MB=$(echo "${NUM} / 1024" | bc) + ;; + *) + echo "Invalid unit: ${UNIT}" + exit 1 + ;; + esac + + # 输出结果 + XMX_MEMORY_MB=${MEMORY_SIZE_MB} + echo "XMX_MEMORY_MB:${XMX_MEMORY_MB}" +} + +#关闭 +stop() { + PID=$(ps -ef | grep AppMain | grep -v grep | awk '{print $2}') + echo "PID=${PID}" + $(kill -9 ${PID}) +} + +#启动 +start() { + $(java -Denv=${ENV} -jar java-demo.jar) +} + +# See how we were called. +case "${METHOD_NAME}" in +start) + start + ;; +*) + echo $"Usage: $0 {start}" + exit 2 + ;; +esac + +exit $? diff --git a/java-web/src/main/resources/db-init.sql b/java-web/src/main/resources/db-init.sql new file mode 100644 index 0000000000000000000000000000000000000000..c82c27035157b723e32ff0d64e71486b4a356cb6 --- /dev/null +++ b/java-web/src/main/resources/db-init.sql @@ -0,0 +1,10 @@ +-- 创建数据库 +create database java_demo_test; + +use java_demo_test; +-- 创建用户表 +create table user( +`id` bigint(19) primary key not null auto_increment comment 'id', +`user_name` varchar(200) default null comment '用户名', +`password` varchar(200) default null comment '密码' +) \ No newline at end of file diff --git a/java-web/src/test/java/com/study/java/service/TestUserService.java b/java-web/src/test/java/com/study/java/service/TestUserService.java new file mode 100644 index 0000000000000000000000000000000000000000..6d9dd92fc674c3783d18a10100d92750f4b9967e --- /dev/null +++ b/java-web/src/test/java/com/study/java/service/TestUserService.java @@ -0,0 +1,42 @@ +package com.study.java.service; + +import com.study.java.AppMain; +import com.study.java.dal.entity.User; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * @author: wangmy + * @date: 2023/05/13/1:16 + * @email: wangmywork@163.com + * @description: 测试用户服务 + */ +@RunWith(SpringRunner.class) +@SpringBootTest(properties = {AppMain.SPRING_CONFIG_NAME + "=" + AppMain.DEFAULT_SPRING_CONFIG_NAME, + AppMain.SPRING_PROFILES_ACTIVE + "=" + AppMain.DEFAULT_ENV}) +public class TestUserService { + + @Autowired + @Qualifier(value = "primaryUserService") + private UserService primaryUserService; + + @Autowired + @Qualifier(value = "secondaryUserService") + private UserService secondaryUserService; + + @Test + public void testInsertUser() { + User user1 = User.builder().userName("user1").password("123").build(); + int user1InsertNum = primaryUserService.addUser(user1); + System.out.println("user1InsertNum = " + user1InsertNum); + + User user2 = User.builder().userName("user2").password("456").build(); + int user2InsertNum = secondaryUserService.addUser(user2); + System.out.println("user2InsertNum = " + user2InsertNum); + + } +} diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..3dd701e645b9e924bb99f70df29fa2b41e547960 --- /dev/null +++ b/pom.xml @@ -0,0 +1,169 @@ + + + 4.0.0 + + com.study + java-demo + pom + 1.0-SNAPSHOT + + java-web + java-service + java-dao + java-util + java-dal + + + + 2.6.1 + 2.6.5 + 2.6.5 + 2.6 + 5.3.13 + 5.3.17 + 3.5.3 + 4.12 + 28.0-jre + 1.7.30 + 1.18.22 + 2.9.2 + 1.9.2 + 2.2.2 + 5.1.47 + 2.13.0 + 4.4.13 + + + + + + + org.springframework.boot + spring-boot-starter + ${spring-boot-version} + + + org.springframework.boot + spring-boot-autoconfigure + ${spring-boot-version} + + + org.springframework.boot + spring-boot-starter-web + ${spring-boot-version} + + + org.springframework.boot + spring-boot-starter-tomcat + ${spring-boot-version} + + + org.springframework + spring-test + ${spring-test-version} + + + org.springframework.boot + spring-boot-test + ${spring-boot-test-version} + + + org.springframework + spring-core + ${spring-core-version} + + + org.springframework + spring-beans + ${spring-core-version} + + + org.springframework + spring-aop + ${spring-core-version} + + + org.springframework + spring-context + ${spring-core-version} + + + org.springframework + spring-expression + ${spring-core-version} + + + + + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + ${mybatis-spring-boot-version} + + + mysql + mysql-connector-java + ${mysql-connector-version} + + + junit + junit + ${junit-version} + test + + + + + io.springfox + springfox-swagger2 + ${springfox-swagger-version} + + + + io.springfox + springfox-swagger-ui + ${springfox-swagger-version} + + + + com.github.xiaoymin + swagger-bootstrap-ui + ${swagger-bootstrap-ui-version} + + + + com.google.guava + guava + ${guava-version} + + + org.projectlombok + lombok + ${lombok.version} + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + + com.fasterxml.jackson.core + jackson-annotations + ${jackson.annotations.version} + + + + org.apache.httpcomponents + httpcore + ${apache.http.component.version} + + + + + \ No newline at end of file