springboot -- springboot中简单运用Mybatis

1、相关依赖

<dependency>
		<!--jdbc-->
		<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		
		<!--mybatis-->
		<dependency>
		    <groupId>org.mybatis.spring.boot</groupId>
		    <artifactId>mybatis-spring-boot-starter</artifactId>
		    <version>2.1.1</version>
		</dependency>
		
		<!--mysql-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.11</version>
		</dependency>

2、数据库连接(application.yml)

spring:
  datasource:
    username: root
    password: admin
    url: jdbc:mysql://localhost:3306/abc?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
    driver-class-name: com.mysql.cj.jdbc.Driver

一、使用注解

1、配置类添加mybatis-config.xml, mapper文件路径:

mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

2、创建一个Mapper接口类

package com.example.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.bean.Department;

public interface DepartmentMapper {
	@Select("select * from department where id=#{id}")
	public Department get(Integer id);
	
	@Delete("delete from department where id=#{id}")
	public Integer delete(Integer id);
	
	@Options(useGeneratedKeys = true,keyProperty = "id")
	@Insert("insert into department(departmentName) values(#{departmentName})")
	public Integer insert(Department department);
	
	@Update("update department set departmentName=#{departmentName} where id=#{id}")
	public Integer update(Department department);

}

2、在主程序上添加注解让spring扫描这个包下所有mapper类

@MapperScan(value=“com.example.mapper”)

package com.example;

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

@MapperScan(value="com.example.mapper")
@SpringBootApplication
public class MybatisApplication {

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

}

3、调用并使用

	@Autowired
	DepartmentMapper mapper;
	
	@ResponseBody
	@GetMapping("/get/{id}")
	public Department BB(@PathVariable("id")int id) {
		return mapper.get(id);
	}

二、使用配置文件

1、创建一个Mapper接口类

package com.example.mapper;

import com.example.bean.Employee;

public interface EmployeeMapper {
	
	public Employee get(int id);
	
	public int add(Employee employee);

}

2、创建mybatis的配置文件和实体类的SQL配置文件

在这里插入图片描述

mapper文件夹放置所有实体类mapper配置文件

mybatis文件夹放置mybatis的配置文件

mybatis-config.xml

<?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>
	//连接数据库相关操作之前已经在approperties.yml已经配置好, 以下连接数据库的操作可以省略
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
  //扫描mapper的操作之前在主程序已经添加了MapperScan, 所以也可以省略
  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  </mappers>
</configuration>

所以可以直接这样

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

</configuration>

EmployeeMapper.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.mapper.EmployeeMapper">
  <select id="get" resultType="com.example.bean.Employee">
    select * from employee where id = #{id}
  </select>
  
  <insert id="add">
  		insert into employee(lastName, email, gender, d_id) values (#{lastName}, #{email}, )#{gender}, #{d_id})
  </insert>
</mapper>

3、调用并使用

	@Autowired
	EmployeeMapper employeeMapper;
	
	@ResponseBody
	@GetMapping("/emp/{id}")
	public Employee getEmp(@PathVariable("id")int id){
		return employeeMapper.get(id);
	}

当然注解和配置文件两种方式可以一起使用

发布了52 篇原创文章 · 获赞 1 · 访问量 1762

猜你喜欢

转载自blog.csdn.net/qq_42039738/article/details/104033423
今日推荐