springboot整合mybatis(有效)<xml式crud>

1、创建springboot工程

2、添加依赖(和步骤1同时进行)

<?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.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.mybatis</groupId>
	<artifactId>mtbatis-study</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>mybatis-study</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
<!--		日志显示-->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>

		<!-- 热部署 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
			<scope>true</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
<!--		引入mybatis整合springboot-->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
	</dependencies>

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


		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.yml</include>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
				</includes>
				<filtering>false</filtering>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.yml</include>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
				</includes>
				<filtering>false</filtering>
			</resource>
		</resources>
	</build>

</project>

3、在resource目录添加映射文件

4、配置文件

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis-study?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
    username: root
    password: xxxxx
server:
  port: 8083
#映射文件地址
mybatis:
  mapper-locations: classpath:/mappers/*.xml

5、编写测试(在此只演示查询操作

  • 编写xxxMapper.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.mybatis.study.dao.IEmployeeMapper">
        
        <select id="findEmployeeById" resultType="com.mybatis.study.entity.Employee">
            select id,last_name lastName,gender,email from employee where id=#{id}
        </select>
        
    </mapper>
  • 编写pojo实体类

        

package com.mybatis.study.entity;

/**
 * @author zhencheng
 * @create 2021-09-07 11:29
 * @desc 1
 */
public class Employee {
    private Integer id;

    private String lastName;

    private String gender;

    private String email;

    public Employee() {
    }

    public Employee(Integer id, String lastName, String gender, String email) {
        this.id = id;
        this.lastName = lastName;
        this.gender = gender;
        this.email = email;
    }

    public Integer getId() {
        return id;
    }

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

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", lastName='" + lastName + '\'' +
                ", gender='" + gender + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}
  • 编写dao层文件
package com.mybatis.study.dao;

import com.mybatis.study.entity.Employee;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

/**
 * @author zhencheng
 * @create 2021-09-07 11:29
 * @desc 1
 */
@Mapper
@Repository
//上述两个注解可以你不写,在主启动类添加包扫描,视情况二选一,如下
//@MapperScan(basePackages = "com.mybatis.study.dao")<添加在主启动类上>
public interface IEmployeeMapper {

    public Employee findEmployeeById(@Param("id") Integer id);

}
  • 编写service层文件

        

package com.mybatis.study.service;

import com.mybatis.study.entity.Employee;

/**
 * @author zhencheng
 * @create 2021-09-07 11:27
 * @desc 1
 */
public interface IEmployee {

    Employee selectById(Integer id);//方法名和映射文件id对应
}
package com.mybatis.study.service.impl;

import com.mybatis.study.dao.IEmployeeMapper;
import com.mybatis.study.entity.Employee;
import com.mybatis.study.service.IEmployee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhencheng
 * @create 2021-09-07 11:28
 * @desc 1
 */
@Service
public class EmployeeImpl implements IEmployee {

    @Autowired
    private IEmployeeMapper iEmployeeMapper;

    @Override
    public Employee selectById(Integer id) {
        return iEmployeeMapper.findEmployeeById(id);
    }
}
  • 编写controller层文件
package com.mybatis.study.controller;
import com.mybatis.study.entity.Employee;
import com.mybatis.study.service.IEmployee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("mybatis/")
public class EmployeeController {
	
	@Autowired
	private IEmployee iEmployee;

	@RequestMapping("hello")
	public String sayHello(){
		return "hello success-";
	}

	@RequestMapping("selectById")
	public Employee selectById(Integer id){

		return iEmployee.selectById(id);
	}

}
  • 访问测试

访问locahost :8083/mybatis/selectById?id=1

 成功。。。。。。。。over

自己记下:(勿理)

开启idea日志栏颜色

在run->debug->edit configuration-> VM options 添加

-Dspring.output.ansi.enabled=ALWAYS

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值