• SpringBoot集成Mybatis


    1、创建配置文件,并添加数据库连接信息

      application.properties形式添加数据库连接信息

    spring.datasource.driverClassName=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/jeff
    spring.datasource.username=root
    spring.datasource.password=mysql

      application.yml形式添加数据库连接信息

    #配置数据库
    spring: 
      datasource: 
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/jeff
        username: root
        password: mysql

    2、在pom.xml中引入Mybatis的jar包

            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.1</version>
            </dependency>
            
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>

    3、创建数据库(jeff),创建用户表(sys_user),并添加数据

    4、创建用户实体类(User)

    package com.jeff.entity;
    
    import java.util.Date;
    
    import com.fasterxml.jackson.annotation.JsonFormat;
    
    public class User {
    
        // 主键
        private long id;
    
        // 登陆名
        private String loginName;
    
        // 密码
        private String password;
    
        //
        private String salt;
    
        // 性别(0:保密;1:男;2:女)
        private int sex;
    
        // 状态(0:有效;1:无效)
        private int status;
    
        // 真实姓名
        private String name;
    
        // 昵称
        private String nickName;
    
        // 手机号
        private String phone;
    
        // 邮箱
        private String email;
    
        // 生日
        @JsonFormat(pattern = "yyyy-MM-dd")
        private Date birthday;
    
        // 用户头像
        private String headimgUrl;
    
        // 创建时间
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private Date createTime;
    
        // 创建人
        private String createName;
    
        // 修改时间
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private Date modifyTime;
    
        // 修改人
        private String modifyName;
    
        public long getId() {
            return id;
        }
    
        public void setId(long id) {
            this.id = id;
        }
    
        public String getLoginName() {
            return loginName;
        }
    
        public void setLoginName(String loginName) {
            this.loginName = loginName;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getSalt() {
            return salt;
        }
    
        public void setSalt(String salt) {
            this.salt = salt;
        }
    
        public int getSex() {
            return sex;
        }
    
        public void setSex(int sex) {
            this.sex = sex;
        }
    
        public int getStatus() {
            return status;
        }
    
        public void setStatus(int status) {
            this.status = status;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getNickName() {
            return nickName;
        }
    
        public void setNickName(String nickName) {
            this.nickName = nickName;
        }
    
        public String getPhone() {
            return phone;
        }
    
        public void setPhone(String phone) {
            this.phone = phone;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public Date getBirthday() {
            return birthday;
        }
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    
        public String getHeadimgUrl() {
            return headimgUrl;
        }
    
        public void setHeadimgUrl(String headimgUrl) {
            this.headimgUrl = headimgUrl;
        }
    
        public Date getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
    
        public String getCreateName() {
            return createName;
        }
    
        public void setCreateName(String createName) {
            this.createName = createName;
        }
    
        public Date getModifyTime() {
            return modifyTime;
        }
    
        public void setModifyTime(Date modifyTime) {
            this.modifyTime = modifyTime;
        }
    
        public String getModifyName() {
            return modifyName;
        }
    
        public void setModifyName(String modifyName) {
            this.modifyName = modifyName;
        }
    
    }

    5、创建UserMapper

    package com.jeff.mapper;
    
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.Select;
    
    import com.jeff.entity.User;
    
    @Mapper
    public interface UserMapper {
        
        @Select("select * from sys_user where id=#{id}")
        User getUserById(@Param("id") Long id);
    
    }

    6、创建UserService

    package com.jeff.service;
    
    import com.jeff.entity.User;
    
    public interface UserService {
    
        User getUserById(Long id);
    
    }

    7、创建UserServiceImpl

    package com.jeff.service.impl;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.jeff.entity.User;
    import com.jeff.mapper.UserMapper;
    import com.jeff.service.UserService;
    
    @Service
    public class UserServiceImpl implements UserService {
        
        @Autowired
        private UserMapper mapper;
    
        @Override
        public User getUserById(Long id) {
            
            return mapper.getUserById(id);
        }
    
    }

    8、创建UserController并添加getUserById方法

    package com.jeff.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.jeff.entity.User;
    import com.jeff.service.UserService;
    
    @RestController
    @RequestMapping("user")
    public class UserController {
        
        @Autowired
        private UserService service;
    
        @RequestMapping("getUserById")
        public User getUserById(Long id) {
            
            return service.getUserById(id);
        }
    
    }

    7、测试getUserById方法 http://localhost:8080/user/getUserById?id=1

  • 相关阅读:
    MySQL的Date()函数拼接
    org.osgi.framework.BundleException: Exception in org.eclipse.core.resources.ResourcesPlugin.start()
    js判断对象是否为空对象的几种方法
    json,js中typeof用法详细介绍及NaN、 null 及 undefined 的区别
    将[object Object]转换成json对象
    升级d7的代码到2010以上版本注意事项(SetLength的参数就是字符长度,而不是字节长度,但Move函数要改)
    我是如何用 10 天自学编程,改变一生的?(学习编程的时候,不要死记硬背,要培养感觉)
    Anbox —— 在 Linux 系统中运行 Android 应用
    一定要在commit之前做RAR备份,这样在出问题的时候,可以排除别人代码的干扰
    排序算法总结
  • 原文地址:https://www.cnblogs.com/jiefu/p/10023494.html
Copyright © 2020-2023  润新知