从零开始搭建springboot项目(二)、整合mybatis,搭建数据访问层框架

本次创建项目分成下面几个步骤介绍
(1)创建springboot项目;
(2)整合mybatis,搭建数据访问层框架
(3)整合shiro搭建权限验证框架;
(4)自动生成代码;
(5)整合logger搭建日志框架

上一篇我们介绍了如何创建springboot项目,这一篇我们将介绍如何整和mybatis,搭建数据访问层框架;

整合mybatis

准备工作:

第一步:pom文件准备:pom文件引入mybatis包:

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

第二步:数据库添加测试数据:

/*
 Navicat Premium Data Transfer

 Source Server         : 47.100.254.99
 Source Server Type    : MySQL
 Source Server Version : 50642
 Source Host           : 47.100.254.99:3306
 Source Schema         : booting

 Target Server Type    : MySQL
 Target Server Version : 50642
 File Encoding         : 65001

 Date: 29/03/2020 17:36:27
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for sys_user
-- ----------------------------
DROP TABLE IF EXISTS `sys_user`;
CREATE TABLE `sys_user` (
  `uid` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT '主键',
  `username` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '用户名',
  `password` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '密码',
  `password_salt` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '盐值',
  `user_identify` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '标识',
  `realname` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '真实姓名',
  `user_status` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '状态',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

-- ----------------------------
-- Records of sys_user
-- ----------------------------
BEGIN;
INSERT INTO `sys_user` VALUES ('f69afaa8ccd14cfb911675e143200486', 'booting', 'edd7d02129698975439eb42896f03147', '6bc35c3a33c50f2ce9701b4d83a02c4e', '1', '管理员', '1', '2020-03-29 15:56:23');
COMMIT;

SET FOREIGN_KEY_CHECKS = 1;

(一)、配置application.properties文件,配置spring.profiles.active=dev;用来扫描开发环境的配置文件,具体用法看我前面的文章,传送门

application.properties

spring.profiles.active=dev

(二)、在application-dev.properties文件中,配置数据库连接信息和mybatis mapper配置信息,具体配置如下:
mybatis.mapper-locations:指定mapping文件位置
mybatis.config-location:指定mybatis配置文件位置

spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/booting?useUnicode=true&characterEncoding=utf-8&useSSL=true

mybatis.mapper-locations=classpath:mybatis/**/mapping/*.xml
mybatis.config-location=classpath:mybatis-config.xml

(三)、配置mybatis-config.xml;
mapUnderscoreToCamelCase=true:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
         <!-- mybatis 自动驼峰命名转换 -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

(四)、创建SysUser.java

package com.itwopqq.booting.system.model;

import java.io.Serializable;
import java.util.Date;

/**
 * @author fanzhen
 * @desx
 * @date 2020-03-29
 */
public class SysUser implements Serializable {

    private String uid;

    /***
     * 用户名
     */
    private String username;

    /***
     * 密码
     */
    private String password;

    /***
     * 盐值
     */
    private String passwordSalt;

    /***
     * 标识位
     */
    private String userIdentify;

    /***
     * 真实姓名
     */
    private String realname;

    /***
     * 创建时间
     */
    private Date createTime;

    /***
     * 用户状态
     */
    private String userStatus;

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPasswordSalt() {
        return passwordSalt;
    }

    public void setPasswordSalt(String passwordSalt) {
        this.passwordSalt = passwordSalt;
    }

    public String getUserIdentify() {
        return userIdentify;
    }

    public void setUserIdentify(String userIdentify) {
        this.userIdentify = userIdentify;
    }

    public String getRealname() {
        return realname;
    }

    public void setRealname(String realname) {
        this.realname = realname;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getUserStatus() {
        return userStatus;
    }

    public void setUserStatus(String userStatus) {
        this.userStatus = userStatus;
    }
}

(五)、SysUserMapper.java

package com.itwopqq.booting.system.mapper;

import com.itwopqq.booting.system.model.SysUser;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author fanzhen
 * @desx
 * @date 2020-03-29
 */
@Repository
public interface SysUserMapper {

    /***
     * @desc 获取所有用户
     * @author fanzhen
     * @date 2020-03-29 16:04
     * @return SysUser
     */
    List<SysUser> selectAll();
}

(六)、SysUserMapping.xml

<?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.itwopqq.booting.system.mapper.SysUserMapper">

    <resultMap id="BaseResultMap" type="com.itwopqq.booting.system.model.SysUser">
        <result column="uid" jdbcType="VARCHAR" property="uid" />
        <result column="username" jdbcType="VARCHAR" property="username" />
        <result column="password" jdbcType="VARCHAR" property="password" />
        <result column="password_salt" jdbcType="VARCHAR" property="passwordSalt" />
        <result column="user_identify" jdbcType="VARCHAR" property="userIdentify" />
        <result column="realname" jdbcType="VARCHAR" property="realname" />
        <result column="user_status" jdbcType="VARCHAR" property="userStatus" />
        <result column="create_time" jdbcType="VARCHAR" property="createTime" />
    </resultMap>

    <select id="selectAll" resultMap="BaseResultMap">
        SELECT uid, username, password, password_salt, user_identify, realname, user_status, create_time
        FROM sys_user
    </select>
</mapper>

(七)、ISysUserService.java

package com.itwopqq.booting.system.service;

import com.itwopqq.booting.system.model.SysUser;

import java.util.List;

public interface ISysUserService {

    List<SysUser> selectAll();
}

(八)、SysUserServiceImpl.java

package com.itwopqq.booting.system.serviceimpl;

import com.itwopqq.booting.system.mapper.SysUserMapper;
import com.itwopqq.booting.system.service.ISysUserService;
import com.itwopqq.booting.system.model.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fanzhen
 * @desx
 * @date 2020-03-29
 */
@Service
public class SysUserServiceImpl implements ISysUserService {

    @Autowired
    public SysUserMapper sysUserMapper;
    @Override
    public List<SysUser> selectAll() {
        return sysUserMapper.selectAll();
    }
}

(十)、SysUserController.java

package com.itwopqq.booting.system.controller;

import com.itwopqq.booting.system.model.SysUser;
import com.itwopqq.booting.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author fanzhen
 * @desx
 * @date 2020-03-29
 */
@RestController
@RequestMapping("/system/sysUser")
public class SysUserController {

    @Autowired
    public ISysUserService sysUserService;

    @GetMapping("/selectAll")
    public List<SysUser> selectAll(){

        List<SysUser> sysUsers = sysUserService.selectAll();
        return sysUsers;
    }
}

(十一)文件位置
在这里插入图片描述
(十二)、启动程序,访问测试路径:
在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值