第一步:引入mybatis依赖

            <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>

第二步:类目表实体类

package com.payease.dataobject;

import lombok.Data;
import org.hibernate.annotations.DynamicUpdate; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; /**
* 类目
* Created by liuxiaoming
* 2017-11-09
*/
@Entity
@DynamicUpdate //动态时间修改
@Data // get set toString 登方法
public class ProductCategory { /** 类目id. */
@Id
@GeneratedValue
private Integer categoryId; /** 类目名字. */
private String categoryName; /** 类目编号. */
private Integer categoryType; // private Date createTime;
//
// private Date updateTime; public ProductCategory() {
} public ProductCategory(String categoryName, Integer categoryType) {
this.categoryName = categoryName;
this.categoryType = categoryType;
}
}

第三步:编写相应的mapper文件

package com.payease.dataobject.mapper;

import com.payease.dataobject.ProductCategory;
import org.apache.ibatis.annotations.*; import java.util.List;
import java.util.Map; /**
* @Created By liuxiaoming
* @CreateTime 2017/12/12 下午6:13
**/ public interface ProductCategoryMapper { /**
* 通过参数为map保存
* @param map
* @return
*/
@Insert("insert into product_category(category_name, category_type) values (#{category_name , jdbcType=VARCHAR}, #{category_type, jdbcType=INTEGER})")
int insertByMap(Map<String, Object> map); /**
* 通过参数为对象保存
* @param productCategory
* @return
*/
@Insert("insert into product_category(category_name, category_type) values (#{categoryName , jdbcType=VARCHAR}, #{categoryType, jdbcType=INTEGER})")
int insertByObject(ProductCategory productCategory); /**
* 查单一数据
* 通过categoryType查询product_category表 @Result注解设置返回值
* @param categoryType
* @return
*/
@Select("select * from product_category where category_type = #{categoryType}")
@Results({
@Result(column = "category_id", property = "categoryId"),
@Result(column = "category_name", property = "categoryName"),
@Result(column = "category_type", property = "categoryType")
})
ProductCategory findByCategoryType(Integer categoryType); /**
* 查集合
* 通过categoryName查询product_category表 @Result注解设置返回值
* @param categoryName
* @return
*/
@Select("select * from product_category where category_name = #{categoryName}")
@Results({
@Result(column = "category_id", property = "categoryId"),
@Result(column = "category_name", property = "categoryName"),
@Result(column = "category_type", property = "categoryType")
})
List<ProductCategory> findByCategoryName(String categoryName); /**
* 根据某个字段更新
* 通过查询category_type 来修改 category_name
* @param categoryName
* @param categoryType
* @return
*/
@Update("update product_category set category_name = #{categoryName} where category_type = #{categoryType}")
int updateByCategoryType(@Param("categoryName") String categoryName,
@Param("categoryType") Integer categoryType); /**
* 根据对象更新
* 通过查询category_type 来修改 category_name
* @param productCategory
* @return
*/
@Update("update product_category set category_name = #{categorName} where category_type = #{categoryType}")
int updateByObject(ProductCategory productCategory); /**
* 根据某个字段来删除数据
* 通过category_type 来删除数据
* @param categoryType
* @return
*/
@Delete("delete from product_category where category_type = #{categoryType}")
int deleteByCategoryType(Integer categoryType); /**
* mybatis xml的使用样例
* 通过categoryType 查询数据
* @param categoryType
* @return
*/
ProductCategory selectByCategoryType(Integer categoryType);
}

第四步:测试类的编写

package com.payease.dataobject.mapper;

import com.payease.dataobject.ProductCategory;
import org.junit.Assert;
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.SpringRunner; import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* @Created By liuxiaoming
* @CreateTime 2017/12/12 下午6:19
**/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryMapperTest { @Autowired
private ProductCategoryMapper mapper; @Test
public void insertByMap() throws Exception {
Map<String, Object> map = new HashMap<>();
map.put("category_name","吃鸡专属");
map.put("category_type",103);
int result = mapper.insertByMap(map);
Assert.assertEquals(1,result);
} @Test
public void insertByObject() throws Exception {
ProductCategory productCategory = new ProductCategory();
productCategory.setCategoryName("大吉大利");
productCategory.setCategoryType(102);
int result = mapper.insertByObject(productCategory);
Assert.assertEquals(1,result);
} @Test
public void findByCategoryType() throws Exception{
ProductCategory result = mapper.findByCategoryType(102);
Assert.assertNotNull(result);
} @Test
public void findByCategoryName() throws Exception{
List<ProductCategory> result = mapper.findByCategoryName("吃鸡专属");
Assert.assertNotEquals(0,result.size());
} @Test
public void updateByCategoryType(){
int result = mapper.updateByCategoryType("绝地求生", 103);
Assert.assertEquals(1, result);
} @Test
public void updateByObject(){
ProductCategory productCategory = new ProductCategory();
productCategory.setCategoryName("今晚吃鸡|大吉大利");
productCategory.setCategoryType(102);
int result = mapper.updateByObject(productCategory);
Assert.assertEquals(1, result);
} @Test
public void deleteByCategoryType(){
int result = mapper.deleteByCategoryType(102);
Assert.assertEquals(1, result);
} @Test
public void selectByCategoryType(){
ProductCategory result = mapper.selectByCategoryType(101);
Assert.assertNotNull(result);
} }

