springboot(五):整合mybatis

1、引入依赖

pom.xml文件引入依赖:

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

<!-- mysql -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

2、相关配置

在配置文件配置数据源:

spring:
    datasource:
        url: jdbc:mysql://localhost:3306/szl?useUnicode=true&characterEncoding=utf8&useSSL=true
        username: root
        password: root

配置文件mybatis的相关配置:

mybatis:
    type-aliases-package: com.szl.entity
    mapper-locations: classpath:mapper/*.xml

3、创建文件

文件结构如下:

在mysql库szl下创建customer表:

-- 创建customer表
CREATE TABLE `customer` (
  `uuid` varchar(32) NOT NULL COMMENT '主键',
  `delFlag` varchar(1) DEFAULT '' COMMENT '逻辑删除标记',
  `opeTime` varchar(25) DEFAULT '' COMMENT '操作时间',
  `oper` varchar(32) DEFAULT '' COMMENT '操作人',
  `createTime` varchar(25) DEFAULT '' COMMENT '创建时间',
  `customerName` varchar(60) DEFAULT '' COMMENT '用户名称',
  `email` varchar(60) DEFAULT '' COMMENT '邮箱',
  `frozenState` varchar(1) DEFAULT '' COMMENT '冻结状态',
  `frozenType` varchar(1) DEFAULT '' COMMENT '冻结类型',
  `lastWrongLoginTime` varchar(25) DEFAULT '' COMMENT '最后登录错误时间',
  `loginErrorTimes` int(11) DEFAULT NULL COMMENT '登录错误次数',
  `mobile` varchar(20) DEFAULT '' COMMENT '手机号',
  `password` varchar(32) DEFAULT '' COMMENT '登录密码',
  `salt` varchar(32) DEFAULT '' COMMENT '密码哈希',
  PRIMARY KEY (`uuid`),
  KEY `customerName_mobile_email` (`customerName`,`mobile`(11),`email`),
  KEY `mobile_index` (`mobile`(11)),
  KEY `email_index` (`email`),
  KEY `idx_mix_customername_createtime` (`customerName`,`createTime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户主表';

INSERT INTO customer (uuid, delflag, mobile, customername) VALUES (REPLACE(UUID(), '-',''),'1','18829281000','信仰');

创建实体类Customer和表对应

package com.szl.entity;

import java.io.Serializable;

public class Customer implements Serializable{

	/**
	 * 
	 */
	private String uuid;//主键
	private String delFlag;//逻辑删除标识
	private String opeTime;//操作时间
	private String oper;//操作人
	private String createTime;//创建时间
	private String customerName;//用户名
	private String email;//邮箱
	private String frozenState;//冻结状态
	private String frozenType;//冻结类型
	private String lastWrongLoginTime;//最后登录错误时间
	private Integer loginErrorTimes;//登录错误次数
	private String mobile;//手机号
	private String password;//登录密码
	private String salt;//密码哈希
	
	//getter , setter and toString
}

在resource下创建mapper文件夹,用于存放mapper*.xml;

创建CustomerMapper.xml文件,用于操作sql:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.szl.mapper.CustomerMapper">
	<select id="getCustomerByMobile" resultType="com.szl.entity.Customer">
		select * from customer where mobile = #{mobile}
	</select>
</mapper>

创建CustomerMapper.java接口,和mapper.xml对应

package com.szl.mapper;

import org.apache.ibatis.annotations.Param;

import com.szl.entity.Customer;

public interface CustomerMapper {

	public Customer getCustomerByMobile(@Param("mobile")String mobile);
}

创建CustomerService接口,需要执行的方法

扫描二维码关注公众号,回复: 3508130 查看本文章
package com.szl.service;

import com.szl.entity.Customer;

public interface CustomerService{

	/**
	 * 根据手机号查询用户信息
	 * @param mobile
	 * @return
	 */
	public Customer getCustomerByMobile(String mobile);
}

创建CustomerServiceImpl,实现CustomerService接口,类上使用@Service注解;

通过注入mapper,调用方法操作数据库;

方法上可使用@Transaction注解开启事务

package com.szl.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.szl.entity.Customer;
import com.szl.mapper.CustomerMapper;
import com.szl.service.CustomerService;

@Service
public class CustomerServiceImpl implements CustomerService{

	@Autowired
	private CustomerMapper customerMapper;
	
	@Override
	public Customer getCustomerByMobile(String mobile) {
		return customerMapper.getCustomerByMobile(mobile);
	}

}

创建controller,注入Service,调用Service方法,实现接口,返回数据

package com.szl.controller;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.szl.common.ResourceUtil;
import com.szl.common.ResultEnum;
import com.szl.entity.Customer;
import com.szl.service.CustomerService;
import com.szl.vo.ResultVo;

@RestController
@RequestMapping("/customer")
public class CustomerController {

	@Resource
	private CustomerService customerService;
	
	@Autowired
	private ResourceUtil resourceUtil;
	
	/**
	 * 根据手机号查询用户信息
	 * @param mobile
	 * @return
	 */
	@RequestMapping(value = "/getCustomer", method = RequestMethod.GET)
	public ResultVo getCustomer(@RequestParam(value = "mobile") String mobile){
		Customer customer = customerService.getCustomerByMobile(mobile);
		return ResultVo.getData(ResultEnum.SUCCESS, customer);
	}
}

通过访问:

localhost:8082/szl/customer/getCustomer?mobile=18829281000

返回用户手机号位18829281000的所有信息。

逻辑处理通常放在Service层(Service的实现类中)进行。

猜你喜欢

转载自blog.csdn.net/ynzz123/article/details/81007146