spring Boot+Mybatis-Plus+Maven简单配置

  • Post author:
  • Post category:其他


1、eclipse-new–》Spring Starter Project,或者new–》other…..–》搜索spring–》Spring Starter Project

2、选择jdk版本(我的是jdk1.8),打包类型(jar),语言(java)

3、选择导入的springboot版本以及所需jar,可以省略此步骤,点击finish

IDEA同理



4、eclipse创建的SpringBoot项目pom.xml的第一行也许会报错,加上以下代码,之后选中项目右键,maven–》update project

<properties>
    <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>

5、添加所需jar,pom.xml中如果存在则不需要再次引入

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.2</version>
		<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- java web驱动 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- Oracle驱动 -->
		<dependency>
			<groupId>com.oracle.ojdbc</groupId>
			<artifactId>ojdbc8</artifactId>
			<scope>runtime</scope>
		</dependency>
		<!-- mybatisplus -->
		<dependency>
	        <groupId>com.baomidou</groupId>
	        <artifactId>mybatis-plus-boot-starter</artifactId>
	        <version>3.3.2</version>
	    </dependency>
	    <!-- alibaba JSON -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.31</version>
		</dependency>
		<!-- net.sf.json -->
		<dependency>    
		    <groupId>net.sf.json-lib</groupId>    
		    <artifactId>json-lib</artifactId>    
		    <version>2.4</version>    
		    <classifier>jdk15</classifier>
		</dependency>
		<!-- druid数据库连接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.9</version>
		</dependency>
		<!-- 解决Oracle中文字符集问题 -->
		<dependency>
			<groupId>cn.easyproject</groupId>
			<artifactId>orai18n</artifactId>
			<version>12.1.0.2.0</version>
		</dependency>
</dependencies>
<!-- SpringBoot打jar包所需插件 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

6、如图所示,创建config包(自定义组件类,修改或者添加springboot的组件),controller包,entity包,mapper包,service包,自然,在src/main/java包下的任意目录下创建都可

7、打开SpringBoot启动类(类名都带**Application.java),在@SpringBootApplication 下添加 @MapperScan 和 @ComponentScan 注解,其中@MapperScan 是告知SpringBoot mapper接口所在位置

8、修改application.properties或者是application.yml(我的是yml文件)

application.yml

#数据库配置
spring:
  datasource:
    driver-class-name: oracle.jdbc.driver.OracleDriver
    url: jdbc:oracle:thin:@ip:port:数据库名称
    username: 用户名
    password: 密码
    type: com.alibaba.druid.pool.DruidDataSource
    
#Mybatis-Plus配置
mybatis-plus:
  configuration:
    #是否将sql打印到控制面板(该配置会将sql语句和查询的结果都打印到控制台)
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

application.properties

#数据库配置
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@ip:port:数据库名称
spring.datasource.username=用户名
spring.datasource.password=密码
#配置数据库连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

9、新增组件

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
import com.baomidou.mybatisplus.extension.incrementer.OracleKeyGenerator;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;

@Configuration
public class MyBatisPlusConfig {

	/**
	 * Mybatis-Plus 分页插件
	 */
	@Bean
	public PaginationInterceptor paginationInterceptor() {
		PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
		// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
		// paginationInterceptor.setOverflow(false);
		// 设置最大单页限制数量,默认 500 条,-1 不受限制
		// paginationInterceptor.setLimit(500);
		return paginationInterceptor;
	}

	/**
	 * Oracle主键生成策略
	 */
	@Bean
	public IKeyGenerator keyGenerator() {
		return new OracleKeyGenerator();
	}
}

10、实体类编写

import java.io.Serializable;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;


@TableName("LP_USER")
//Oracle主键新增所需序列,clazz为主键类型
@KeySequence(value = "SEQ_LP_USER", clazz = Integer.class)
public class LpUser implements Serializable {

    private static final long serialVersionUID = 1L;

