admin 1 éve
szülő
commit
1c74d7cdf1

+ 75 - 0
myBatisSpringBoot/pom.xml

@@ -0,0 +1,75 @@
+<?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>
+
+    <groupId>org.example</groupId>
+    <artifactId>myBatisSpringBoot</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.2.2.RELEASE</version>
+    </parent>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+            <version>1.2.78</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.mybatis.spring.boot</groupId>
+            <artifactId>mybatis-spring-boot-starter</artifactId>
+            <version>1.3.1</version>
+        </dependency>
+
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>druid-spring-boot-starter</artifactId>
+            <version>1.1.10</version>
+        </dependency>
+
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 12 - 0
myBatisSpringBoot/src/main/java/org/example/Main.java

@@ -0,0 +1,12 @@
+package org.example;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class Main {
+    public static void main(String[] args){
+        SpringApplication.run(Main.class, args);
+    }
+}
+// http://localhost:8080/druid

+ 34 - 0
myBatisSpringBoot/src/main/java/org/example/Student.java

@@ -0,0 +1,34 @@
+package org.example;
+
+import java.io.Serializable;
+
+public class Student implements Serializable {
+    private static final long serialVersionUID = -339516038496531943L;
+    private String sno;
+    private String name;
+    private String sex;
+
+    public String getSno() {
+        return sno;
+    }
+
+    public void setSno(String sno) {
+        this.sno = sno;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getSex() {
+        return sex;
+    }
+
+    public void setSex(String sex) {
+        this.sex = sex;
+    }
+}

+ 18 - 0
myBatisSpringBoot/src/main/java/org/example/StudentController.java

@@ -0,0 +1,18 @@
+package org.example;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class StudentController {
+
+    @Autowired
+    private StudentService studentService;
+
+    @RequestMapping( value = "/querystudent", method = RequestMethod.GET)
+    public Student queryStudentBySno(String sno) {
+        return this.studentService.queryStudentBySno(sno);
+    }
+}

+ 25 - 0
myBatisSpringBoot/src/main/java/org/example/StudentMapper.java

@@ -0,0 +1,25 @@
+package org.example;
+
+import org.apache.ibatis.annotations.*;
+import org.springframework.stereotype.Component;
+
+@Component
+@Mapper
+public interface StudentMapper {
+    @Insert("insert into student(sno,sname,ssex) values(#{sno},#{name},#{sex})")
+    int add(Student student);
+
+    @Update("update student set sname=#{name},ssex=#{sex} where sno=#{sno}")
+    int update(Student student);
+
+    @Delete("delete from student where sno=#{sno}")
+    int deleteBysno(String sno);
+
+    @Select("select * from student where sno=#{sno}")
+    @Results(id = "student",value= {
+            @Result(property = "sno", column = "sno", javaType = String.class),
+            @Result(property = "name", column = "sname", javaType = String.class),
+            @Result(property = "sex", column = "ssex", javaType = String.class)
+    })
+    Student queryStudentBySno(String sno);
+}

+ 8 - 0
myBatisSpringBoot/src/main/java/org/example/StudentService.java

@@ -0,0 +1,8 @@
+package org.example;
+
+public interface StudentService {
+    int add(Student student);
+    int update(Student student);
+    int deleteBysno(String sno);
+    Student queryStudentBySno(String sno);
+}

+ 30 - 0
myBatisSpringBoot/src/main/java/org/example/StudentServiceImpl.java

@@ -0,0 +1,30 @@
+package org.example;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service("studentService")
+public class StudentServiceImpl implements StudentService{
+    @Autowired
+    private StudentMapper studentMapper;
+
+    @Override
+    public int add(Student student) {
+        return this.studentMapper.add(student);
+    }
+
+    @Override
+    public int update(Student student) {
+        return this.studentMapper.update(student);
+    }
+
+    @Override
+    public int deleteBysno(String sno) {
+        return this.studentMapper.deleteBysno(sno);
+    }
+
+    @Override
+    public Student queryStudentBySno(String sno) {
+        return this.studentMapper.queryStudentBySno(sno);
+    }
+}

+ 60 - 0
myBatisSpringBoot/src/main/resources/application.yml

@@ -0,0 +1,60 @@
+spring:
+  datasource:
+    druid:
+      # 数据库访问配置, 使用druid数据源
+      type: com.alibaba.druid.pool.DruidDataSource
+      driver-class-name: com.mysql.cj.jdbc.Driver
+      url: jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
+      username: root
+      password: 5608095liyukun
+      # 连接池配置
+      initial-size: 5
+      min-idle: 5
+      max-active: 20
+      # 连接等待超时时间
+      max-wait: 30000
+      # 配置检测可以关闭的空闲连接间隔时间
+      time-between-eviction-runs-millis: 60000
+      # 配置连接在池中的最小生存时间
+      min-evictable-idle-time-millis: 300000
+      validation-query: select '1' from dual
+      test-while-idle: true
+      test-on-borrow: false
+      test-on-return: false
+      # 打开PSCache,并且指定每个连接上PSCache的大小
+      pool-prepared-statements: true
+      max-open-prepared-statements: 20
+      max-pool-prepared-statement-per-connection-size: 20
+      # 配置监控统计拦截的filters, 去掉后监控界面sql无法统计, 'wall'用于防火墙
+      filters: stat,wall
+      # Spring监控AOP切入点,如x.y.z.service.*,配置多个英文逗号分隔
+      aop-patterns: com.springboot.servie.*
+
+
+      # WebStatFilter配置
+      web-stat-filter:
+        enabled: true
+        # 添加过滤规则
+        url-pattern: /*
+        # 忽略过滤的格式
+        exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
+
+      # StatViewServlet配置
+      stat-view-servlet:
+        enabled: true
+        # 访问路径为/druid时,跳转到StatViewServlet
+        url-pattern: /druid/*
+        # 是否能够重置数据
+        reset-enable: false
+        # 需要账号密码才能访问控制台
+        login-username: druid
+        login-password: druid
+        # IP白名单
+        # allow: 127.0.0.1
+        # IP黑名单(共同存在时,deny优先于allow)
+        # deny: 192.168.1.218
+
+      # 配置StatFilter
+      filter:
+        stat:
+          log-slow-sql: true