SpringBoot整合mybatis

一、创建一个spring项目
在这里插入图片描述
二、添加依赖

<?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.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.jt</groupId>
	<artifactId>springboot_demo2_mybatis</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot_demo2_mybatis</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-web</artifactId>
		</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>

		<!--引入插件lombok 自动的set/get/构造方法插件  -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>

		<!--引入数据库驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>

		<!--springBoot数据库连接  -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>

		<!--spring整合mybatis-plus -->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.2.0</version>
		</dependency>
	</dependencies>

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

</project>

三、配置yml文件

server:
  port: 8090    #定义端口
  servlet:
    context-path: /   #定义项目的发布路径
spring:
  datasource:
    #driver-class-name: com.mysql.cj.jdbc.Driver   springboot程序采用默认的配置
    url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    username: root
    password: root

mybatis-plus:
  #定义别名包
  type-aliases-package: com.jt.pojo
  #加载user标签的mapper文件
  mapper-locations: classpath:/mybatis/mappers/*.xml
  #开启驼峰映射
  configuration:
    map-underscore-to-camel-case: true

#引入日志信息.
logging:
  level:
    com.jt.dao: debug

四、编辑pojo对象

package com.jt.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
@Data
@Accessors(chain = true)
@TableName//将对象与表,进行一对一关联
public class User implements Serializable {

    @TableId(type = IdType.AUTO)//主键信息,设定自增
    private Integer id;
   // @TableId(value = "name")//如果字段名称与属性名称一致,可以省略不写
    private String name;
    private Integer age;
    private String sex;
}

五、在启动类添加注解

package com.jt;

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

@SpringBootApplication
@MapperScan("com.jt.dao")//告诉mapper的包路径,自动完成包扫描
public class SpringbootDemo2MybatisApplication {

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

}

六、编辑接口

package com.jt.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jt.pojo.User;
import org.apache.ibatis.annotations.Select;

import java.util.List;
//@Mapper
public interface UserDao extends BaseMapper<User> {
    //查询user表的所有记录
    @Select("select * from user")
    List<User> findAll();
}

}

五、编辑测试类

package com.jt;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jt.dao.UserDao;
import com.jt.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@SpringBootTest
class SpringbootDemo2MybatisApplicationTests {
	@Autowired
	private UserDao userDao;
	@Test
	void testFind() {
		List<User> userList = userDao.findAll();
		System.out.println(userList);
	}
	//查询所有数据
	@Test
	public void testSelect01(){
		List<User> userList=userDao.selectList(null);
		System.out.println(userList);
	}

	/**
	 * 业务:
	 * 查询id=11的用户信息   主键
	 * select * from user where id=11
	 */
	@Test
	public void testSelect02(){
		User user =userDao.selectById(11);
		System.out.println(user);
	}
	/**
	 * 业务:
	 * 查询name属性为小乔的数据
	 */
	@Test
	public void testSelect03(){
		QueryWrapper queryWrapper=new QueryWrapper<User>();
		queryWrapper.eq("name","小乔" );
		List<User> userList=userDao.selectList(queryWrapper);
		System.out.println(userList);
	}
	/**
	 * 业务:
	 * 查询name属性为小乔的数据,并且age>=18
	 *
	 */
	@Test
	public void testSelect04(){
		QueryWrapper<User> queryWrapper =new QueryWrapper<>();
		queryWrapper.eq("name", "小乔").ge("age",18);
		List<User> userList=userDao.selectList(queryWrapper);
		System.out.println(userList);
	}
	/**
	 * 业务:
	 * 	业务: 查询name中包含 "精"的用户,并且sex为女
	 * 	业务: 查询name中包含 以精结尾的数据,并且sex为女
	 * likeLeft 已什么结尾
	 * 大于 > gt
	 * 小于 < lt
	 * 等于 = eq
	 * 大于等于 >=ge
	 * 小于等于 <=le
	 */
	@Test
	public void testSelect05(){
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.likeLeft("name", "精").eq("sex", "女");
		List<User> userList = userDao.selectList(queryWrapper);
		System.out.println(userList);
	}
	/**
	 * 需求: 查询user表中的数据 要求按照年龄降序排列,如果年龄相同按照id降序排列
	 */
	@Test
	public  void  testSelect06(){
		QueryWrapper<User> queryWrapper=new QueryWrapper<>();
		queryWrapper.orderByDesc("age","id");
		List<User> userList=userDao.selectList(queryWrapper);
		System.out.println(userList);


	}
	/**
	 * 需求: 查询name属性为null的数据.
	 */
	@Test
	public  void  testSelect07(){
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.isNull("name");
		List<User> userList = userDao.selectList(queryWrapper);
		System.out.println(userList);
	}
	/**
	 * * 查询name="小乔" age=17 性别=女的用户
	 * 	 * 如果传递的是对象.会根据对象中不为null的属性充当where条件.
	 */
	@Test
	public  void  testSelect08(){
		User user = new User();
		user.setName("小乔");
		user.setAge(17);
		user.setSex("女");
		QueryWrapper<User> queryWrapper = new QueryWrapper<>(user);
		List<User> userList = userDao.selectList(queryWrapper);
		System.out.println(userList);

	}
	/**
	 *  查询id=1,3,5,10数据.
	 */
	@Test
	public  void  testSelect09() {
		Integer [] ids={1,3,5,10};
		List<Integer> idList = Arrays.asList(ids);//“Arrays.asList的作用是将数组转化为list,
		List<User> userList =userDao.selectBatchIds(idList);
		System.out.println(userList);
	}
	/**
	 * 添加特朗普 年纪70 性别男
	 */
	@Test
	public  void  testInsert(){
	User user = new User();
	user.setName("特朗普");
	user.setSex("人妖");
	user.setAge(70);
	userDao.insert(user);
	}

	/**
	 * 删除name=null的数据,或者name="特朗普"
	 * delete from user where name is null or name="特朗普";
	 */
	@Test
	public  void  testDelete(){
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.isNull("name").or().eq("name", "特朗普");
		 userDao.delete(queryWrapper);
	}
	/**
	 * 	 例1: 将id=1的数据的年龄改为8000岁.
	 * 	 sql1:  update user set age=8000 where id=1;
	 * 	 案例2: 要求将name="黑熊精" age=5500.
	 * 	 sql2:  update user set age=5500 where name="黑熊精";
	 */
	@Test
	public void testUpdate(){
		User user = new User();
		user.setId(1);
		user.setAge(8000);
		userDao.updateById(user);

		User temp = new User();
		temp.setAge(5500);
		UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("name", "黑熊精");
		userDao.update(temp, updateWrapper);

	}


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值