Spring boot+Mybatis 框架搭建(一)

Java SSM框架即指Spring+SpringMVC+MyBatis的简称,框架集由Spring、MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容),常作为数据源较简单的web项目的框架。
相比于之前的SSH(Spring+Struts+Hibernate),SSM更加轻量化和灵活,是目前业界主流的Java Web开发框架。

一、新建maven工程(参考https://www.cnblogs.com/jiangger/p/12745235.html)

1.pom.xml引入依赖

 <!-->spring-boot启动+web启动<-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <!-->连接数据库配置<-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <!-->自动生成Getter/Setter方法<-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

  

 

2. resources/application.yml(配置本地访问端口,数据库相关信息) 

server:
 port: 8080

spring:
 datasource:
  driverClassName: com.mysql.jdbc.Driver
  url: jdbc:mysql://数据库库服务器:端口/数据库名称
  username: 用户名
  password: 密码

二、编写实体类(entry/UserInfo)

package Class4.entity;

import lombok.Getter;
import lombok.Setter;

/**
 * @author grjiang
 * @description:用户信息实体类,可以直接运用lombok里的Getter/Setter注解,也可以手动填写
 * @date 2021-07-02 18:51
 */
@Setter
@Getter
public class UserInfo {

    private Integer id;
    private Integer age;
    private String info;
    private String name;

//手动填写
//    public Integer getId() {
//        return id;
//    }
//
//    public void setId(Integer id) {
//        this.id = id;
//    }
//
//    public Integer getAge() {
//        return age;
//    }
//
//    public void setAge(Integer age) {
//        this.age = age;
//    }
//
//    public String getInfo() {
//        return info;
//    }
//
//    public void setInfo(String info) {
//        this.info = info;
//    }
//
//    public String getName() {
//        return name;
//    }
//
//    public void setName(String name) {
//        this.name = name;
//    }
}

 三、编写mapper(mapper/UserinfoMapper)

package Class4.mapper;

import Class4.entity.UserInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author grjiang
 * @description:mapper
 * @date 2021-07-02 18:47
 */

    @Mapper
    @Repository
    public interface UserinfoMapper {
        //查询语句+id用变量表示
        @Select({
                "select id, age, info, name from test_user_info where id = #{id,jdbcType=TINYINT}"
        })
     //查询语句返回结果映射到entry/UserInfo类里的变量 @Results({ @Result(column="id", property="id", jdbcType= JdbcType.TINYINT, id=true), @Result(column="age", property="age", jdbcType=JdbcType.INTEGER), @Result(column="info", property="info", jdbcType=JdbcType.VARCHAR), @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR) }) List<UserInfo> selectById(int id); //插入 @Insert({ "insert into test_user_info values(#{id,jdbcType=TINYINT},#{age,jdbcType=INTEGER},#{name,jdbcType=VARCHAR},#{info,jdbcType=VARCHAR})" }) @Results({ @Result(column="id", property="id", jdbcType= JdbcType.TINYINT, id=true), @Result(column="age", property="age", jdbcType=JdbcType.INTEGER), @Result(column="info", property="info", jdbcType=JdbcType.VARCHAR), @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR) }) int insert(int id, int age,String name,String info); //修改 @Update({ " update test_user_info set name='jiangger' where id= #{id,jdbcType=TINYINT}" }) @Results({ @Result(column="id", property="id", jdbcType= JdbcType.TINYINT, id=true), @Result(column="age", property="age", jdbcType=JdbcType.INTEGER), @Result(column="info", property="info", jdbcType=JdbcType.VARCHAR), @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR) }) int update(int id); }

四、编写业务控制层(controller/MsqlController),默认访问格式(以查询为例 http://localhost:8080/test/select?id=1)

package Class4.controller;

import Class4.entity.UserInfo;
import Class4.mapper.UserinfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.ResponseWrapper;
import java.util.List;

/**
 * @author grjiang
 * @description:实现
 * @date 2021-07-02 19:01
 */
@Controller
@RequestMapping("/test")
public class MysqlController {

    @Autowired
    UserinfoMapper userinfoMapper;

    @RequestMapping(value="/select",method= RequestMethod.GET)
    @ResponseBody
    public List<UserInfo> select(@RequestParam int id){
        return userinfoMapper.selectById(id);  //调用mapper里的查询语句,页面传入的id作为参数
    }

    @RequestMapping(value="/insert",method= RequestMethod.GET)
    @ResponseBody
    public int add(@RequestParam int id, int age,String name,String info){
       return userinfoMapper.insert(id,age,name,info);
    }

    @RequestMapping(value="/update",method= RequestMethod.GET)
    @ResponseBody
    public int add(@RequestParam int id){
        return userinfoMapper.update(id);
    }
}

 五、启动类(固定)--一个项目一个启动类

package Class4;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author grjiang
 * @description:启动类
 * @date 2021-07-02 19:27
 */

@SpringBootApplication
public class MySpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringbootApplication.class, args);
    }

}

 五、执行启动类将服务启动,浏览器输入网址即可查看数据库数据

 

原文地址:https://www.cnblogs.com/jiangger/p/14971271.html