SpringBoot 集成 Mybatis 使用 Druid数据源 MySQL数据库

思路:

1、创建项目

项目结构如下:

2、导入相应包

POM.xml文件如下:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.xxx</groupId>
	<artifactId>xxx</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

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

		<!--mybatis-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.1.1</version>
		</dependency>

		<dependency>
			<groupId>org.apache.ibatis</groupId>
			<artifactId>ibatis-core</artifactId>
			<version>3.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.ibatis</groupId>
			<artifactId>ibatis-sqlmap</artifactId>
			<version>2.3.4.726</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.0</version>
		</dependency>

		<!-- druid 线程池模块 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.3</version>
		</dependency>
		<dependency>
			<groupId>org.jetbrains</groupId>
			<artifactId>annotations</artifactId>
			<version>RELEASE</version>
		</dependency>

	</dependencies>

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


</project>

2.1 编写相应配置文件

spring.application.name=maybee-managementcenter
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
spring.thymeleaf.mode=LEGACYHTML5

server.port=1112

mybatis.type-aliases-package=com.thunisoft.ssptms.pojo
mybatis.mapperLocations=classpath:mapper/*.xml

# mybatis datasource
spring.datasource.url=jdbc:mysql://xxxxxxxx:3306/xxx?useUnicode=true&characterEncoding=utf8&useSSL=false
#spring.datasource.schema=classpath:/mysql.sql
spring.datasource.username=root
spring.datasource.password=Root@123
spring.application.yml.initialize=true
spring.datasource.continueOnError=true
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

web.upload-path=C:/data/
spring.resources.static-locations=classpath:/config/,classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.upload-path}

spring.http.multipart.enabled=true
spring.http.multipart.max-file-size=50Mb
spring.http.multipart.max-request-size=100Mb

3、添加mapper、dao、pojo

使用mybatis自动生成工具生成:

4、测试

TestService:

package com.xxx.xxx.service;

import com.xxx.xxx.dao.SsptFyMapper;
import com.xxx.xxx.pojo.SsptFy;
import com.xxx.xxx.pojo.SsptFyExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TestService {

    @Autowired
    private SsptFyMapper ssptFyMapper;

    public int testAdd(SsptFy ssptFy) {
        if(ssptFyMapper.selectByPrimaryKey(ssptFy.getId()) != null) {
            return -1;
        }
        return ssptFyMapper.insert(ssptFy);
    }

    public List<SsptFy> getAllSsptFy() {
        return ssptFyMapper.selectByExample(new SsptFyExample());
    }
}

TestController:

package com.xxx.xxx.controller;

import com.xx.xx.xx.utils.UUIDHelper;
import com.xx.xx.pojo.SsptFy;
import com.xx.xx.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/test")
public class TestController {

    @Autowired
    private TestService testService;

    @RequestMapping("/addtest")
    @ResponseBody
    public String addtest() {
        SsptFy ssptFy = new SsptFy();
        ssptFy.setId(UUIDHelper.uuid().toLowerCase().replace("-",""));
//        ssptFy.setId("81c0c81787424b9e8e048f2f1313789e"); // test duplicate.
        ssptFy.setName("xxx");
        ssptFy.setAddress("xxx");
        ssptFy.setCrtTm(new Date());
        ssptFy.setUpdTm(new Date());
        ssptFy.setCrtUser("xxx");
        ssptFy.setUpdUser("xxx");
        int res = testService.testAdd(ssptFy);
        System.out.println("insert result : " + res);
        return "insert result : " + res;
    }

    @RequestMapping("/getallssptfy")
    @ResponseBody
    public List<SsptFy> getAllSsptFy() {
        return testService.getAllSsptFy();
    }
}

猜你喜欢

转载自www.cnblogs.com/hfultrastrong/p/8917343.html