Spring Boot整合MyBatis通用Mapper和PageHelper
This commit is contained in:
parent
6de47bbad4
commit
a1cdb3c9b1
|
|
@ -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>
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -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> {
|
||||
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package com.springboot.mapper;
|
||||
|
||||
import com.springboot.bean.User;
|
||||
import com.springboot.config.MyMapper;
|
||||
|
||||
public interface UserMapper extends MyMapper<User> {
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package com.springboot.service;
|
||||
|
||||
import com.springboot.bean.User;
|
||||
|
||||
public interface UserService extends IService<User>{
|
||||
|
||||
}
|
||||
|
|
@ -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<T> implements IService<T> {
|
||||
|
||||
@Autowired
|
||||
protected Mapper<T> mapper;
|
||||
@Autowired
|
||||
protected SeqenceMapper seqenceMapper;
|
||||
|
||||
public Mapper<T> getMapper() {
|
||||
return mapper;
|
||||
}
|
||||
@Override
|
||||
public Long getSequence(@Param("seqName") String seqName){
|
||||
return seqenceMapper.getSequence(seqName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> 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<T> selectByExample(Object example) {
|
||||
//说明:根据Example条件进行查询
|
||||
//重点:这个查询支持通过Example类指定查询列,通过selectProperties方法指定查询列
|
||||
return mapper.selectByExample(example);
|
||||
}
|
||||
}
|
||||
|
|
@ -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<User> implements UserService{
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
<?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.springboot.mapper.UserMapper">
|
||||
<resultMap id="BaseResultMap" type="com.springboot.bean.User">
|
||||
<!--
|
||||
WARNING - @mbg.generated
|
||||
-->
|
||||
<id column="ID" jdbcType="DECIMAL" property="id" />
|
||||
<result column="USERNAME" jdbcType="VARCHAR" property="username" />
|
||||
<result column="PASSWD" jdbcType="VARCHAR" property="passwd" />
|
||||
<result column="CREATE_TIME" jdbcType="TIMESTAMP" property="createTime" />
|
||||
<result column="STATUS" jdbcType="CHAR" property="status" />
|
||||
</resultMap>
|
||||
</mapper>
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE generatorConfiguration
|
||||
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
|
||||
<generatorConfiguration>
|
||||
|
||||
<context id="oracle" targetRuntime="MyBatis3Simple" defaultModelType="flat">
|
||||
|
||||
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
|
||||
<property name="mappers" value="com.springboot.config.MyMapper" />
|
||||
<!--caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true-->
|
||||
<property name="caseSensitive" value="false"/>
|
||||
</plugin>
|
||||
|
||||
<!-- 阻止生成自动注释 -->
|
||||
<commentGenerator>
|
||||
<property name="javaFileEncoding" value="UTF-8"/>
|
||||
<property name="suppressDate" value="true"/>
|
||||
<property name="suppressAllComments" value="true"/>
|
||||
</commentGenerator>
|
||||
|
||||
<!--数据库链接地址账号密码-->
|
||||
<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
|
||||
connectionURL="jdbc:oracle:thin:@localhost:1521:ORCL"
|
||||
userId="scott"
|
||||
password="6742530">
|
||||
</jdbcConnection>
|
||||
|
||||
<javaTypeResolver>
|
||||
<property name="forceBigDecimals" value="false"/>
|
||||
</javaTypeResolver>
|
||||
|
||||
<!--生成Model类存放位置-->
|
||||
<javaModelGenerator targetPackage="com.springboot.bean" targetProject="src/main/java">
|
||||
<property name="enableSubPackages" value="true"/>
|
||||
<property name="trimStrings" value="true"/>
|
||||
</javaModelGenerator>
|
||||
|
||||
<!--生成映射文件存放位置-->
|
||||
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
|
||||
<property name="enableSubPackages" value="true"/>
|
||||
</sqlMapGenerator>
|
||||
|
||||
<!--生成Dao类存放位置-->
|
||||
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
|
||||
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
|
||||
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
|
||||
<javaClientGenerator type="XMLMAPPER" targetPackage="com.springboot.mapper" targetProject="src/main/java">
|
||||
<property name="enableSubPackages" value="true"/>
|
||||
</javaClientGenerator>
|
||||
|
||||
<!--生成对应表及类名去掉Mybatis Generator生成的一堆 example-->
|
||||
<table tableName="T_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
|
||||
<generatedKey column="id" sqlStatement="oralce" identity="true"/>
|
||||
</table>
|
||||
</context>
|
||||
</generatorConfiguration>
|
||||
Loading…
Reference in New Issue