springBoot整合Mybatis简单的crud操作

搭建一个简单的SpringBoot整合Mybatis的crud的简单项目

一、到springboot官网下载一个基础项目

      https://start.spring.io/下载一个dome

二、下载后可以改一个适合的名字,直接改文件名就可以了。

三、导入项目到idea

       1.启动项目

        2.启动项目之后会发现,可以启动,但是启动之后就停下来了,那是因为少了web包。

        

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

    加入后就可以了。

四、整合Mybatis

 1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>weChartApp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>weChartApp</name>
	<description>weChartApp project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<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>

		<dependency>
			<groupId>org.thymeleaf.extras</groupId>
			<artifactId>thymeleaf-extras-java8time</artifactId>
		</dependency>

		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-typehandlers-jsr310</artifactId>
			<version>1.0.2</version>
		</dependency>

		<!--mysql的依赖-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>

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


		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

2.springboot配置application.yml

server:
  port: 8080

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/gobiln?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver



mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    auto-mapping-behavior: partial
    map-underscore-to-camel-case: true
  mapper-locations: classpath:mapping/*Mapper.xml
  type-aliases-package: com.example.demo.entity


   3.    mybatis的mapping配置

         resources下新建mapping目录,在mapping下新建文件Demomapper.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.example.demo.mapper.DemoMapper">

    <resultMap id="BaseResultMap" type="com.example.demo.entity.Demo">
        <result column="id" jdbcType="INTEGER" property="id" />
        <result column="age" jdbcType="INTEGER" property="age" />
        <result column="user_name" jdbcType="VARCHAR" property="userName" />
        <result column="text" jdbcType="VARCHAR" property="text" />
        <result column="startTime" jdbcType="DATE" property="start_time" />
        <result column="money" jdbcType="DECIMAL" property="money" />
    </resultMap>

    <select id="Sel" resultType="com.example.demo.entity.Demo">
        select * from demo where id = #{id}
    </select>

    <select id="SelAll" resultType="com.example.demo.entity.Demo">
        select * from demo
    </select>

    <select id="SelUserName" resultType="com.example.demo.entity.Demo">
        select * from demo where user_name like $%{value}%
    </select>

    <insert id="insert" parameterType="com.example.demo.entity.Demo">
        insert  into  demo(user_name,text) values(#{userName},#{text})
    </insert>

    <delete id="deleteId" parameterType="int">
        delete from demo where id = #{id}
    </delete>

    <update id="update" parameterType="com.example.demo.entity.Demo">
        update demo set user_name = #{userName} where id = #{id}
    </update>

</mapper>

4.代码结构

 

package com.example.demo.controller;

import com.example.demo.entity.Demo;
import com.example.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @Autowired
    private DemoService demoService;

    @RequestMapping("/demo")
    public Demo getDemo(@RequestParam int id){
        Demo demo = demoService.getDemo(id);
        System.out.println(demo);
        return demoService.getDemo(id);
    }
    @RequestMapping("/demo/insert")
    public void insert(@RequestParam String text ,String userName){
        Demo demo = new Demo();
        demo.setText(text);
        demo.setUserName(userName);
        demoService.insert(demo);
    }
    @RequestMapping("/demo/deleteId")
    public void deleteId(int id){
        demoService.deleteId(id);
    }
    @RequestMapping("/demo/update")
    public void update(@RequestParam int id ,String userName){
        Demo demo = new Demo();
        demo.setId(id);
        demo.setUserName(userName);
        demoService.update(demo);
    }
}
package com.example.demo.entity;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.time.LocalDateTime;
import java.util.Date;

public class Demo {
    private Integer id;
    private String userName;
    private String text;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime startTime;
    private Double money;
    private Integer age;

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

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setText(String text) {
        this.text = text;
    }

    public void setStartTime(LocalDateTime startTime) {
        this.startTime = startTime;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

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

    public Integer getId() {
        return id;
    }

    public String getUserName() {
        return userName;
    }

    public String getText() {
        return text;
    }

    public LocalDateTime getStartTime() {
        return startTime;
    }

    public Double getMoney() {
        return money;
    }

    public Integer getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Demo{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", text='" + text + '\'' +
                ", startTime=" + startTime +
                ", money=" + money +
                ", age=" + age +
                '}';
    }
}
package com.example.demo.mapper;

import com.example.demo.entity.Demo;
import org.springframework.stereotype.Repository;

@Repository
public interface DemoMapper {

    Demo Sel(int id);

    void insert(Demo demo);

    void deleteId(int id);

    void update(Demo demo);
}
package com.example.demo.service;

import com.example.demo.entity.Demo;
import com.example.demo.mapper.DemoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DemoService {

    @Autowired
    private DemoMapper demoMapper;

    public Demo getDemo(int id){
        return demoMapper.Sel(id);
    }

    public void insert(Demo demo){
        demoMapper.insert(demo);
    }

    public void deleteId(int id){
        demoMapper.deleteId(id);
    }

    public void update(Demo demo){
        demoMapper.update(demo);
    }
}
package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class DemoApplication {

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

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值