From 25ccae307cd5ff70015567f29e7c805c6fc251cf Mon Sep 17 00:00:00 2001
From: fxb <fanxb.tl@gmail.com>
Date: Wed, 1 Aug 2018 22:22:31 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0mybatis=E6=A0=B7=E4=BE=8B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 mybatis-test/pom.xml                          | 71 +++++++++++++++++++
 .../mybatistest/MybatisTestApplication.java   | 14 ++++
 .../controller/UserController.java            | 45 ++++++++++++
 .../com/example/mybatistest/dao/UserDao.java  | 28 ++++++++
 .../com/example/mybatistest/entity/User.java  | 55 ++++++++++++++
 .../mybatistest/service/UserService.java      | 45 ++++++++++++
 .../src/main/resources/application.yml        | 46 ++++++++++++
 .../src/main/resources/mapper/UserMapper.xml  | 41 +++++++++++
 .../MybatisTestApplicationTests.java          | 16 +++++
 9 files changed, 361 insertions(+)
 create mode 100644 mybatis-test/pom.xml
 create mode 100644 mybatis-test/src/main/java/com/example/mybatistest/MybatisTestApplication.java
 create mode 100644 mybatis-test/src/main/java/com/example/mybatistest/controller/UserController.java
 create mode 100644 mybatis-test/src/main/java/com/example/mybatistest/dao/UserDao.java
 create mode 100644 mybatis-test/src/main/java/com/example/mybatistest/entity/User.java
 create mode 100644 mybatis-test/src/main/java/com/example/mybatistest/service/UserService.java
 create mode 100644 mybatis-test/src/main/resources/application.yml
 create mode 100644 mybatis-test/src/main/resources/mapper/UserMapper.xml
 create mode 100644 mybatis-test/src/test/java/com/example/mybatistest/MybatisTestApplicationTests.java