第五步:启动类上加入mapper扫描注解

package com.payease;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan(basePackages = "com.payease.dataobject.mapper")
public class SellApplication { public static void main(String[] args) {
SpringApplication.run(SellApplication.class, args);
}
}

第六步:对于mybatis xml文件的使用需要

1.在 resource/mapper文件夹下创建相应的xml文件

<?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.payease.dataobject.mapper.ProductCategoryMapper" >
<resultMap id="BaseResultMap" type="com.payease.dataobject.ProductCategory" >
<id column="category_id" property="categoryId" jdbcType="INTEGER" />
<result column="category_name" property="categoryName" jdbcType="VARCHAR" />
<result column="category_type" property="categoryType" jdbcType="INTEGER" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
</resultMap> <sql id="base_column" >
category_id,category_name,category_type
</sql>
<select id="selectByCategoryType" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select <include refid="base_column" />
from product_category
where category_type = #{category_type,jdbcType=INTEGER}
</select> </mapper>

2.在application.yml文件夹下配置xml文件的扫描

注1: mapper文件的使用 封装到dao层

package com.payease.dataobject.dao;

import com.payease.dataobject.mapper.ProductCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired; import java.util.Map; /**
* @Created By liuxiaoming
* @CreateTime 2017/12/13 下午3:23
**/
public class ProductCategoryDao { @Autowired
ProductCategoryMapper mapper; public int insertByMap(Map<String, Object> map){
return mapper.insertByMap(map);
}
}

注2:日志查看mapper文件中的SQL语句

这是application.yml文件的配置

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: liuxiaoming_123 #1234
url: jdbc:mysql://rm-uf6qe0894f7hv8977o.mysql.rds.aliyuncs.com/sell?characterEncoding=utf-8&useSSL=false
#url: jdbc:mysql://127.0.0.1/sell?characterEncoding=utf-8&useSSL=false
jpa:
show-sql: true
jackson:
default-property-inclusion: non_null
redis:
host: 192.168.1.183
port: 6379
server:
context-path: /sell
#logging:
# pattern:
# console: "%d - %msg%n" #日志格式 日期 - 信息 空格
# path: /Users/liuxiaoming/Documents/ideawork/sell_log #日志路径 默认名字spring.log
# file: /Users/liuxiaoming/Documents/ideawork/sell_log/sell.log #日志文件+路径
# level: #日志级别
# com.payease.LoggerTest: debug #日志级别指定某个类 也可以步制定类 直接在level: 后面配置 #日志查看SQL语句
logging:
level:
com.payease.dataobject.mapper: trace wechat:
mpAppId: wxd898fcb01713c658
mpAppSecret: 47ccc303338cee6e62894fxxxxxxxxxxx
openAppId: wx6ad144e54af67d87
openAppSecret: 91a2ff6d38a2bbccfb7e9f9079108e2e
mchId: 1483469312
mchKey: 06C56A89949D617xxxxxxxxxxx
keyPath: /var/weixin_cert/h5.p12
notifyUrl: http://sell.natapp4.cc/sell/pay/notify
templateId:
orderStatus: e-Cqq67QxD6YNI41iRiqawEYdFavW_7pc7LyEMb-yeQ #projectUrl:
# wechatMpAuthorize: http://sell.natapp4.cc
# wechatOpenAuthorize: http://sell.natapp4.cc
# sell: http://sell.natapp4.cc
projectUrl:
wechatMpAuthorize: http://127.0.0.1:8080
wechatOpenAuthorize: http://127.0.0.1:8080
sell: http://127.0.0.1:8080

