springboot2.1入门系列五 springboot项目使用Mybatis

本文为Spring Boot2.1系列的第五篇,代码可以从github下载 https://github.com/yinww/demo-springboot2.git

本章介绍Springboot 使用Mybatis。

一、准备数据库

create database demo005;

create table user
(
   id                   bigint not null,
   name            varchar(64),
   primary key (id)
)
ENGINE = InnoDB
DEFAULT CHARSET = utf8;

二、创建工程demo005

pom.xml的内容为

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.yinww</groupId>
        <artifactId>demo-springboot2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>demo005</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>

三、Java代码

主类

package com.yinww.demo.springboot2.demo005;

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

@SpringBootApplication
public class Demo005Application {

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

}

User类

package com.yinww.demo.springboot2.demo005.domain;

public class User {

    private Long id;
    private String name;

    // getter and setter

}

UserMapper

package com.yinww.demo.springboot2.demo005.mapper;

import org.apache.ibatis.annotations.Mapper;

import com.yinww.demo.springboot2.demo005.domain.User;

@Mapper
public interface UserMapper {

    void addUser(User user);

}

UserService

package com.yinww.demo.springboot2.demo005.service;

import com.yinww.demo.springboot2.demo005.domain.User;

public interface UserService {
    
    void addUser(User user);

}

UserServiceImpl

package com.yinww.demo.springboot2.demo005.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yinww.demo.springboot2.demo005.domain.User;
import com.yinww.demo.springboot2.demo005.mapper.UserMapper;
import com.yinww.demo.springboot2.demo005.service.UserService;

@Service
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserMapper userMapper;

    @Override
    public void addUser(User user) {
        userMapper.addUser(user);
    }

}

Controller, 提供了一个存储数据的接口

package com.yinww.demo.springboot2.demo005.controller;

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

import com.yinww.demo.springboot2.demo005.domain.User;
import com.yinww.demo.springboot2.demo005.service.UserService;

@RestController
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @RequestMapping("add")
    public Object addUser() {
        User user = new User();
        user.setId(new Long(1));
        user.setName("张三丰");
        userService.addUser(user);
        return "ok";
    }

}

四、配置文件

UserMapper.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.yinww.demo.springboot2.demo005.mapper.UserMapper">
    <resultMap id="UserResultMap" type="com.yinww.demo.springboot2.demo005.domain.User">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
    </resultMap>

    <insert id="addUser">
        insert into user (id, name) values(#{id}, #{name})
    </insert>
</mapper>

application.properties 主要配置redis

spring.application.name=demo005

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demo005?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root

mybatis.typeAliasesPackage=com.yinww.demo.springboot2.demo005.domain
mybatis.mapperLocations=classpath:mapper/*.xml

 

五、运行

执行

java -jar demo005-0.0.1-SNAPSHOT.jar

也可以以其他方式启动。

添加数据

查看数据库,可以看到数据已经保存成功

mysql> select * from user;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | 张三丰    |
+----+-----------+
1 row in set (0.00 sec)

本文内容到此结束,更多内容可关注公众号:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值