概述
tkmybatis的主要作用就是让我们不需要在手动写简单的CRUD sql语句了,复杂的sql肯定还是避免不了手写的。
pom文件配置
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.practice</groupId>
<artifactId>tkmybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>tkmybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- tkmybatis -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
测试环境配置
application.properties
#tomcat port
server.port=8080
#datasource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://ip:port/database?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=xxx
spring.datasource.password=xxx
#logging
#TkMybatis
mybatis.mapper-locations=classpath*:mapper/*.xml
controller层编写
package com.practice.tkmybatis.controller;
import com.practice.tkmybatis.dao.UserEntity;
import com.practice.tkmybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
/**
* @Description : 测试tkmybatis功能
* @Author : asus
* @Version : xxx
* @Date : 2021/6/8 22:38
*/
@Controller
public class UserController {
@Autowired
private UserService userService;
public UserEntity findById(int id) {
UserEntity entity = userService.findById(id);
System.out.println(entity);
return entity;
}
public void insertUser() {
UserEntity userEntity = UserEntity.builder().id(1).name("张三").age(18).build();
userService.insert(userEntity);
}
}
dao层编写
package com.practice.tkmybatis.dao;
import lombok.Builder;
import lombok.Data;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
/**
* @Description : user实体类
* @Author : asus
* @Version : xxx
* @Date : 2021/6/8 22:41
*/
@Data
@Builder
@Table(name = "test_user")
public class UserEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Integer id;
private String name;
private String sex;
private Integer age;
private String address;
private String phone;
}
service层编写
package com.practice.tkmybatis.service;
import com.practice.tkmybatis.dao.UserEntity;
import java.util.List;
/**
* @Description : user服务接口
* @Author : asus
* @Version : xxx
* @Date : 2021/6/8 23:12
*/
public interface UserService {
UserEntity findById(int id);
List<UserEntity> findAll();
void insert(UserEntity user);
void update(UserEntity user);
void delete(UserEntity user);
}
package com.practice.tkmybatis.service.impl;
import com.practice.tkmybatis.dao.UserEntity;
import com.practice.tkmybatis.mapper.UserMapper;
import com.practice.tkmybatis.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* @Description : user service 实现类
* @Author : asus
* @Version : xxx
* @Date : 2021/6/8 23:16
*/
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public UserEntity findById(int id) {
return userMapper.selectByPrimaryKey(1);
}
@Override
public List<UserEntity> findAll() {
return null;
}
@Override
public void insert(UserEntity user) {
userMapper.insertSelective(user);
}
@Override
public void update(UserEntity user) {
}
@Override
public void delete(UserEntity user) {
}
}
mapper层编写
- 重点来了,这里mapper继承tkmybatis中的Mapper接口,我们可以做到不写任何sql语句就可以完成简单增删改查操作。
package com.practice.tkmybatis.mapper;
import com.practice.tkmybatis.dao.UserEntity;
import tk.mybatis.mapper.common.Mapper;
/**
* @Description : user mapper类
* @Author : asus
* @Version : xxx
* @Date : 2021/6/8 23:12
*/
@org.apache.ibatis.annotations.Mapper
public interface UserMapper extends Mapper<UserEntity> {
}
测试
package com.practice.tkmybatis;
import com.practice.tkmybatis.controller.UserController;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class TkmybatisApplicationTests {
@Autowired
private UserController controller;
@Test
public void testInsertUser() {
controller.insertUser();
}
}
- 数据表中新增了记录
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/100320.html