springboot: mybatis的使用的更多相关文章

  1. 第五章 springboot + mybatis(转载)

    本编博客转发自:http://www.cnblogs.com/java-zhao/p/5350021.html springboot集成了springJDBC与JPA,但是没有集成mybatis,所以 ...

  2. 第九章 springboot + mybatis + 多数据源 (AOP实现)

    在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改 1.ShopDao package com.xxx.firstboot.dao; import org.spr ...

  3. 第五章 springboot + mybatis

    springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成.集成方式相当简单. 1.项目结构 2.pom.xml <!-- 与数 ...

  4. 基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建

    基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Sp ...

  5. Docker+SpringBoot+Mybatis+thymeleaf的Java博客系统开源啦

    个人博客 对于技术人员来说,拥有自己的个人博客应该是一件令人向往的事情,可以记录和分享自己的观点,想到这件事就觉得有意思,但是刚开始写博客的时候脑海中是没有搭建个人博客这一想法的,因为刚起步的时候连我 ...

  6. springboot mybatis 事务管理

    本文主要讲述springboot提供的声明式的事务管理机制. 一.一些概念 声明式的事务管理是基于AOP的,在springboot中可以通过@Transactional注解的方式获得支持,这种方式的优 ...

  7. SpringBoot+Mybatis+Freemark 最简单的例子

    springboot-sample 实现最简单的 SpringBoot + Mybatis + Freemarker 网页增删改查功能,适合新接触 Java 和 SpringBoot 的同学参考 代码 ...

  8. springboot + mybatis 前后端分离项目的搭建 适合在学习中的大学生

    人生如戏,戏子多半掉泪! 我是一名大四学生,刚进入一家软件件公司实习,虽说在大学中做过好多个实训项目,都是自己完成,没有组员的配合.但是在这一个月的实习中,我从以前别人教走到了现在的自学,成长很多. ...

  9. springboot+mybatis+redis实现分布式缓存

    大家都知道springboot项目都是微服务部署,A服务和B服务分开部署,那么它们如何更新或者获取共有模块的缓存数据,或者给A服务做分布式集群负载,如何确保A服务的所有集群都能同步公共模块的缓存数据, ...

  10. Java逆向工程SpringBoot + Mybatis Generator + MySQL

    Java逆向工程SpringBoot+ Mybatis Generator + MySQL Meven pop.xml文件添加引用: <dependency> <groupId> ...

随机推荐

  1. tab切换代码优化

    上次的tab切换的代码里面有很多重复的代码,需要做做优化,把重复的代码用函数封装起来调用. 优化前: <script> //获取id封装成一个函数$()方便调用 function $(id ...

  2. HDU 3157 Crazy Circuits (有源汇上下界最小流)

    题意:一个电路板,上面有N个接线柱(标号1~N)   还有两个电源接线柱  +  - 然后是 给出M个部件正负极的接线柱和最小电流,求一个可以让所有部件正常工作的总电流. 析:这是一个有源汇有上下界的 ...

  3. angularjs 之 $watch

    双向绑定是Angular的核心概念之一,它给我们带来了思维方式的转变:不再是DOM驱动,而是以Model为核心,在View中写上声明式标签.然后,Angular就会在后台默默的同步View的变化到Mo ...

  4. FlexBox弹性盒布局

    网页布局(layout)是 CSS 的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂 ...

  5. How to Baskup and Restore a MySQL database

    If you're storing anything in MySQL databases that you do not want to lose, it is very important to ...

  6. 结对项目— 词频统计2(语言C++)

    结对对象:季天梦 博客地址:http://www.cnblogs.com/jitianmeng/ github链接:https://github.com/liuyutianlyt/EX_4.md 比例 ...

  7. OpenglEs开篇

    1.,但博客有接近一年没有写了.虽然有学到东西,但没有记录感觉是是空空的,最近在学习Opengles, 现在开始重操旧业(写博客了).

  8. C# task和timer实现定时操作

    C#中,定时器,或者叫作间隔器,每隔一段时间执行一个操作. 1.Timer本身就是多线程 C#中为不同场合下使用定时器,提供了不同的Timer类,在asp.net中一般使用System.Timers. ...

  9. ES6——异步操作之Promise

    基本概念: Promise : 是 ES6 中新增的异步编程解决方案,提现在代码中他是一个对象 可以通过Promise构造函数来实例化. -new Promise(cb) ===> 实例的基本使 ...

  10. Spring @SCHEDULED(CRON = "0 0 * * * ?")实现定时任务

    Spring配置文件xmlns加入 xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocati ...