zoukankan      html  css  js  c++  java
  • springboot整合tkmybatis

      tkmybatis是什么?

      tkmybatis是为了简化mybatis单表的增删改查而诞生的,极其方便的使用MyBatis单表的增删改查,在使用mybatis单表增删改查时,可以直接调用tkmybatis中提供的方法直接调用而不用写xml配置文件。支持单表操作,不支持通用的多表联合查询。

      Springboot整合tkmybatis

      pom.xml:

      org.springframework.boot

      spring-boot-starter-web

      org.springframework.boot

      spring-boot-devtools

      runtime

      true

      org.springframework.boot

      spring-boot-starter-test

      test

      tk.mybatis

      mapper

      4.1.5

      tk.mybatis

      mapper-spring-boot-starter

      2.1.5

      mysql

      mysql-connector-java

      runtime

      org.projectlombok

      lombok

      true

      UserController:

      @RestController

      public class UserController {

      @Autowired

      private UserService userService;

      @RequestMapping("/findById/{id}")

      public User findById(@PathVariable int id) {

      User user = userService.findById(id);

      return user;

      }

      @RequestMapping("/findAll")

      public List findAll() {

      List userList = userService.findAll();

      return userList;

      }

      @RequestMapping("/insert")

      public void insert() {

      User user = new User();

      user.setName("张三");

      user.setSex("男");

      user.setAge(18);

      user.setAddress("江西省");

      user.setPhone("456789645");

      userService.insert(user);

      }

      @RequestMapping("/delete")

      public void delete() {

      User user = new User();

      user.setId(5);

      userService.delete(user);

      }

      @RequestMapping("/update")

      public void update() {

      User user = new User();

      user.setId(5);

      user.setName("李四");

      userService.update(user);

      }

      }

      UserService:

      public interface UserService {

      public User findById(int id);

      public List findAll();

      public void insert(User user);

      public void update(User user);

      public void delete(User user);

      }

      UserServiceImpl:

      @Service

      public class UserServiceImpl implements UserService {

      @Autowired

      private UserMapper userMapper;

      @Override

      public User findById(int id) {

      return userMapper.selectByPrimaryKey(id);

      }

      @Override

      public List findAll() {

      return userMapper.selectAll();

      }

      @Override

      public void insert(User user) {

      userMapper.insertSelective(user);

      }

      @Override

      public void update(User user) {

      userMapper.updateByPrimaryKey(user);

      }无锡人流医院 http://xmobile.wxbhnk120.com/

      @Override

      public void delete(User user) {

      userMapper.deleteByPrimaryKey(user);

      }

      }

      UserMapper:

      public interface UserMapper extends Mapper {

      }

      application.properties:

      #tomcat port

      server.port=8080

      #datasource

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

      spring.datasource.url=jdbc:mysql://188.131.247.26:3306/

      spring.datasource.username=root

      spring.datasource.password=root

      #logging

      logging.level.com.wyj.mapper:debug

      User:

      @Data

      @Entity

      public class User implements Serializable {

      @Id

      @KeySql(useGeneratedKeys = true)

      private Integer id;

      private String name;

      private String sex;

      private Integer age;

      private String address;

      private String phone;

      }

      SpringbootTkmybatisApplication:

      @SpringBootApplication

      @MapperScan("com.wyj.mapper")//tkmybatis的注解

      public class SpringbootTkmybatisApplication {

      public static void main(String[] args) {

      SpringApplication.run(SpringbootTkmybatisApplication.class, args);

      }

      }

  • 相关阅读:
    第五章 Python——字符编码与文件处理
    第六章 Python——函数与面向过程编程
    第七章 Python——模块与包
    第一章 计算机硬件基础与操作系统介绍
    luogu P1706 全排列问题
    luogu 2142 高精度减法
    luogu P1601 高精度加法
    luogu P1803 线段覆盖 贪心
    luogu P1031 均分纸牌 贪心
    luogu P2678 跳石头 二分答案
  • 原文地址:https://www.cnblogs.com/djw12333/p/11119129.html
Copyright © 2011-2022 走看看