tkmapper教程_trello怎么用

tkmapper教程_trello怎么用tkMappertkMapper整合整合mybatis依赖配置类启动类扫描整合tkMapper依赖修改启动类的`@MapperScan`注解的包tkMapper使用创建数据表创建实体类创建Mapper接口测试tkMapper的常用方法增删改添加修改删除查询查询所有根据主键查询条件查询分页查询带条件分页查询基于MyBatis提供了很多第三方插件,这些插件通常可以完成数据操作方法的封装(GeneralDAO)、数据库逆向工程工作(根据数据表生成实体类、生成映射文件)tkMapper就是一个MyBatis

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

基于MyBatis提供了很多第三方插件,这些插件通常可以完成数据操作方法的封装(GeneralDAO)、数据库逆向工程工作(根据数据表生成实体类、生成映射文件)

  • tkMapper就是一个MyBatis插件,是在MyBatis的基础上提供了很多工具,让开发变得简单,提高开发效率。

  • 提供了针对单表通用的数据库操作方法

  • 逆向工程(根据数据表生成实体类、dao接口、映射文件)

tkMapper整合

整合mybatis

依赖

		<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.20</version>
        </dependency>


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

配置类

server:
  port: 8080
  servlet:
    context-path: /

#连接数据库的信息
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/fmmall?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: 123456


