springboot mybatis-plus使用

1.項目目錄結構java

2.pom.xmlmysql

<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.idreamsky</groupId>
	<artifactId>test1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>test1</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<druid.version>1.1.10</druid.version>
	</properties>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<!--<version>Finchley.SR2</version> -->
				<version>Edgware.SR5</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<!-- 服務註冊中心要使用,eureka客戶端不用 -->
		<!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> 
			</dependency> -->
		<!-- 服務提供者,eureka客戶端須要使用 -->
		<!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> 
			</dependency> -->
		<!-- Feign遠程調用,須要調用服務接口可使用 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
		<!-- 斷路器,服務調用者須要使用該組件,來保證其餘服務降級時的處理邏輯 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		<!-- 網關,對外開放的接收外部請求的服務 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zuul</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-consul-discovery</artifactId>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.0</version>
			<scope>provided</scope>
		</dependency>
		<!-- <dependency> <groupId>com.spring4all</groupId> <artifactId>swagger-spring-boot-starter</artifactId> 
			<version>1.7.0.RELEASE</version> </dependency> -->
		<!-- 熱啓動,熱部署依賴包 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<version>1.5.6.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.9.2</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.3.12.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.8.0</version>
		</dependency>

		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>25.1-jre</version>
		</dependency>

		<dependency>
			<groupId>com.ctrip.framework.apollo</groupId>
			<artifactId>apollo-client</artifactId>
			<version>1.1.0</version>
		</dependency>

		<!-- Mybatis依賴 -->
		<!-- <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> 
			<version>1.3.1</version> </dependency> -->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.0.6</version>
		</dependency>

		<!--數據庫驅動 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.46</version>
		</dependency>

		<!--數據庫鏈接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>${druid.version}</version>
		</dependency>
	</dependencies>
</project>

3.application.propertiesspring

server:
  port: 8081
spring:
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        healthCheckPath: /health
        healthCheckInterval: 5s
        instance-id: test1
  application:
    name: test1
      # mysql 屬性配置
  datasource: 
     driver-class-name: com.mysql.jdbc.Driver
     url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
     username: root 
     password: root
  
app:
  id: test1
apollo:
  meta: http://localhost:8080
  

  # mybatis-plus 配置
mybatis-plus:
  # 若是是放在src/main/java目錄下 classpath:/com/yourpackage/*/mapper/*Mapper.xml
  # 若是是放在resource目錄 classpath:/mapper/*Mapper.xml
  config-location: classpath:/mybatis/mybatis-config.xml
  mapper-locations: classpath:/mybatis/mapper/*.xml
  #實體掃描,多個package用逗號或者分號分隔
  typeAliasesPackage: com.idreamsky.test1.entity
  global-config:
    #主鍵類型  0:"數據庫ID自增", 1:"用戶輸入ID",2:"全局惟一ID (數字類型惟一ID)", 3:"全局惟一ID UUID";
    id-type: 3
    # 熱加載mapper文件
    refresh: true
    db-config:
      db-type: mysql

4.UserMapper.java文件sql

 

package com.test1.dao;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.test1.entity.User;

@Mapper
public interface UserMapper extends BaseMapper<User> {

	@Update("update tbl_user set addr = #{address} where user_id = #{userid}")
	public int updateU(@Param("userid") String id, @Param("address") String addr);

	@Results({ @Result(column = "user_id", property = "id", id = true),
			@Result(column = "user_name", property = "name") })
	@Select("<script> select * from tbl_user where user_id = #{id} </script>")
	public User q(String id);
}

5.UserService.java文件數據庫

package com.test1.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.test1.entity.User;

public interface UserService extends IService<User> {
	public int u(String id, String addr);

	public User q(String id);
}

6.UserServiceImpl.java文件apache

package com.test1.service;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test1.dao.UserMapper;
import com.test1.entity.User;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

	@Override
	public int u(String id, String addr) {
		return baseMapper.updateU(id, addr);
	}

	@Override
	public User q(String id) {
		return baseMapper.q(id);
	}
}

7.controller部分代碼mybatis

 

@Autowired
	private UserService userservice;

	@RequestMapping("/user")
	public String user() {
		Random r = new Random();
		int i = r.nextInt(100);
		User user = new User();
		user.setName("name-" + i).setAddr("add-" + i);
		userservice.save(user);
		return "添加用戶成功--" + new Date();
	}

	@RequestMapping("/user1")
	public String user1() {
		System.out.println(userservice.u("1121678028402647041", "廣東深圳"));
		return "添加用戶成功--" + new Date();

	}

	@RequestMapping("/user2")
	public User user2() {
		return userservice.q("1121678028402647041");
	}

8.mybatis-config.xml文件app

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

    <settings>
        <setting name="callSettersOnNulls" value="true"/>

        <setting name="cacheEnabled" value="true"/>

        <setting name="lazyLoadingEnabled" value="true"/>

        <setting name="aggressiveLazyLoading" value="true"/>

        <setting name="multipleResultSetsEnabled" value="true"/>

        <setting name="useColumnLabel" value="true"/>

        <setting name="useGeneratedKeys" value="false"/>

        <setting name="autoMappingBehavior" value="PARTIAL"/>

        <setting name="defaultExecutorType" value="SIMPLE"/>

        <setting name="mapUnderscoreToCamelCase" value="true"/>

        <setting name="localCacheScope" value="SESSION"/>

        <setting name="jdbcTypeForNull" value="NULL"/>

    </settings>

    <typeAliases>
        <typeAlias alias="Integer" type="java.lang.Integer" />
        <typeAlias alias="Long" type="java.lang.Long" />
        <typeAlias alias="HashMap" type="java.util.HashMap" />
        <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
        <typeAlias alias="ArrayList" type="java.util.ArrayList" />
        <typeAlias alias="LinkedList" type="java.util.LinkedList" />
    </typeAliases>

</configuration>
相關文章
相關標籤/搜索