springboot从零开始[1] gradle+mybatis注解

来源:互联网 发布:debian mysql 编辑:程序博客网 时间:2024/06/07 14:05

springboot从零开始[1] gradle+mybatis注解

从来都没接触过过,各方面消息都说springboot是个好东西,那就开始学吧,好东西就是要get!
毕竟小白,从零开始学习,一步步慢慢来吧,然后尽量做到具体。

先来看看全部的结构,是很简单的结构,因为入门嘛

目录结构

下面开始动手了!

  1. 当然是建表
CREATE TABLE users (  id VARCHAR (36) NOT NULL PRIMARY KEY,  user_name VARCHAR (36) NOT NULL,  PASSWORD VARCHAR (100) NOT NULL,  create_date DATETIME NULL,  create_by VARCHAR (36) NULL) ;
  1. application.properties,这里设置了数据库连接地址及日志级别
spring.datasource.url=jdbc:mysql://localhost:3306/castlespring.datasource.username=rootspring.datasource.password=1234spring.datasource.driverClassName=com.mysql.jdbc.Drivermybatis.config-location=classpath:mybatis-config.xmllogging.level.root=INFOlogging.level.sample.mybatis.mapper=DEBUG

上面的application.properties中有个mybatis.config-location,是为mybatis做更细致的设置

<?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>        <!-- 使用驼峰命名法转换字段。 -->        <setting name="mapUnderscoreToCamelCase" value="true"/>    </settings></configuration>

比如必要的驼峰转换~

  1. 码代码了
    实体User
package top.aircastle.springboot.comment.domin;/** * Created by Castle on 2017/8/9. */public class User {    private String id;    private String userName;    private String password;    private String createDate;    private String createBy;    public String getId() {        return id;    }    public void setId(String 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 String getCreateDate() {        return createDate;    }    public void setCreateDate(String createDate) {        this.createDate = createDate;    }    public String getCreateBy() {        return createBy;    }    public void setCreateBy(String createBy) {        this.createBy = createBy;    }}

Mapper文件UserMapper

package top.aircastle.springboot.comment.mapper;import org.apache.ibatis.annotations.*;import top.aircastle.springboot.comment.domin.User;import java.util.List;/** * Created by Castle on 2017/8/9. */@Mapperpublic interface UserMapper {    @Select(value = {"SELECT * FROM user WHERE user_name = #{userName}"})    List<User> getUserByUserName(String userName);    @Select(value = { "SELECT * FROM user" })    List<User> listAllUser();    @Select(value = "INSERT INTO USER (id,user_name,PASSWORD,create_date,create_by) VALUES (REPLACE(UUID(), '-', ''),'test0','1234',NOW(),'test')")    int addUser(User user);}

控制器UserController

package top.aircastle.springboot.comment.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import top.aircastle.springboot.comment.domin.User;import top.aircastle.springboot.comment.mapper.UserMapper;import java.util.List;/** * Created by Castle on 2017/8/9. */@RestController@RequestMapping("/user")public class UserController {    @Autowired    UserMapper userMapper;    @RequestMapping(value = "getUserByUserName", method = RequestMethod.POST)    public List<User> getUserByUserName(@RequestParam(name = "userName", required = false) String userName) {        List<User> data = userMapper.getUserByUserName(userName);        return data;    }    @RequestMapping(value = "listAllUser", method = RequestMethod.POST)    List<User> listAllUser() {        List<User> data = userMapper.listAllUser();        return data;    }    @RequestMapping(value = "addUser", method = RequestMethod.POST)    int listAllUser(@RequestBody User user) {        int data = userMapper.addUser(user);        return data;    }}
  1. SampleApplication启动类
package top.aircastle.springboot.comment;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class SampleApplication {    public static void main(String[] args) {        SpringApplication.run(SampleApplication.class, args);    }}

现在就可以开始运行了,下面我贴张自测的截图吧
接口调用结果

原创粉丝点击