mybatis:
  # 扫描映射文件
  mapper-locations: classpath:mapper/*.xml
  # 配置别名扫描的包
  type-aliases-package: com.blb.entity
  configuration:
    # 开启驼峰映射配置
    map-underscore-to-camel-case: true


启动类扫描

@SpringBootApplication
@MapperScan("com.blb.mapper")
public class TkmapperApplication { 
   

    public static void main(String[] args) { 
   
        SpringApplication.run(TkmapperApplication.class, args);
    }

}

整合tkMapper

依赖

<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>2.1.5</version>
</dependency>

修改启动类的@MapperScan注解的包

  • tk.mybatis.spring.annotation.MapperScan
package com.blb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@MapperScan("com.blb.mapper")
public class TkmapperApplication { 
   

    public static void main(String[] args) { 
   
        SpringApplication.run(TkmapperApplication.class, args);
    }

}

tkMapper使用

创建数据表

CREATE TABLE `users`  (
  `user_id` int(64) NOT NULL AUTO_INCREMENT COMMENT '主键id 用户id',
  `username` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户名 用户名',
  `password` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '密码 密码',
  `nickname` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '昵称 昵称',
  `realname` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '真实姓名 真实姓名',
  `user_img` varchar(1024) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '头像 头像',
  `user_mobile` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '手机号 手机号',
  `user_email` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱地址 邮箱地址',
  `user_sex` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '性别 M(男) or F(女)',
  `user_birth` date NULL DEFAULT NULL COMMENT '生日 生日',
  `user_regtime` datetime(0) NOT NULL COMMENT '注册时间 创建时间',
  `user_modtime` datetime(0) NOT NULL COMMENT '更新时间 更新时间',
  PRIMARY KEY (`user_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '用户 ' ROW_FORMAT = Compact;

创建实体类

@Data
@NoArgsConstructor
@AllArgsConstructor


@Table(name="users")// 表名和数据库名不一致无法映射
public class User { 
   

    private int userId;
    private String username;
    private String password;
    private String nickname;
    private String realname;
    private String userImg;
    private String userMobile;
    private String userEmail;
    private String userSex;
    private Date userBirth;
    private Date userRegtime;
    private Date userModtime;

}

创建Mapper接口

tkMapper已经完成了对单表的通用操作的封装,封装在Mapper接口和MySqlMapper接口;因此如果我们要完成对单表的操作,只需自定义Mapper接口继承Mapper接口和MySqlMapper接口

package com.blb.mapper;

import com.blb.entity.User;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

public interface UserMapper extends Mapper<User>, MySqlMapper<User> { 
   
}

测试

@SpringBootTest
class TkmapperApplicationTests { 
   


    @Autowired
   private UserMapper userMapper;
    @Test
    void testInsert() { 
   
        User user=new User();
        user.setUsername("aaaa");
        user.setPassword("1111");
        user.setUserImg("img/default.png");
        user.setUserRegtime(new Date());
        user.setUserModtime(new Date());
        int insert = userMapper.insert(user);
        System.out.println(insert);
    }

}

tkMapper的常用方法

package com.blb.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Id;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Category { 
   

    @Id
    private Integer categoryId;
    private String categoryName;
    private Integer categoryLevel;
    private Integer parentId;
    private String categoryIcon;
    private String categorySlogan;
    private String categoryPic;
    private String categoryBgColor;

}

package com.blb.mapper;

import com.blb.entity.Category;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

public interface CategoryMapper extends Mapper<Category>, MySqlMapper<Category> { 
   
}

增删改

添加

Category category = new Category(0,"测试类别3",1,0,"03.png","xixi","aaa.jpg","black");
        //int i = categoryMapper.insert(category);
        int i = categoryMapper.insertUseGeneratedKeys(category);
        System.out.println(category.getCategoryId());
        assertEquals(1,i);

注意如果要主键回填一定要在对应的实体类的主键上添加@Id

修改

Category category = new Category(48,"测试类别4",1,0,"04.png","heihei","aaa.jpg","black");
        int i = categoryDAO.updateByPrimaryKey(category);
        // 根据自定义条件修改,Example example就是封装条件的
        // int i1 = categoryDAO.updateByExample( Example example);
        assertEquals(1,i);

删除

  int i = categoryDAO.deleteByPrimaryKey(48);
        // 根据条件删除
        //int i1 = categoryDAO.deleteByExample(Example example);
        assertEquals(1,i);

查询

查询所有

 //查询所有
        List<Category> categories = categoryDAO.selectAll();
        for (Category category: categories) { 
   
            System.out.println(category);
        }

根据主键查询

//根据主键查询
        Category category = categoryDAO.selectByPrimaryKey(47);
        System.out.println(category);

条件查询

//条件查询
        //1.创建一个Example封装 类别Category查询条件
        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("categoryLevel",1);
        criteria.orLike("categoryName","%干%");

        List<Category> categories = categoryDAO.selectByExample(example);
        for (Category category: categories) { 
   
            System.out.println(category);
        }

分页查询

//分页查询
        int pageNum = 2;
        int pageSize = 10;
        int start = (pageNum-1)*pageSize;

        RowBounds rowBounds = new RowBounds(start,pageSize);
        List<Category> categories = categoryDAO.selectByRowBounds(new Category(), rowBounds);
        for (Category category: categories) { 
   
            System.out.println(category);
        }

        //查询总记录数
        int i = categoryDAO.selectCount(new Category());
        System.out.println(i);

带条件分页查询

//带条件分页
        //条件
        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("categoryLevel",1);
        //分页
        int pageNum = 2;
        int pageSize = 3;
        int start = (pageNum-1)*pageSize;
        RowBounds rowBounds = new RowBounds(start,pageSize);

        List<Category> categories = categoryDAO.selectByExampleAndRowBounds(example,rowBounds);
        for (Category category: categories) { 
   
            System.out.println(category);
        }

        //查询总记录数(满足条件)
        int i = categoryDAO.selectCountByExample(example);
        System.out.println(i);

关联查询

就想原来一样在接口里写方法,写mybatis的xml即可

逆向工程

逆向工程,根据创建好的数据表,生成实体类、DAO、映射文件、

添加逆向工程依赖

<build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>

                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.20</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>4.1.5</version>
                    </dependency>
                </dependencies>
            </plugin>

            <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>
<?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>
    <!-- 引入数据库连接配置 -->
    <!-- <properties resource="jdbc.properties"/>-->

    <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <!-- 配置 GeneralDAO -->
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="com.blb.general.GeneralMapper"/>
        </plugin>

        <!-- 配置数据库连接 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/fmmall?serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=UTF-8" userId="root" password="123456">
        </jdbcConnection>

        <!-- 配置实体类存放路径 -->
        <javaModelGenerator targetPackage="com.blb.entity" targetProject="src/main/java"/>

        <!-- 配置 XML 存放路径 -->
        <sqlMapGenerator targetPackage="/" targetProject="src/main/resources/mapper"/>

        <!-- 配置 DAO 存放路径 -->
        <javaClientGenerator targetPackage="com.blb.mapper" targetProject="src/main/java" type="XMLMAPPER"/>

        <!-- 配置需要指定生成的数据库和表,% 代表所有表 -->
        <table tableName="%">
            <!-- mysql 配置 -->
            <!-- <generatedKey column="id" sqlStatement="Mysql" identity="true"/>-->
        </table>
        <!-- <table tableName="tb_roles">-->
        <!-- &lt;!&ndash; mysql 配置 &ndash;&gt;-->
        <!-- <generatedKey column="roleid" sqlStatement="Mysql" identity="true"/>-->
        <!-- </table>-->
        <!-- <table tableName="tb_permissions">-->
        <!-- &lt;!&ndash; mysql 配置 &ndash;&gt;-->
        <!-- <generatedKey column="perid" sqlStatement="Mysql" identity="true"/>-->
        <!-- </table>-->
    </context>
</generatorConfiguration>
<build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                </configuration>

                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.20</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>3.4.4</version>
                    </dependency>
                </dependencies>
            </plugin>

            <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>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/184975.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • navicat怎么连接sql_内镜连接时的注意事项

    navicat怎么连接sql_内镜连接时的注意事项使用Navicat连接SqlServer注意事项1.需要到Navicat安装目录下安装驱动2.端口连接时ip与端口号用“,”隔开

  • java必背代码_java必背代码入门有哪些?如何写出优质代码?

    java必背代码_java必背代码入门有哪些?如何写出优质代码?学习java还是要不断的积累,有必要的话,还是需要大家记忆的,这样更加有利于大家学习java,那么今天我们就给大家分享一些java入门必背代码,希望能够对大家有用!1、把Javautil.Date转成sql.Datejava.util.DateutilDate=newjava.util.Date();java.sql.DatesqlDate=newjava.sql.Date…

  • 多线程修改VCL控件造成的ntdll.dll错

    多线程修改VCL控件造成的ntdll.dll错 http://www.delphibbs.com/delphibbs/dispq.asp?lid=1574375问题:我调试老是提示:NTDLL.DLL出错线程中:Execute中调用一个写在线程单元的过程P,p执行完后调用Call过程,如下:procedureTDatav.Execute;begin if…..  begin    p;PP:

  • iphone手机获取不了软件_手机uuid什么意思

    iphone手机获取不了软件_手机uuid什么意思UDID是一种iOS设备的特殊识别码。除序号之外,每台iOS装置都另有一组独一无二的号码,我们就称之为识别码(UniqueDeviceIdentifier,UDID)。每个iOS开发者在缴交99美元年费的同时,都会得到100个betatest名额,可以让软件开发者输入100台iOS装置的UDID。有意成为betatester的人,只需向开发者提交UDID,经过输入及制成一个描述

  • python生成一组随机数_python随机数组

    python生成一组随机数_python随机数组“Anyonewhoconsidersarithmeticalmethodsofproducingrandomdigitsis,ofcourse,inastateofsin.”JohnvonNeumann,1951Python中自带了随机数的模块random,它们编程当前往往是十分重要的。下面对random模块进行介绍。random模块randint()ran…

    2022年10月24日
  • 树莓派连接wifi教程[通俗易懂]

    树莓派连接wifi教程[通俗易懂]第一种方法:如果你已经连接了VNC图形界面,就像手机电脑一样点击wifi的图标找到你的wifi输入密码就行第二种方法:如果登录了putty1.输入sudonano/etc/wpa_supplicant/wpa_supplicant.conf2.在尾部添加network={ssid=""psk=""}引号内容SSID是你的无线名称PSK是你的无线密码无线名称不能是中…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号