目錄:
- 1、搭建maven項目(工具:IDEA 2019.2.3)
- 2、在maven項目中搭建SpringBoot框架
- 3、maven項目中使用jsp(SpringBoot框架)
- 4、自定義SpringBoot配置
- 5、自定義項目啓動banner顯示(SpringBoot)
- 6、整合MyBatis Plus+MySQL(SpringBoot框架)
- 注:Maven pom.xml詳解
1、在pom.xml文件中導入以下包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.1.9</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>1.1.9</version>
</dependency>
2、配置application.xml
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
druid:
test:
url: jdbc:mysql://192.168.1.1:3306/test?allowMultiQueries=true&com.mysql.jdbc.faultInjection.serverCharsetIndex=45
username: root
password: 123456
#mybatis
mybatis-plus:
mapper-locations: classpath:mapper/*.xml #路徑爲src/main/resources/mapper
typeAliasesPackage: com.lzw.entity
global-config:
#主鍵類型:0-數據庫自增id, 1-用戶輸入id,2-全局唯一ID (數字類型唯一id), 3-全局唯一id uuid
id-type: 2
#字段策略:0-忽略判斷,1-非NULL判斷,2-非空判斷
field-strategy: 2
#駝峯下劃線轉換
db-column-underline: true
#刷新mapper
refresh-mapper: true
#邏輯刪除配置
logic-delete-value: -1
logic-not-delete-value: 0
#自定義SQL注入器
sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
call-setters-on-nulls: true
3、配置數據源,創建DynamicDataSourceConfig.java和DynamicDataSource.java
DynamicDataSourceConfig.java:
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
/**
* 配置數據源
*/
@Configuration
public class DynamicDataSourceConfig {
private static String DATA_SOURCE = "test";
@Bean
@ConfigurationProperties("spring.datasource.druid.test")
public DataSource firstDataSource(){
return DruidDataSourceBuilder.create().build();
}
@Bean
@Primary
public DynamicDataSource dataSource(DataSource dataSource) {
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DATA_SOURCE, dataSource);
return new DynamicDataSource(dataSource, targetDataSources);
}
}
DynamicDataSource.java:
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import javax.sql.DataSource;
import java.util.Map;
/**
* 動態數據源
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
public DynamicDataSource(DataSource defaultTargetDataSource, Map<Object, Object> targetDataSources) {
super.setDefaultTargetDataSource(defaultTargetDataSource);
super.setTargetDataSources(targetDataSources);
super.afterPropertiesSet();
}
@Override
protected Object determineCurrentLookupKey() {
return getDataSource();
}
public static String getDataSource() {
return contextHolder.get();
}
}
4、創建測試類
①、創建實體類User.java
package com.lzw.entity;
public class User {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
②、創建dao層UserDao.java
package com.lzw.dao;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.lzw.entity.User;
import org.apache.ibatis.annotations.Mapper;
/**
* 此處不添加@Mapper則需要在Application.java中添加註解@MapperScan("com.lzw.dao")進行統一配置
*/
@Mapper
public interface UserDao extends BaseMapper<User> {
}
③、創建服務層 UserService.java和實現類UserServiceImpl.java
UserService.java:
package com.lzw.service;
import com.baomidou.mybatisplus.service.IService;
import com.lzw.entity.User;
import java.util.List;
public interface UserService extends IService<User> {
List<User> queryUserList();
}
UserServiceImpl.java:
package com.lzw.service.impl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lzw.dao.UserDao;
import com.lzw.entity.User;
import com.lzw.service.UserService;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
public List<User> queryUserList() {
return this.selectList(new EntityWrapper<User>());
}
}
④、創建Controller
package com.lzw.controller;
import com.lzw.entity.User;
import com.lzw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class TestController {
@Autowired
private UserService userService;
@RequestMapping("/test")
public List<User> test() {
return userService.queryUserList();
}
}
5、啓動項目並訪問
自定義sql語句:
①、src/main/resources/mapper路徑下創建測試xml,如圖
UserDao.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lzw.dao.UserDao">
<select id="queryList" resultType="com.lzw.entity.User">
select * from `user`
</select>
</mapper>
②、在dao層編寫代碼
@Mapper
public interface UserDao extends BaseMapper<User> {
List<User> queryList(@Param("name") String name); // 方法名稱須和UserDao.xml中的id名稱一致,其中@Param("name")單個參數可不寫,多個必須寫
}
③、調用dao層方法,請求接口即可實現自定義sql查詢