17

spring boot2.x 集成tk.mybatis

s

1.pom.xml文件引入对应jar

 <!-- tk spring boot 依赖-->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>2.1.4</version>
</dependency>
<!-- 引入lombok,需要安装插件,简约代码使用 -->
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <optional>true</optional>
</dependency>

2.application.properties配置

##启动端口
server.port=9090
##数据源
spring.datasource.url=jdbc:mysql://localhost:3306/ad?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##mybatis
mybatis.type-aliases-package=com.ad.project2019.entity
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=30
##tk.mybatis
mapper.mappers=tk.mybatis.mapper.common.Mapper
mapper.notEmpty=true

3.创建表

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户id',
  `username` varchar(20) NOT NULL COMMENT '用户名称',
  `password` varchar(32) NOT NULL COMMENT '用户密码',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='用户信息';

4.创建entity

package com.ad.project2019.entity;

import lombok.Data;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.io.Serializable;

@Data
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String username;

    private String password;

}

5.创建service,impl,dao层

1.service-一些基本的增删改查

package com.ad.project2019.service;

import com.ad.project2019.entity.User;

import java.util.List;

public interface UserService {

    //根据主键id查询
    User findById(Integer id);
    //查询所有
    List<User> findAll();
    //单表插入
    int saveUser(User user);
    //批量插入user
    int saveList(List<User> users);
    //编辑(通用mapper会根绝主键id进行编辑)
    int updateUser(User user);
    //根据主键删除
    int deteleById(Integer id);
}

2.impl

package com.ad.project2019.service.impl;

import com.ad.project2019.dao.UserDao;
import com.ad.project2019.entity.User;
import com.ad.project2019.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    private final UserDao userDao;

    @Autowired
    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public User findById(Integer id) {
        return userDao.selectByPrimaryKey(id);
    }

    @Override
    public List<User> findAll() {
        return userDao.selectAll();
    }

    @Override
    public int saveUser(User user) {
        return userDao.insert(user);
    }

    @Override
    public int saveList(List<User> users) {

        return userDao.insertList(users);
    }
    @Override
    public int updateUser(User user) {
        return userDao.updateByPrimaryKey(user);
    }

    @Override
    public int deteleById(Integer id) {
        return userDao.deleteByPrimaryKey(id);
    }
}

3.dao层

package com.ad.project2019.dao;

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

@org.apache.ibatis.annotations.Mapper
public interface UserDao extends Mapper<User>, MySqlMapper<User> {

}

6.测试

package com.ad.project2019;

import com.ad.project2019.entity.User;
import com.ad.project2019.service.UserService;
import lombok.extern.slf4j.Slf4j;
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.ArrayList;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
@Slf4j
public class UserServiceTest {

    @Autowired
    private UserService userService;


    @Test
    public void saveUser(){
        User user = new User();
        user.setUsername("userName");
        user.setPassword("password");
        log.info("saveUser:"+userService.saveUser(user));
    }
    @Test
    public void saveList(){
        List<User> users = new ArrayList<>();
        for(int i=0;i<10;i++){
            User user = new User();
            user.setUsername("userName"+i);
            user.setPassword("password"+i);
            users.add(user);
        }
        log.info("saveUser:"+userService.saveList(users));
    }

    @Test
    public void findById(){
        log.info("findById:"+userService.findById(1));
    }
    @Test
    public void findAll(){
        log.info("findAll:"+userService.findAll());
    }
    @Test
    public void updateUser(){
        User user = userService.findById(1);
        user.setUsername("updateTest");
        user.setPassword("updatePassword");
        log.info("updateUser:"+userService.updateUser(user));
    }

    @Test
    public void deteleById(){
        log.info("deteleById:"+userService.deteleById(1));
    }
}

Example 的查询,可以查找相关文献,在这里就不细说了…twemoji-1f1e8-1f1f3

评论后,需要站长审核通过,才会展出

人一共会长大三次;
第一次,是当你发现你不是世界的中心时,于是你开始努力
第二次是当你无能为力还会拼命争取时
第三次,是当你发现已经站在世界中心但不想成为世界中心的时候.

范俊伟(17)

Email:vanjoon@163.com
WeChat:Vanjoon_