Skip to content

Commit

Permalink
Spring Boot整合MyBatis通用Mapper和PageHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyouzhuguli committed Aug 21, 2018
1 parent 6de47bb commit a1cdb3c
Show file tree
Hide file tree
Showing 14 changed files with 596 additions and 0 deletions.
115 changes: 115 additions & 0 deletions 27.Spring-Boot-Mapper-PageHelper/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?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.springboot</groupId>
<artifactId>Spring-Boot-Mapper-PageHelper</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Spring-Boot-Mapper-PageHelper</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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.7</java.version>
</properties>

<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>
</dependency>

<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--通用mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.5</version>
</dependency>
<!--pagehelper 分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>

<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>6.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.6</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>6.0</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<!--允许移动生成的文件 -->
<verbose>true</verbose>
<!-- 是否覆盖 -->
<overwrite>true</overwrite>
<!-- 自动生成的配置 -->
<configurationFile>src/main/resources/mybatis-generator.xml</configurationFile>
</configuration>
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<User> userList = this.userService.selectByExample(example);
// for (User u : userList) {
// System.out.println(u.getUsername());
// }
//
// List<User> 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<User> list = userService.selectAll();
PageInfo<User> pageInfo = new PageInfo<User>(list);
List<User> result = pageInfo.getList();
for (User u : result) {
System.out.println(u.getUsername());
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.springboot.config;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {

}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.springboot.mapper;

import com.springboot.bean.User;
import com.springboot.config.MyMapper;

public interface UserMapper extends MyMapper<User> {
}
Original file line number Diff line number Diff line change
@@ -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<T> {

Long getSequence(@Param("seqName") String seqName);

List<T> selectAll();

T selectByKey(Object key);

int save(T entity);

int delete(Object key);

int updateAll(T entity);

int updateNotNull(T entity);

List<T> selectByExample(Object example);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.springboot.service;

import com.springboot.bean.User;

public interface UserService extends IService<User>{

}
Loading

0 comments on commit a1cdb3c

Please sign in to comment.