1. 新建springboot工程

访问https://start.spring.io/,新建一个springboot工程。

自动生成的工程主要的注意点如下:

1)pom.xml

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.1.2.RELEASE</version>

<relativePath /> <!-- lookup parent from repository -->

</parent>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

将Spring Boot应用打包为可执行的jar或war文件:

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

2. 使用tk-mybatis构建数据库访问示例

1)application.properties

resources目录下的application.properties文件配置如下:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mytest?useUnicode=true&characterEncoding=utf8&useSSL=false

spring.datasource.username=root

spring.datasource.password=123456

spring.datasource.max-idle=10

spring.datasource.max-wait=10000

spring.datasource.min-idle=5

spring.datasource.initial-size=5

logging.level.com.tkmybatislearning.demo=DEBUG

2)entity

新建entity实体类Country.java, 命名与数据库的表名country对应:

package com.tkmybatislearning.demo.entity;

import javax.persistence.Id;

public class Country {

@Id

private Integer id;

private String  countryname;

private String  countrycode;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getCountryname() {

return countryname;

}

public void setCountryname(String countryname) {

this.countryname = countryname;

}

public String getCountrycode() {

return countrycode;

}

public void setCountrycode(String countrycode) {

this.countrycode = countrycode;

}

}

3)mapper

新增mapper接口文件CountryMapper.java,继承tk的Mapper,可以自定义方法:

package com.tkmybatislearning.demo.mapper;

import org.apache.ibatis.annotations.Select;

import com.tkmybatislearning.demo.entity.Country;

import tk.mybatis.mapper.common.Mapper;

public interface CountryMapper extends Mapper<Country> {

@Select("select * from country where countryname = #{countryname}")

Country selectByCountryName(String countryname);

}

4)service

新增service服务文件CountryService.java,自动注入CountryMapper,可以使用tk自带的方法,对数据库表进行CRUD操作:

package com.tkmybatislearning.demo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.tkmybatislearning.demo.entity.Country;

import com.tkmybatislearning.demo.mapper.CountryMapper;

@Service

public class CountryService {

@Autowired

private CountryMapper countryMapper;

public void testCountry() {

//从 MyBatis 或者 Spring 中获取 countryMapper,然后调用 selectAll 方法

List<Country> countries = countryMapper.selectAll();

//根据主键查询

Country country = countryMapper.selectByPrimaryKey(1);

//或者使用对象传参,适用于1个字段或者多个字段联合主键使用

Country query = new Country();

query.setId(1);

country = countryMapper.selectByPrimaryKey(query);

}

}

3. 新建应用启动程序

DemoApplication.java:

package com.tkmybatislearning.demo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication

@MapperScan("com.tkmybatislearning.demo.mapper")

public class DemoApplication {

public static void main(String[] args) {

SpringApplication.run(DemoApplication.class, args);

}

}

4. 使用单元测试验证示例

新建DemoApplicationTests.java:

package com.tkmybatislearning.demo;

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 com.tkmybatislearning.demo.service.CountryService;

@RunWith(SpringRunner.class)

@SpringBootTest(classes = DemoApplication.class)

public class DemoApplicationTests {

@Autowired

private CountryService countryService;

@Test

public void contextLoads() {

countryService.testCountry();

}

}

