plugins {
id 'org.springframework.boot' version '2.1.5.RELEASE'
id 'groovy'
id 'org.asciidoctor.convert' version '1.5.3'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.wxx.modules'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-web'
compile.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-jetty'
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
maven {
url 'http://maven.aliyun.com/nexus/content/groups/public/'
}
mavenCentral()
}
repositories {
mavenCentral()
}
dependencies {
//非传递依赖
//implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
//implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
//implementation 'org.springframework.boot:spring-boot-starter-data-mongodb-reactive'
//implementation 'org.springframework.boot:spring-boot-starter-data-redis-reactive'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-configuration-processor'
implementation 'org.springframework.boot:spring-boot-starter-reactor-netty'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-aop'
//JMS
implementation "javax.jms:javax.jms-api:2.0"
implementation "org.springframework:spring-messaging"
implementation "org.springframework:spring-jms"
implementation "com.sun.messaging.mq:imq:4.6-b01"
implementation "com.sun.messaging.mq:jms:4.6-b01"
implementation 'com.google.guava:guava:27.0.1-jre'
implementation 'org.apache.commons:commons-lang3:3.8.1'
implementation 'org.apache.commons:commons-text:1.6'
implementation 'org.apache.cxf:cxf-rt-transports-http-hc:3.2.0'
implementation 'org.apache.cxf:cxf-rt-frontend-jaxws:3.2.0'
implementation 'com.sun.xml.bind:jaxb-core:2.2.11'
implementation 'com.sun.xml.bind:jaxb-impl:2.2.11'
implementation 'commons-lang:commons-lang:2.6'
implementation 'commons-collections:commons-collections:3.2.2'
implementation 'org.apache.commons:commons-collections4:4.3'
implementation 'joda-time:joda-time:2.10.1'
implementation 'com.graphql-java:java-dataloader:2.2.1'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1'
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.15'
implementation group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '6.1.0.jre7'
implementation group: 'capital.scalable', name: 'spring-auto-restdocs-json-doclet', version: '2.0.4'
//测试时编译
testCompile group: 'capital.scalable', name: 'spring-auto-restdocs-core', version: '2.0.4'
//编译时依赖
compileOnly 'org.projectlombok:lombok'
//注解处理器
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
//测试时依赖
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation "org.spockframework:spock-core:1.2-groovy-2.4"
testImplementation 'org.mockito:mockito-core:2.24.5'
testImplementation 'io.projectreactor:reactor-test:3.2.6.RELEASE'
}
management:
endpoints.web:
base-path: /management
exposure.include: '*'
server.port: 9090
server:
connection-timeout: 30000ms
port: 8081
servlet.context-path: /
logging:
path: ./
file: st.log
#max-size: 10M
#max-history: 1
level:
com:
wxx:
modules:
mapper: debug
spring:
data:
mongodb:
database: studentService
host: localhost
port: 27017
datasource:
url: jdbc:mysql://localhost:3306/studentService?useSSL=false&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 12345678
# schema.sql中一般存放的是DDL脚本,即通常为创建或更新库表的脚本
# data.sql中一般是DML脚本,即通常为数据插入脚本
schema: classpath:schema.sql
data: classpath:data.sql
platform: mysql
initialization-mode: always
continue-on-error: false
#data-password:
#data-username:
#schema-password:
#schema-username:
sql-script-encoding: utf-8
separator: ;
mybatis:
typeAliasesPackage: com.com.wxx.modules.st.domain
mapper-locations: classpath:mapper\/*.xml*/
master:
name: EvanDevyml
sex: nv
subject: chi
schema.sql
CREATE DATABASE IF NOT EXISTS `studentService`;
CREATE TABLE IF NOT EXISTS `t_student` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) DEFAULT NULL,
`age` INT(255) DEFAULT NULL,
`sex` VARCHAR(255) DEFAULT NULL,
`phone` INT(255) DEFAULT NULL,
`address` VARCHAR(255) DEFAULT NULL,
`citation_count` INT(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `t_student_score` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`subject` VARCHAR(255) DEFAULT NULL,
`student_id` INT(255) DEFAULT NULL,
`subject_score` INT(255),
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=1;
data.sql
INSERT INTO `t_student` (`name`,`age`, `sex`, `phone`, `address`, `citation_count`) VALUES('小明',10,'男',111,'小明家',1);
INSERT INTO `t_student` (`name`,`age`, `sex`, `phone`, `address`, `citation_count`) VALUES('小红',11,'男',112,'小红家',3);
INSERT INTO `t_student` (`name`,`age`, `sex`, `phone`, `address`, `citation_count`) VALUES('小灰',9,'男',113,'小灰家',0);
INSERT INTO `t_student` (`name`,`age`, `sex`, `phone`, `address`, `citation_count`) VALUES('小朋',8,'男',114,'小朋家',2);
INSERT INTO `t_student_score` (`subject`, `student_id`, `subject_score`) VALUES('语文','1','30');
INSERT INTO `t_student_score` (`subject`, `student_id`, `subject_score`) VALUES('数学','1','40');
INSERT INTO `t_student_score` (`subject`, `student_id`, `subject_score`) VALUES('语文','2','50');
INSERT INTO `t_student_score` (`subject`, `student_id`, `subject_score`) VALUES('数学','2','35');
INSERT INTO `t_student_score` (`subject`, `student_id`, `subject_score`) VALUES('语文','3','50');
INSERT INTO `t_student_score` (`subject`, `student_id`, `subject_score`) VALUES('数学','3','35');
INSERT INTO `t_student_score` (`subject`, `student_id`, `subject_score`) VALUES('语文','4','50');
INSERT INTO `t_student_score` (`subject`, `student_id`, `subject_score`) VALUES('数学','4','35');
commit;
@Data
@Builder
public class Score {
private Integer id;
//科目
private String subject;
//学生编号
private String studentId;
//科目成绩
private Integer subjectScore;
}
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Student {
private Integer id;
private String name;
private Integer age;
private String sex;
private String phone;
private String address;
private Integer citationCount;
}
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class StudentScores {
private Integer id;
private String studentName;
private String studentPhone;
private List<Score> scores;
}
import com.wxx.modules.st.domain.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
@Mapper
public interface StudentMapper {
public Student findStudentById(@Param("id") Integer id);
//新增学生信息
public Integer addStudent(Student student);
//查询所有同学信息
public Integer deleteByStudent(Student student);
//查询所有同学信息
public Integer deleteByStudentName(String studentName);
//查询所有同学信息
public Integer updateStudent(Student student);
//查询所有同学信息
public List<Student> findStudents();
//根据Map条件查询所有同学
public List<Student> findStudentByMap(Map<String, String> params);
//根据String条件查询所有同学
public List<Student> findStudentByParams(String studentPhone, String studentName);
//查询所有同学的所有科目成绩
public List<Student> findStudentScores();
}
StudentMapper.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.wxx.modules.st.mapper.StudentMapper">
<!--
执行增加操作的SQL语句
1.id和parameterType 分别与StudentMapper接口中的addStudent方法的名字和 参数类型一致
2.以#{studentName}的形式引用Student参数 的studentName属性,MyBatis将使用反射读取Student参数的此属性
3.#{studentName}中studentName大小写敏感。引用其他的属性与此一致
4.seGeneratedKeys设置为"true"表明要MyBatis获取由数据库自动生成的主键
5.keyProperty="id"指定把获取到的主键值注入到Student的id属性
-->
<!-- 当表中字段名跟实体类字段名不一致 或 为了返回list类型 而定义的returnMap -->
<resultMap id="scoreResultMap" type="com.wxx.modules.st.domain.Score">
<id column="id" property="id"/>
<result column="subject" property="subject"/>
<result column="student_id" property="studentId"/>
<result column="subject_score" property="subjectScore"/>
</resultMap>
<resultMap id="studentMap" type="com.wxx.modules.st.domain.Student">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
<result column="address" property="address"/>
<result column="citation_count" property="citationCount"/>
</resultMap>
<!--
联合查询场景:在读取某个用户的所有成绩
当然还是需要在 StudentMapper.xml 里面配置 select 语句, 但重点是这个 select 的resultMap 对应什么样的数据呢。
这是重点,这里要引入 association 看定义如下 这样配置之后,就可以了,将 select 语句与resultMap 对应的映射结合起来看,就明白了。
用 association 来 得到关联的用户,这是多对一的情况,因为所有的文章都是同一个用户的。
-->
<resultMap id="resultStudentScoreList" type="com.wxx.modules.st.domain.StudentScores">
<id column="id" property="id"/>
<result column="name" property="studentName"/>
<result column="phone" property="studentPhone"/>
<association property="scores" javaType="List">
<id column="id" property="id"/>
<result column="subject" property="subject"/>
<result column="student_id" property="studentId"/>
<result column="subject_core" property="subjectScore"/>
</association>
</resultMap>
<!--
还有另外一种处理方式,可以复用我们前面已经定义好的resultMap ,前面我们定义过一个resultListStudent,
看这第二种方法如何实现:将association 中对应的映射独立抽取出来,可以达到复用的目的
-->
<resultMap id="resultStudentScoreList1" type="com.wxx.modules.st.domain.StudentScores">
<id column="id" property="id"/>
<result column="student_name" property="studentName"/>
<result column="student_phone" property="studentPhone"/>
<association property="scores" javaType="List" resultMap="scoreResultMap"/>
</resultMap>
<!--新增-->
<insert id="addStudent" parameterType="com.wxx.modules.st.domain.Student" useGeneratedKeys="true" keyProperty="id">
insert into t_student(name,age,sex,phone,address,citationCount)
values(#{name},#{age},#{sex},#{phone},#{address},#{citationCount})
</insert>
<!--删除 1-->
<delete id="deleteByStudentName" parameterType="String">
delete from t_student where name=#{name}
</delete>
<!--删除 2-->
<delete id="deleteByStudent" parameterType="com.wxx.modules.st.domain.Student">
delete from t_student where name=#{name}
</delete>
<!--修改-->
<update id="updateStudent" parameterType="com.wxx.modules.st.domain.Student">
update t_student set
name = #{name},
phone = #{phone}
where name=#{name}
</update>
<!--查询列表:返回list的select语句,注意resultMap的值是指向前面定义好的 -->
<select id="findStudents" resultMap="studentMap">
select * from t_student
</select>
<select id="findStudentById" parameterType="Integer" resultMap="studentMap">
SELECT * FROM t_student where id = #{id};
</select>
<!-- 利用 hashMap 传递多个参数 -->
<select id="findStudentByMap" parameterType="map" resultType="com.wxx.modules.st.domain.Student">
select * from t_student where
<where>
1=1
<if test="phone != null and phone != ''">
and phone = #{phone}
</if>
<if test="name != null and name != ''">
and name=#{name}
</if>
</where>
</select>
<!-- Mybatis 自带的多个参数传递方法 这个时候没有 parameterType, 但用到了类似 #{param1} 类似的参数 -->
<select id="findStudentByParams" resultType="com.wxx.modules.st.domain.Student">
select * from t_student where phone = #{param1} and name=#{param2}
</select>
<!-- 两个表关联查询 1 -->
<!-- 两个表关联查询 2 -->
<!-- 两个表关联查询 3 -->
</mapper>