快速搭建springboot+mybatis+postgresql开发环境

    这里,利用eclipse或者idea提供的springboot项目创建向导,不用去找依赖。

    普通的eclipse需要安装spring插件。可以直接使用sts版本。全称是Spring Tools Suite。在eclipse下,新建->Spring Starter Project,跟着向导,选择项目位置,填写项目名称。

    下一步,选择Spring Web,MyBatis Framework,PostgreSQL Driver三个依赖即可。

    pom.xml主要内容,其中junit是手动添加:

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.5.6</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.xx</groupId>
	<artifactId>springmybatis</artifactId>
	<version>1.0</version>
	<name>springmybatis</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.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.2.0</version>
		</dependency>

		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
		
	</dependencies>

    这里在eclipse下配置文件默认是properties后缀类型,这个没关系。

    application.properties

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://192.168.61.150:5432/webapp?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=postgres
spring.datasource.password=postgres
logging.level.com.xx.mybatis.dao=Debug

    接着,添加实体类与dao接口。

        UserBean.java

package com.xx.mybatis.domain;

public class UserBean {
	private int id;
	private String name ;
	private String mobile ;
	
	public void setId(int id) {
		this.id = id;
	}
	
	public int getId() {
		return id;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	
	@Override
	public String toString() {
		return "user[id="+id+",name="+name+",mobile="+mobile+"]";
	}
}

    dao接口:

package com.xx.mybatis.dao;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import com.xx.mybatis.domain.UserBean;

@Mapper
public interface UserMapper {
	
	@Select(value = "select * from xx_user where id = #{id}")
	public UserBean findById(int id);
}

    定义一个service接口:

package com.xx.mybatis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xx.mybatis.dao.UserMapper;
import com.xx.mybatis.domain.UserBean;

@Service
public class UserService {
	
	@Autowired
	private UserMapper userMapper;
	
	public UserBean findById(int id) {
		return userMapper.findById(id);
	}
}

编写启动类,这个少不了,添加一个MapperScan的注解,指定Mapper的位置:

package com.xx.mybatis;

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

@SpringBootApplication
@MapperScan(basePackages = "com.xx.mybatis.dao")
public class SpringmybatisApplication {

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

}

    编写单元测试:

package com.xx.mybatis;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.xx.mybatis.domain.UserBean;
import com.xx.mybatis.service.UserService;

import junit.framework.Assert;

@SpringBootTest
@RunWith(SpringRunner.class)
public class SpringmybatisApplicationTests {
	
	@Autowired
	private UserService userService;

	@Test
	public void contextLoads() {
		UserBean user = userService.findById(1);
		
		Assert.assertEquals("feiy", user.getName());
	}

}

    准备数据:

    如果数据库准备了,这时候直接运行单元测试,前面配置文件开启了sql日志打印,这里如果运行单元测试,会看到查询时的sql。

    这里,使用了mybatis注解,sql语句直接写在方法体的注解中,少了UserMapper.xml配置文件的编写,以及配置文件位置指定,很方便。

    这个测试,没有使用阿里的druid数据源,本身默认的hikaricp数据源就是性能很好的,而且远胜于druid数据源,所以没有额外引入druid数据源依赖(pom.xml少了一个配置),所以配置文件中(application.properties)也不需要指定datasource。更加简化了配置,只配置了数据库连接信息。

     整个测试,没有结合springboot-web,没有编写controller,这里主要是为了突出springboot+mybatis+postgresql整合,至于web接口,如果service层都调试通过,web层几乎是套用,难度不大。

    整篇文章是一个入门级的整合,对于springboot开发来说,我们自然希望我们的工程建的又快又简单。

猜你喜欢

转载自blog.csdn.net/feinifi/article/details/121110692
今日推荐