在springboot环境下tk-mybatis的使用记录的更多相关文章

  1. 项目总结10:通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题

    通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题 关键字 springboot热部署  ClassCastException异常 反射 ...

  2. springboot集成下,mybatis的mapper代理对象究竟是如何生成的

    前言 开心一刻 中韩两学生辩论. 中:端午节是属于谁的? 韩:韩国人! 中:汉字是谁发明的? 韩:韩国人! 中:中医是属于谁的? 韩:韩国人! 中:那中国人到底发明过什么? 韩:韩国人! 前情回顾 M ...

  3. springboot环境下配置过滤器和拦截器

    以前我们在配置过滤器和拦截器的时候,都是一个类继承一个接口,然后在xml中配置一下就ok 但是,但是,这是springboot的环境,没有xml的配置.所以我们还要继续学习啊啊啊啊啊~~~~~ 先简单 ...

  4. MyBatisPlus环境下使用MyBatis的配置类

    通过@Configuration使用MyBatis配置类的资料比较少,大部分都是通过XML的形式.找了好久,最终还是通过官方的文档找到了解决方法:http://www.mybatis.org/spri ...

  5. 非Spring环境下使用Mybatis操作数据库的流程

    准备工作 1,  导入mybatis-3.2.7.jar,mysql-connector-5.1.25-bin.jar两个jar包 2,  在数据库中创建一个db_test数据库,库中有一个表为use ...

  6. SpringBoot环境中使用MyBatis代码生成工具

    一.Maven配置文件中添加如下依赖 <dependency> <groupId>org.mybatis.generator</groupId> <artif ...

  7. SpringBoot环境下使用测试类注入Mapper接口报错解决

    当我们在进行开发中难免会要用到测试类,而且测试类要注入Mapper接口,如果测试运行的时候包空指针异常,看看测试类上面的注解是否用对! 正常测试我们需要用到的注解有这些: @SpringBootTes ...

  8. SpringBoot环境下java实现文件的下载

    思路:文件下载,就是给服务器上的文件创建输入流,客户端创建输出流,将文件读出,读入到客户端的输出流中,(流与流的转换) package com.cst.icode.controller; import ...

  9. Centos7.4 版本环境下安装Mysql5.7操作记录

    Centos7.x版本下针对Mysql的安装和使用多少跟之前的Centos6之前版本有所不同的,废话就不多赘述了,下面介绍下在centos7.x环境里安装mysql5.7的几种方法: 一.yum方式安 ...

随机推荐

  1. C# —— 枚举

    一.使用枚举的优点 枚举能够使代码更加的清晰,它允许使用描述性的名称表示整数值. 枚举使代码更易于维护,有助于确保给变量指定合法的.期望的值. 枚举使代码更易输入. 二.枚举说明 1.简单枚举 枚举使 ...

  2. SpringBoot @Transactional的rollbackFor属性

    1.简单回顾Java Exception 该图摘自:https://blog.csdn.net/zhangerqing/article/details/8248186 一方面,我们可以将异常分为运行时 ...

  3. @codeforces - 1221G@ Graph And Numbers

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 n 点 m 边的无向图. 现在要求给每个点写上 0 或 ...

  4. 如何减少idea的内存消耗

    如何减少idea的内存消耗 标签: idea 内存 内存泄露 异常 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u014028392/articl ...

  5. 2014年最热门的国人开发开源软件TOP100

    2014年最热门的国人开发开源软件TOP100 不知道从什么时候开始,很多一说起国产好像就非常愤慨,其实大可不必.做开源中国六年有余,这六年时间国内的开源蓬勃发展,从一开始的使用到贡献,到推出自己很多 ...

  6. 3、.net core 部署到IIS

    1)下载对应的Hosting Bundle https://dotnet.microsoft.com/download/dotnet-core/2.2 2)VS发布项目,选择window平台环境 3 ...

  7. [C#] WebClient性能优化

    WebClient缺省是为了安全和方便,不是为了性能.所以,当你打算做压力测试的时候,就会发现WebClient很慢. WebClient性能很差,主要原因有: 1.它缺省会使用IE的代理设置,而IE ...

  8. Python--day69--ORM正反向查找(外键)

    ForeignKey操作 正向查找 对象查找(跨表) 语法: 对象.关联字段.字段   示例: book_obj = models.Book.objects.first() # 第一本书对象 prin ...

  9. pytorch入坑一 | Tensor及其基本操作

    由于之前的草稿都没了,现在只有重写…. 我好痛苦 本章只是对pytorch的常规操作进行一个总结,大家看过有脑子里有印象就好,知道有这么个东西,需要的时候可以再去详细的看,另外也还是需要在实战中多运用 ...

  10. H3C 帧中继数据链路标识