Spring Boot(3)整合Mybatis

如何使用Spring Boot一步一步搭建一个Web的基础工程……

安装Mysql

官方下载地址:https://dev.mysql.com/downloads/windows/ 自己去下载安装就行了。
新建数据库moxi和管理员表admin

Spring Boot(3)整合Mybatis_第1张图片
图片.png

工程加载Mybatis和Mysql的依赖

修改pom.xml



    4.0.0

    com.moxi
    moxi
    0.0.1-SNAPSHOT
    jar

    moxi
    mox

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.2.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-freemarker
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-devtools
        
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.2.0
        
        
        
            mysql
            mysql-connector-java
            runtime
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



或者在新建工程的时候直接勾选mybatis和mysql

Spring Boot(3)整合Mybatis_第2张图片
图片.png

建Model、Service和Controller

新建package:model,新建Admin类:

package com.moxi.model;

import java.sql.Date;

public class Admin {

    private long id;
    private String userName;
    private String password;
    private int age;
    private String phoneNumber;
    private String headPicture;
    private Date addDate;
    private Date updateDate;
    private int state;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getHeadPicture() {
        return headPicture;
    }

    public void setHeadPicture(String headPicture) {
        this.headPicture = headPicture;
    }

    public Date getAddDate() {
        return addDate;
    }

    public void setAddDate(Date addDate) {
        this.addDate = addDate;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    @Override
    public String toString() {
        return "Admin [id=" + id + ", userName=" + userName + ", password=" + password + ", age=" + age
                + ", phoneNumber=" + phoneNumber + ", headPicture=" + headPicture + ", addDate=" + addDate
                + ", updateDate=" + updateDate + ", state=" + state + "]";
    }

}

新建package:service,新建AdminService类:

package com.moxi.service;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.moxi.model.Admin;

@Mapper
public interface AdminService {

    @Select("SELECT * FROM `moxi`.`admin` where userName = #{userName} and password = #{password} and state = 0;")
    Admin findByNameAndPassword(Admin admin);

    @Select("SELECT * FROM `moxi`.`admin` where userName = #{userName} and password = #{password} and realName = #{realName}")
    List findByAdminWithPage(Admin admin, int start, int end);

    @Insert("INSERT INTO `moxi`.`admin` (`id`, `userName`, `password`, `realName`, `age`, `phoneNumber`, `headPicture`, `addDate`, `updateDate`, `state`) VALUES (null, #{userName}, #{password}, #{realName}, #{age}, #{phoneNumber}, #{headPicture}, now(), now(), 0);")
    int insert(Admin admin);

    @Update("UPDATE `moxi`.`admin` SET `userName` = #{userName}, `password` = #{password}, `realName` = #{realName}, `age` = #{age}, `phoneNumber` = #{phoneNumber}, `headPicture` = #{headPicture}, `updateDate` = now(), `state` = #{state} WHERE `id` = #{id};")
    int updateStateById(int id);

    @Delete("DELETE FROM `moxi`.`admin` WHERE id  = #{id}")
    int deleteById(int id);
}

application.properties文件中添加数据源配置:

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/moxi?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=Shu1shu2
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

新建package:controller,新建AdminController类:

package com.moxi.controller;


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

import com.moxi.model.Admin;
import com.moxi.service.AdminService;


@RestController
@RequestMapping("/admin")
public class AdminController {
    
    @Autowired
    private AdminService service;

    @RequestMapping("login")
    public Admin page1(Admin admin) {
        return service.findByNameAndPassword(admin);
    }
    
}

运行工程

访问页面:http://localhost:8080/admin/login?userName=admin&password=123456

Spring Boot(3)整合Mybatis_第3张图片
图片.png

数据库连接成功!!!

项目地址

https://github.com/daleiwang/moxi

Spring Boot(1)工具安装:

http://www.jianshu.com/p/fb6ed37c90eb

Spring Boot(2)新建Spring Boot工程

http://www.jianshu.com/p/00fd73f515f6

Spring Boot(3)整合Mybatis

http://www.jianshu.com/p/8401e9304fa0

Spring Boot(4)整合thymeleaf

http://www.jianshu.com/p/8d2cc7207fb2

Spring Boot(5)一个极简且完整的后台框架

http://www.jianshu.com/p/923d26d705ed

Spring Boot(6)jar方式打包发布

http://www.jianshu.com/p/9cf6faa8595e

Spring Boot(7)war方式打包发布

http://www.jianshu.com/p/ae170a58f88c

你可能感兴趣的:(Spring Boot(3)整合Mybatis)