diff --git a/mybatis-test/pom.xml b/mybatis-test/pom.xml
new file mode 100644
index 0000000..242becb
--- /dev/null
+++ b/mybatis-test/pom.xml
@@ -0,0 +1,71 @@
+<?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>com.example</groupId>
+    <artifactId>mybatis-test</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <packaging>jar</packaging>
+
+    <name>mybatis-test</name>
+    <description>Demo project for Spring Boot</description>
+
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.0.3.RELEASE</version>
+        <relativePath/> <!-- lookup parent from repository -->
+    </parent>
+
+    <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>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <scope>runtime</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.mybatis.spring.boot</groupId>
+            <artifactId>mybatis-spring-boot-starter</artifactId>
+            <version>1.3.2</version>
+        </dependency>
+        <!--ali连接池依赖-->
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>druid-spring-boot-starter</artifactId>
+            <version>1.1.9</version>
+        </dependency>
+        <!--分页依赖-->
+        <dependency>
+            <groupId>com.github.pagehelper</groupId>
+            <artifactId>pagehelper-spring-boot-starter</artifactId>
+            <version>1.2.5</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
+
+
+</project>
diff --git a/mybatis-test/src/main/java/com/example/mybatistest/MybatisTestApplication.java b/mybatis-test/src/main/java/com/example/mybatistest/MybatisTestApplication.java
new file mode 100644
index 0000000..c8c72ff
--- /dev/null
+++ b/mybatis-test/src/main/java/com/example/mybatistest/MybatisTestApplication.java
@@ -0,0 +1,14 @@
+package com.example.mybatistest;
+
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+@MapperScan("com.example.mybatistest.dao")
+public class MybatisTestApplication {
+
+    public static void main(String[] args) {
+        SpringApplication.run(MybatisTestApplication.class, args);
+    }
+}
diff --git a/mybatis-test/src/main/java/com/example/mybatistest/controller/UserController.java b/mybatis-test/src/main/java/com/example/mybatistest/controller/UserController.java
new file mode 100644
index 0000000..6d3b194
--- /dev/null
+++ b/mybatis-test/src/main/java/com/example/mybatistest/controller/UserController.java
@@ -0,0 +1,45 @@
+package com.example.mybatistest.controller;
+
+import com.example.mybatistest.entity.User;
+import com.example.mybatistest.service.UserService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+/**
+ * Created with IntelliJ IDEA.
+ * Description:
+ * User: ${fxb}
+ * Email: fanxb.tl@gmail.com
+ * Date: 2018-07-30
+ */
+@RestController
+public class UserController {
+
+    @Autowired
+    private UserService userService;
+
+    @GetMapping("/user/{userId}")
+    public User getUser(@PathVariable String userId){
+        return userService.getByUserId(userId);
+    }
+
+    @GetMapping("/user")
+    public List<User> getAll(){
+        return userService.getAll();
+    }
+
+    @GetMapping("/user/page/{pageNum}")
+    public Object getPage(@PathVariable int pageNum,
+                          @RequestParam(name = "pageSize",required = false,defaultValue = "10") int pageSize){
+        return userService.getAll(pageNum,pageSize);
+    }
+
+    @PostMapping("/user")
+    public Object addOne(User user){
+        userService.insert(user);
+        return user;
+    }
+
+}
diff --git a/mybatis-test/src/main/java/com/example/mybatistest/dao/UserDao.java b/mybatis-test/src/main/java/com/example/mybatistest/dao/UserDao.java
new file mode 100644
index 0000000..d06ad5b
--- /dev/null
+++ b/mybatis-test/src/main/java/com/example/mybatistest/dao/UserDao.java
@@ -0,0 +1,28 @@
+package com.example.mybatistest.dao;
+
+import com.example.mybatistest.entity.User;
+import org.apache.ibatis.annotations.Insert;
+import org.apache.ibatis.annotations.Options;
+import org.apache.ibatis.annotations.Select;
+
+import java.util.List;
+
+/**
+ * Created with IntelliJ IDEA.
+ * Description:
+ * User: ${fxb}
+ * Email: fanxb.tl@gmail.com
+ * Date: 2018-07-30
+ */
+public interface UserDao {
+    //插入用户
+    @Insert("insert into user(name,age,password) value(#{name},#{age},#{password})")
+    @Options(useGeneratedKeys=true,keyColumn="id",keyProperty="id")
+    int insert(User user);
+    //根据id查询
+    @Select("select * from user where id=#{id}")
+    User selectById(String id);
+    //查询所有
+    @Select("select * from user")
+    List<User> selectAll();
+}
diff --git a/mybatis-test/src/main/java/com/example/mybatistest/entity/User.java b/mybatis-test/src/main/java/com/example/mybatistest/entity/User.java
new file mode 100644
index 0000000..31516b8
--- /dev/null
+++ b/mybatis-test/src/main/java/com/example/mybatistest/entity/User.java
@@ -0,0 +1,55 @@
+package com.example.mybatistest.entity;
+
+/**
+ * Created with IntelliJ IDEA.
+ * Description:
+ * User: ${fxb}
+ * Email: fanxb.tl@gmail.com
+ * Date: 2018-07-30
+ */
+public class User {
+    private int id;
+    private String name;
+    private int age;
+    private String password;
+
+    public User(int id, String name, int age, String password) {
+        this.id = id;
+        this.name = name;
+        this.age = age;
+        this.password = password;
+    }
+    public User(){}
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public int getAge() {
+        return age;
+    }
+
+    public void setAge(int age) {
+        this.age = age;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+}
diff --git a/mybatis-test/src/main/java/com/example/mybatistest/service/UserService.java b/mybatis-test/src/main/java/com/example/mybatistest/service/UserService.java
new file mode 100644
index 0000000..1eb03b7
--- /dev/null
+++ b/mybatis-test/src/main/java/com/example/mybatistest/service/UserService.java
@@ -0,0 +1,45 @@
+package com.example.mybatistest.service;
+
+import com.example.mybatistest.dao.UserDao;
+import com.example.mybatistest.entity.User;
+import com.github.pagehelper.PageHelper;
+import com.github.pagehelper.PageInfo;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+
+/**
+ * Created with IntelliJ IDEA.
+ * Description:
+ * User: ${fxb}
+ * Email: fanxb.tl@gmail.com
+ * Date: 2018-07-30
+ */
+@Component
+public class UserService {
+
+    @Autowired
+    private UserDao userDao;
+
+    public User getByUserId(String id){
+        return userDao.selectById(id);
+    }
+    //获取全部用户
+    public List<User> getAll(){
+        return userDao.selectAll();
+    }
+    //测试分页
+    public PageInfo<User> getAll(int pageNum,int pageSize){
+        PageHelper.startPage(pageNum,pageSize);
+        List<User> users = userDao.selectAll();
+        System.out.println(users.size());
+        PageInfo<User> result = new PageInfo<>(users);
+        return result;
+    }
+
+    public int insert(User user){
+        return userDao.insert(user);
+    }
+
+}
diff --git a/mybatis-test/src/main/resources/application.yml b/mybatis-test/src/main/resources/application.yml
new file mode 100644
index 0000000..878acdf
--- /dev/null
+++ b/mybatis-test/src/main/resources/application.yml
@@ -0,0 +1,46 @@
+mybatis:
+  #对应实体类路径
+  type-aliases-package: com.example.mybatistest.entity
+  #对应mapper映射文件路径
+#  mapper-locations: classpath:mapper/*.xml
+
+#pagehelper配置
+pagehelper:
+  helper-dialect: mysql
+  reasonable: true
+  support-methods-arguments: true
+  params: count=countSql
+  returnPageInfo: check
+
+server:
+  port: 8081
+
+spring:
+  datasource:
+    name: mysqlTest
+    type: com.alibaba.druid.pool.DruidDataSource
+    #druid相关配置
+    druid:
+      #监控拦截统计的filters
+      filters: stat
+      driver-class-name: com.mysql.jdbc.Driver
+      url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true
+      username: root
+      password: 123456
+      #配置初始化大小,最小,最大
+      initial-size: 1
+      min-idle: 1
+      max-active: 20
+      #获取连接等待超时时间
+      max-wait: 6000
+      #间隔多久检测一次需要关闭的空闲连接
+      time-between-eviction-runs-millis: 60000
+      #一个连接在池中的最小生存时间
+      min-evictable-idle-time-millis: 300000
+      #打开PSCache,并指定每个连接上PSCache的大小。oracle设置为true,mysql设置为false。分库分表设置较多推荐设置
+      pool-prepared-statements: false
+      max-pool-prepared-statement-per-connection-size: 20
+  http:
+    encoding:
+      charset: utf-8
+      enabled: true
diff --git a/mybatis-test/src/main/resources/mapper/UserMapper.xml b/mybatis-test/src/main/resources/mapper/UserMapper.xml
new file mode 100644
index 0000000..08bf682
--- /dev/null
+++ b/mybatis-test/src/main/resources/mapper/UserMapper.xml
@@ -0,0 +1,41 @@
+<?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.example.mybatistest.dao.UserDao">
+    <sql id="BASE_TABLE">
+        user
+    </sql>
+    <sql id="BASE_COLUMN">
+        id,name,age,password
+    </sql>
+    
+    <insert id="insert" parameterType="com.example.mybatistest.entity.User" useGeneratedKeys="true" keyProperty="id">
+        INSERT INTO <include refid="BASE_TABLE"/>
+        <trim prefix="(" suffix=")" suffixOverrides=",">
+            name,password,
+            <if test="age!=null">
+                age
+            </if>
+        </trim>
+        <trim prefix=" VALUE(" suffix=")" suffixOverrides=",">
+            #{name,jdbcType=VARCHAR},#{password},
+            <if test="age!=null">
+                #{age}
+            </if>
+        </trim>
+    </insert>
+
+    <select id="selectById" resultType="com.example.mybatistest.entity.User">
+        select
+          <include refid="BASE_COLUMN"/>
+        from
+          <include refid="BASE_TABLE"/>
+        where id=#{id}
+    </select>
+
+    <select id="selectAll" resultType="com.example.mybatistest.entity.User">
+        select
+          <include refid="BASE_COLUMN"/>
+        from
+          <include refid="BASE_TABLE"/>
+    </select>
+</mapper>
\ No newline at end of file
diff --git a/mybatis-test/src/test/java/com/example/mybatistest/MybatisTestApplicationTests.java b/mybatis-test/src/test/java/com/example/mybatistest/MybatisTestApplicationTests.java
new file mode 100644
index 0000000..abe689d
--- /dev/null
+++ b/mybatis-test/src/test/java/com/example/mybatistest/MybatisTestApplicationTests.java
@@ -0,0 +1,16 @@
+package com.example.mybatistest;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class MybatisTestApplicationTests {
+
+    @Test
+    public void contextLoads() {
+    }
+
+}