通用Mapper的使用

通用Mapper的使用

1.导入Maven


<dependency>
    <groupId>tk.mybatisgroupId>
    <artifactId>mapper-spring-boot-starterartifactId>
<version>1.2.3version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        exclusion>
    exclusions>
dependency>

2.增加注解 扫描

注意:通用mapper是tk.mybatis.mapper

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

@SpringBootApplication
@MapperScan(basePackages = "com.wzy.gmall.gmalluser.mapper")
public class GmallUserApplication {

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

}

3.配置数据源 建表

spring.datasource.url=jdbc:mysql://localhost:3306/gmall?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123123

CREATE TABLE `user_info` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '编号',
  `login_name` varchar(200) DEFAULT NULL COMMENT '用户名称',
  `nick_name` varchar(200) DEFAULT NULL COMMENT '用户昵称',
  `passwd` varchar(200) DEFAULT NULL COMMENT '用户密码',
  `name` varchar(200) DEFAULT NULL COMMENT '用户姓名',
  `phone_num` varchar(200) DEFAULT NULL COMMENT '手机号',
  `email` varchar(200) DEFAULT NULL COMMENT '邮箱',
  `head_img` varchar(200) DEFAULT NULL COMMENT '头像',
  `user_level` varchar(200) DEFAULT NULL COMMENT '用户级别',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8 COMMENT='用户表'

4.bean

package com.wzy.gmall.gmalluser.pojo;


import lombok.Data;

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


@Data// lombok
public class UserInfo implements Serializable {
    @Id//表示主键
    @Column//普通字段列
    @GeneratedValue(strategy = GenerationType.IDENTITY)//Mysql主键自增 oracle为AUTO
    private String id;
    @Column
    private String loginName;
    @Column
    private String nickName;
    @Column
    private String passwd;
    @Column
    private String name;
    @Column
    private String phoneNum;
    @Column
    private String email;
    @Column
    private String headImg;
    @Column
    private String userLevel;

}



5. Mapper 注:tk.mybatis包下的

package com.wzy.gmall.gmalluser.mapper;

import com.wzy.gmall.gmalluser.pojo.UserInfo;
import tk.mybatis.mapper.common.Mapper;/

public interface UserInfoMapper extends Mapper<UserInfo> {
	//这样省去了mybatis的单表操作的sql语句,起到了和mybatis逆向工程相似的作用
}

6. CRUD


package com.wzy.gmall.gmalluser.service;

import com.wzy.gmall.gmalluser.pojo.UserInfo;

import java.util.List;


public interface UserService {


    /**
     * 查询
     * @return
     */
    List<UserInfo> finAll();

    /**
     * 添加
     * @param userInfo
     * @return
     */
    int insertOne(UserInfo userInfo);

    //修改

    /**
     * 修改 按照id修改
     * @param userInfo
     */
    void updataUser(UserInfo userInfo);


    /**
     * 按条件修改
     * @param userInfo
     */
    void updateUserByName(UserInfo userInfo);

    //删除

    /**
     * 按id 删除
     * @param id
     */
    void deleteUserById(String id);

    /**
     * 按条件删除
     * @param userInfo
     */
    void deleteByExample(UserInfo userInfo);


    /**
     * 按条件删除2
     * @param userInfo
     */
    void delete(UserInfo userInfo);

}






package com.wzy.gmall.gmalluser.service.impl;

import com.wzy.gmall.gmalluser.mapper.UserInfoMapper;
import com.wzy.gmall.gmalluser.pojo.UserInfo;
import com.wzy.gmall.gmalluser.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
@Service

public class UserServiceImpl implements UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public List<UserInfo> finAll() {
        return userInfoMapper.selectAll();
    }

    @Override
    public int insertOne(UserInfo userInfo) {
        return userInfoMapper.insertSelective(userInfo);
    }

    @Override
    public void updataUser(UserInfo userInfo) {
        userInfoMapper.updateByPrimaryKeySelective(userInfo);
    }

    @Override
    public void updateUserByName(UserInfo userInfo) {
        userInfo.setEmail("159");
        userInfo.setLoginName("haha");
        Example example = new Example(UserInfo.class);

        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("name",userInfo.getName());
        criteria.andEqualTo("email","123");
        userInfoMapper.updateByExampleSelective(userInfo,example);
    }

    @Override
    public void deleteUserById(String id) {
        userInfoMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void deleteByExample(UserInfo userInfo) {
        Example example = new Example(UserInfo.class);
        example.createCriteria().andEqualTo("name",userInfo.getName());
        userInfoMapper.deleteByExample(example);
    }

    @Override
    public void delete(UserInfo userInfo) {
        userInfoMapper.delete(userInfo);
    }


}

你可能感兴趣的:(SpringBoot框架,mybatis,java,spring)