From a1cdb3c9b1aee4ec26a523eb6b28d8fa4ee5620f Mon Sep 17 00:00:00 2001
From: mrbird <852252810@qq.com>
Date: Tue, 21 Aug 2018 15:53:20 +0800
Subject: [PATCH] =?UTF-8?q?Spring=20Boot=E6=95=B4=E5=90=88MyBatis=E9=80=9A?=
=?UTF-8?q?=E7=94=A8Mapper=E5=92=8CPageHelper?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
27.Spring-Boot-Mapper-PageHelper/pom.xml | 115 ++++++++++++++++++
.../main/java/com/springboot/Application.java | 15 +++
.../java/com/springboot/ApplicationTest.java | 67 ++++++++++
.../main/java/com/springboot/bean/User.java | 96 +++++++++++++++
.../java/com/springboot/config/MyMapper.java | 8 ++
.../com/springboot/mapper/SeqenceMapper.java | 9 ++
.../com/springboot/mapper/UserMapper.java | 7 ++
.../java/com/springboot/service/IService.java | 27 ++++
.../com/springboot/service/UserService.java | 7 ++
.../springboot/service/impl/BaseService.java | 70 +++++++++++
.../service/impl/UserServiceImpl.java | 12 ++
.../src/main/resources/application.yml | 92 ++++++++++++++
.../src/main/resources/mapper/UserMapper.xml | 14 +++
.../src/main/resources/mybatis-generator.xml | 57 +++++++++
14 files changed, 596 insertions(+)
create mode 100644 27.Spring-Boot-Mapper-PageHelper/pom.xml
create mode 100644 27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/Application.java
create mode 100644 27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/ApplicationTest.java
create mode 100644 27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/bean/User.java
create mode 100644 27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/config/MyMapper.java
create mode 100644 27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/mapper/SeqenceMapper.java
create mode 100644 27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/mapper/UserMapper.java
create mode 100644 27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/service/IService.java
create mode 100644 27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/service/UserService.java
create mode 100644 27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/service/impl/BaseService.java
create mode 100644 27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/service/impl/UserServiceImpl.java
create mode 100644 27.Spring-Boot-Mapper-PageHelper/src/main/resources/application.yml
create mode 100644 27.Spring-Boot-Mapper-PageHelper/src/main/resources/mapper/UserMapper.xml
create mode 100644 27.Spring-Boot-Mapper-PageHelper/src/main/resources/mybatis-generator.xml
diff --git a/27.Spring-Boot-Mapper-PageHelper/pom.xml b/27.Spring-Boot-Mapper-PageHelper/pom.xml
new file mode 100644
index 0000000..607a178
--- /dev/null
+++ b/27.Spring-Boot-Mapper-PageHelper/pom.xml
@@ -0,0 +1,115 @@
+
+
+ 4.0.0
+
+ com.springboot
+ Spring-Boot-Mapper-PageHelper
+ 0.0.1-SNAPSHOT
+ jar
+
+ Spring-Boot-Mapper-PageHelper
+ Demo project for Spring Boot
+
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 1.5.9.RELEASE
+
+
+
+
+ UTF-8
+ UTF-8
+ 1.7
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+
+
+
+
+ org.mybatis.spring.boot
+ mybatis-spring-boot-starter
+ 1.3.1
+
+
+
+ tk.mybatis
+ mapper-spring-boot-starter
+ 1.1.5
+
+
+
+ com.github.pagehelper
+ pagehelper-spring-boot-starter
+ 1.2.3
+
+
+
+ com.oracle
+ ojdbc6
+ 6.0
+
+
+ com.alibaba
+ druid-spring-boot-starter
+ 1.1.6
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+ org.mybatis.generator
+ mybatis-generator-maven-plugin
+ 1.3.5
+
+
+ com.oracle
+ ojdbc6
+ 6.0
+
+
+ tk.mybatis
+ mapper
+ 3.4.0
+
+
+
+
+ Generate MyBatis Artifacts
+ package
+
+ generate
+
+
+
+
+
+ true
+
+ true
+
+ src/main/resources/mybatis-generator.xml
+
+
+
+
+
+
+
diff --git a/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/Application.java b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/Application.java
new file mode 100644
index 0000000..449e3c6
--- /dev/null
+++ b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/Application.java
@@ -0,0 +1,15 @@
+package com.springboot;
+
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+
+@SpringBootApplication
+@MapperScan("com.springboot.mapper")
+public class Application {
+
+ public static void main(String[] args) {
+ SpringApplication.run(Application.class,args);
+ }
+}
diff --git a/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/ApplicationTest.java b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/ApplicationTest.java
new file mode 100644
index 0000000..88b498b
--- /dev/null
+++ b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/ApplicationTest.java
@@ -0,0 +1,67 @@
+package com.springboot;
+
+import java.util.Date;
+import java.util.List;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import com.github.pagehelper.PageHelper;
+import com.github.pagehelper.PageInfo;
+import com.springboot.bean.User;
+import com.springboot.service.UserService;
+
+import tk.mybatis.mapper.entity.Example;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest(classes = Application.class)
+public class ApplicationTest {
+
+ @Autowired
+ private UserService userService;
+
+ @Test
+ public void test() throws Exception {
+
+ // User user = new User();
+ // user.setId(userService.getSequence("seq_user"));
+ // user.setUsername("scott");
+ // user.setPasswd("ac089b11709f9b9e9980e7c497268dfa");
+ // user.setCreateTime(new Date());
+ // user.setStatus("0");
+ // this.userService.save(user);
+
+
+// Example example = new Example(User.class);
+// example.createCriteria().andCondition("username like '%i%'");
+// example.setOrderByClause("id desc");
+// List userList = this.userService.selectByExample(example);
+// for (User u : userList) {
+// System.out.println(u.getUsername());
+// }
+//
+// List all = this.userService.selectAll();
+// for (User u : all) {
+// System.out.println(u.getUsername());
+// }
+//
+// User user = new User();
+// user.setId(1l);
+// user = this.userService.selectByKey(user);
+// System.out.println(user.getUsername());
+//
+// user.setId(4l);
+// this.userService.delete(user);
+
+ PageHelper.startPage(2, 2);
+ List list = userService.selectAll();
+ PageInfo pageInfo = new PageInfo(list);
+ List result = pageInfo.getList();
+ for (User u : result) {
+ System.out.println(u.getUsername());
+ }
+ }
+}
diff --git a/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/bean/User.java b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/bean/User.java
new file mode 100644
index 0000000..030bc2a
--- /dev/null
+++ b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/bean/User.java
@@ -0,0 +1,96 @@
+package com.springboot.bean;
+
+import java.util.Date;
+
+import javax.persistence.Column;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Table(name = "T_USER")
+public class User {
+ @Id
+ @Column(name = "ID")
+ private Long id;
+
+ @Column(name = "USERNAME")
+ private String username;
+
+ @Column(name = "PASSWD")
+ private String passwd;
+
+ @Column(name = "CREATE_TIME")
+ private Date createTime;
+
+ @Column(name = "STATUS")
+ private String status;
+
+ /**
+ * @return ID
+ */
+ public Long getId() {
+ return id;
+ }
+
+ /**
+ * @param id
+ */
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ /**
+ * @return USERNAME
+ */
+ public String getUsername() {
+ return username;
+ }
+
+ /**
+ * @param username
+ */
+ public void setUsername(String username) {
+ this.username = username == null ? null : username.trim();
+ }
+
+ /**
+ * @return PASSWD
+ */
+ public String getPasswd() {
+ return passwd;
+ }
+
+ /**
+ * @param passwd
+ */
+ public void setPasswd(String passwd) {
+ this.passwd = passwd == null ? null : passwd.trim();
+ }
+
+ /**
+ * @return CREATE_TIME
+ */
+ public Date getCreateTime() {
+ return createTime;
+ }
+
+ /**
+ * @param createTime
+ */
+ public void setCreateTime(Date createTime) {
+ this.createTime = createTime;
+ }
+
+ /**
+ * @return STATUS
+ */
+ public String getStatus() {
+ return status;
+ }
+
+ /**
+ * @param status
+ */
+ public void setStatus(String status) {
+ this.status = status == null ? null : status.trim();
+ }
+}
\ No newline at end of file
diff --git a/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/config/MyMapper.java b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/config/MyMapper.java
new file mode 100644
index 0000000..88edb47
--- /dev/null
+++ b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/config/MyMapper.java
@@ -0,0 +1,8 @@
+package com.springboot.config;
+
+import tk.mybatis.mapper.common.Mapper;
+import tk.mybatis.mapper.common.MySqlMapper;
+
+public interface MyMapper extends Mapper, MySqlMapper {
+
+}
diff --git a/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/mapper/SeqenceMapper.java b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/mapper/SeqenceMapper.java
new file mode 100644
index 0000000..4affa31
--- /dev/null
+++ b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/mapper/SeqenceMapper.java
@@ -0,0 +1,9 @@
+package com.springboot.mapper;
+
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
+
+public interface SeqenceMapper {
+ @Select("select ${seqName}.nextval from dual")
+ Long getSequence(@Param("seqName") String seqName);
+}
diff --git a/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/mapper/UserMapper.java b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/mapper/UserMapper.java
new file mode 100644
index 0000000..217dc84
--- /dev/null
+++ b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/mapper/UserMapper.java
@@ -0,0 +1,7 @@
+package com.springboot.mapper;
+
+import com.springboot.bean.User;
+import com.springboot.config.MyMapper;
+
+public interface UserMapper extends MyMapper {
+}
\ No newline at end of file
diff --git a/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/service/IService.java b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/service/IService.java
new file mode 100644
index 0000000..38e53b5
--- /dev/null
+++ b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/service/IService.java
@@ -0,0 +1,27 @@
+package com.springboot.service;
+
+import java.util.List;
+
+import org.apache.ibatis.annotations.Param;
+import org.springframework.stereotype.Service;
+
+@Service
+public interface IService {
+
+ Long getSequence(@Param("seqName") String seqName);
+
+ List selectAll();
+
+ T selectByKey(Object key);
+
+ int save(T entity);
+
+ int delete(Object key);
+
+ int updateAll(T entity);
+
+ int updateNotNull(T entity);
+
+ List selectByExample(Object example);
+
+}
diff --git a/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/service/UserService.java b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/service/UserService.java
new file mode 100644
index 0000000..58a3a29
--- /dev/null
+++ b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/service/UserService.java
@@ -0,0 +1,7 @@
+package com.springboot.service;
+
+import com.springboot.bean.User;
+
+public interface UserService extends IService{
+
+}
diff --git a/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/service/impl/BaseService.java b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/service/impl/BaseService.java
new file mode 100644
index 0000000..448033e
--- /dev/null
+++ b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/service/impl/BaseService.java
@@ -0,0 +1,70 @@
+package com.springboot.service.impl;
+
+import java.util.List;
+
+import org.apache.ibatis.annotations.Param;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import com.springboot.mapper.SeqenceMapper;
+import com.springboot.service.IService;
+
+import tk.mybatis.mapper.common.Mapper;
+
+public abstract class BaseService implements IService {
+
+ @Autowired
+ protected Mapper mapper;
+ @Autowired
+ protected SeqenceMapper seqenceMapper;
+
+ public Mapper getMapper() {
+ return mapper;
+ }
+ @Override
+ public Long getSequence(@Param("seqName") String seqName){
+ return seqenceMapper.getSequence(seqName);
+ }
+
+ @Override
+ public List selectAll() {
+ //说明:查询所有数据
+ return mapper.selectAll();
+ }
+
+ @Override
+ public T selectByKey(Object key) {
+ //说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号
+ return mapper.selectByPrimaryKey(key);
+ }
+
+ @Override
+ public int save(T entity) {
+ //说明:保存一个实体,null的属性也会保存,不会使用数据库默认值
+ return mapper.insert(entity);
+ }
+
+ @Override
+ public int delete(Object key) {
+ //说明:根据主键字段进行删除,方法参数必须包含完整的主键属性
+ return mapper.deleteByPrimaryKey(key);
+ }
+
+ @Override
+ public int updateAll(T entity) {
+ //说明:根据主键更新实体全部字段,null值会被更新
+ return mapper.updateByPrimaryKey(entity);
+ }
+
+ @Override
+ public int updateNotNull(T entity) {
+ //根据主键更新属性不为null的值
+ return mapper.updateByPrimaryKeySelective(entity);
+ }
+
+ @Override
+ public List selectByExample(Object example) {
+ //说明:根据Example条件进行查询
+ //重点:这个查询支持通过Example类指定查询列,通过selectProperties方法指定查询列
+ return mapper.selectByExample(example);
+ }
+}
diff --git a/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/service/impl/UserServiceImpl.java b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/service/impl/UserServiceImpl.java
new file mode 100644
index 0000000..fea6e75
--- /dev/null
+++ b/27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/service/impl/UserServiceImpl.java
@@ -0,0 +1,12 @@
+package com.springboot.service.impl;
+
+import org.springframework.stereotype.Repository;
+
+import com.springboot.bean.User;
+import com.springboot.service.UserService;
+
+@Repository("userService")
+public class UserServiceImpl extends BaseService implements UserService{
+
+
+}
diff --git a/27.Spring-Boot-Mapper-PageHelper/src/main/resources/application.yml b/27.Spring-Boot-Mapper-PageHelper/src/main/resources/application.yml
new file mode 100644
index 0000000..c6971f5
--- /dev/null
+++ b/27.Spring-Boot-Mapper-PageHelper/src/main/resources/application.yml
@@ -0,0 +1,92 @@
+server:
+ context-path: /web
+
+spring:
+ datasource:
+ druid:
+ # 数据库访问配置, 使用druid数据源
+ type: com.alibaba.druid.pool.DruidDataSource
+ driver-class-name: oracle.jdbc.driver.OracleDriver
+ url: jdbc:oracle:thin:@localhost:1521:ORCL
+ username: scott
+ password: 6742530
+ # 连接池配置
+ 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: druid123
+ # IP白名单
+ # allow: 127.0.0.1
+ # IP黑名单(共同存在时,deny优先于allow)
+ # deny: 192.168.1.218
+
+ # 配置StatFilter
+ filter:
+ stat:
+ log-slow-sql: true
+
+mybatis:
+ # type-aliases扫描路径
+ type-aliases-package: com.springboot.bean
+ # mapper xml实现扫描路径
+ mapper-locations: classpath:mapper/*.xml
+ property:
+ order: BEFORE
+
+
+#mappers 多个接口时逗号隔开
+mapper:
+ mappers: com.springboot.config.MyMapper
+ not-empty: false
+ identity: oracle
+
+#pagehelper
+pagehelper:
+ helperDialect: oracle
+ reasonable: true
+ supportMethodsArguments: true
+ params: count=countSql
+
+
+logging:
+ level:
+ com:
+ springboot:
+ mapper: debug
\ No newline at end of file
diff --git a/27.Spring-Boot-Mapper-PageHelper/src/main/resources/mapper/UserMapper.xml b/27.Spring-Boot-Mapper-PageHelper/src/main/resources/mapper/UserMapper.xml
new file mode 100644
index 0000000..2000afd
--- /dev/null
+++ b/27.Spring-Boot-Mapper-PageHelper/src/main/resources/mapper/UserMapper.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/27.Spring-Boot-Mapper-PageHelper/src/main/resources/mybatis-generator.xml b/27.Spring-Boot-Mapper-PageHelper/src/main/resources/mybatis-generator.xml
new file mode 100644
index 0000000..04cec3d
--- /dev/null
+++ b/27.Spring-Boot-Mapper-PageHelper/src/main/resources/mybatis-generator.xml
@@ -0,0 +1,57 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file