集成mybaties【JWordpres前台项目实战】

写在前面

本项目采用mysql,数据层框架用mybatis,基于tk.mybatis包实现基础的mapper,具体详见项目工程实例java

代码

pom.xml引入相关的包mysql

<!–mapper –>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>git

<!–mybatis –>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>web

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>spring

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>sql

mapper配置springboot

mapper.identity=MYSQL
mapper.mappers[0]=cn.liuhaihua.web.mapper.BaseMapper
mapper.not-empty=false
mybatis.type-aliases-package=cn.liuhaihua.web.modelmybatis

配置扫描包app

12

通用的mapper框架

/**
* MIT License
* Copyright (c) 2018 haihua.liu
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package cn.liuhaihua.web.mapper;

import tk.mybatis.mapper.common.ConditionMapper;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
/**
*
* @ClassName: BaseMapper
* @Description: 通用的mapper
* @author Liuhaihua
* @date 2018年6月27日
*
* @param <T>
*/
public interface BaseMapper<T> extends Mapper<T>, MySqlMapper<T>,ConditionMapper<T> {

}

测试

/**
* MIT License
* Copyright (c) 2018 haihua.liu
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package cn.liuhaihua.web;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import cn.liuhaihua.web.model.WpUsers;
import cn.liuhaihua.web.service.WpUsersService;

/**
* @ClassName: WpUsersTest
* @Description: 用户测试类
* @author Liuhaihua
* @date 2018年6月29日
*
*/
public class WpUsersTest extends BaseTest{
@Autowired
private WpUsersService wpUsersService;
/**
* @Title: getUserDetail
* @Description: 测试获取用户信息方法
* @param 参数
* @return void 返回类型
* @throws
*/
@Test
public void getUserDetail() {
WpUsers user =wpUsersService.getUserDetail(1l);
Assert.assertEquals(“Harries”, user.getDisplayName());
}
}

运行junit测试用例

1530614118(1)

实战项目介绍

项目介绍

为了知足Java新手朋友课程要求,我特出此教程,因为时间仓促的问题,代码写得很差之处的地方还请多多包涵。

目标以下

  1. 优化wordpress效率低下的问题(目前博主文章数量大概10万+)
  2. 让群里面初级Java朋友们更快上手springboot应用

GIT地址:gitee.com/jxuasea/JWo…

相关文章
相关标签/搜索