    //IdType.INPUT 生成ID策略
	@TableId(value = "ID", type = IdType.INPUT)
	private Integer id;
    /**
     * 姓名
     */
	@TableField("USER_NAME")
	private String userName;
    /**
     * 性别 0:女 1:男
     */
    @TableField("SEX")
    private Integer sex;
    /**
     * 年龄
     */
    @TableField("AGE")
    private Integer age;


	public Integer getId() {
        return id;
    }

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


	public String getUserName() {
		return userName;
	}

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

	public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Integer getAge() {
        return age;
    }

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

    @Override
    public String toString() {
//		return JSONObject.fromObject(this).toString();
		return JSONObject.toJSONString(this);
    }

}

11、mapper包

12、service包

13、测试 src/test/java 下 有个测试类


import java.util.List;

import javax.annotation.Resource;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxit.springboot.entity.LpUser;
import com.sxit.springboot.mapper.LpUserMapper;
import com.sxit.springboot.service.LpUserService;

import net.sf.json.JSONObject;

@SpringBootTest
class SpringbootMybatisPlusApplicationTests {

	@Resource
	LpUserMapper userMapper;
	@Resource
	LpUserService service;

	// 测试查询所有
	@Test
	void testSelect() {
		List<LpUser> list = userMapper.selectList(null);
		System.out.println("=========" + list.toString());
		List<LpUser> list2 = service.list();
		System.out.println("=========" + list2.toString());
	}

	// 测试byId查询
	@Test
	void testSelectById() {
		LpUser byId = userMapper.selectById(4);
		System.out.println("=============" + byId);
		LpUser byId2 = service.getById(4);
		System.out.println("===========" + byId2);
		QueryWrapper<LpUser> queryWrapper = new QueryWrapper<LpUser>();
		queryWrapper.eq("ID", 5);
		LpUser selectOne = userMapper.selectOne(queryWrapper);
		System.out.println("=============" + selectOne);
		LpUser one = service.getOne(queryWrapper);
		System.out.println("=============" + one);
		List<LpUser> list = service.list(queryWrapper);
		System.out.println("============" + list);
	}

	// 测试插入,需要引入主键生成策略组件
	@Test
	void testInsert() {
		LpUser entity = new LpUser();
		entity.setUserName("用户名");
		entity.setSex(1);
//		int insert = userMapper.insert(entity);
//		System.out.println("============" + insert);
		boolean save = service.save(entity);
		System.out.println("==============" + save);
	}

	// 测试修改
	@Test
	void testUpdate() {
		LpUser lpUser = service.getById(4);
//		lpUser.setUserName("111");
		lpUser.setAge(8);
//		int updateById = userMapper.updateById(lpUser);
//		System.out.println("===========" + updateById);
//		QueryWrapper<LpUser> wrapper = new QueryWrapper<LpUser>();
//		wrapper.eq("ID", 4);
//		boolean update = service.update(lpUser, wrapper);
//		System.out.println("===========" + update);
		UpdateWrapper<LpUser> updateWrapper = new UpdateWrapper<LpUser>();
		updateWrapper.eq("ID", 4);
		boolean update = service.update(lpUser, updateWrapper);
		System.out.println("============" + update);
	}

	// 测试删除
	@Test
	void testDel() {
//		QueryWrapper<LpUser> queryWrapper = new QueryWrapper<LpUser>();
//		queryWrapper.eq("ID", 6);
//		boolean remove = service.remove(queryWrapper);
//		System.out.println("=============" + remove);
		QueryWrapper<LpUser> wrapper = new QueryWrapper<LpUser>();
		wrapper.eq("ID", 6);
		int delete = userMapper.delete(wrapper);
		System.out.println("==========" + delete);
	}

	// 测试分页查询,需要手动引入分页组件
	@Test
	void testPage() {
		Page<LpUser> page = new Page<LpUser>(1, 10);
		QueryWrapper<LpUser> queryWrapper = new QueryWrapper<LpUser>();
		page = service.page(page, queryWrapper);
		System.out.println("===========" + JSONObject.fromObject(page).toString());
	}

}



版权声明:本文为a990914093原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。