完整的pom依赖
<!--Springboot parent依赖,管理依赖的版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencies>
<!--web依赖,包括了tomcat,jakson依赖等-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--jdbc依赖,引入对应的连接池,使用本依赖,可去除Druid连接池依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!--通用mapper依赖-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.3</version>
</dependency>
<!--mysql连接驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
引入DataSource的几种方案
一、通过Value注解
-
1、首先准备一个properties文件,我这里取名为jdbc.properties
jdbc.driverClassName = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/yun6 jdbc.username=root jdbc.password=root
-
2、准备一个配置类:
import com.alibaba.druid.pool.DruidDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; /** 这是一个Configuration配置类,代替以前的xml文件 */ @Configuration @PropertySource("classpath:application.properties") public class JdbcConfig { //通过@Value注解来获取属性对应的属性值 @Value("${jdbc.driverClassName}") private String driverClassName; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; /** * 组装的带一个DruidDataSource对象 */ @Bean public DruidDataSource dataSource(){ DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(driverClassName); druidDataSource.setUrl(url); druidDataSource.setName(username); druidDataSource.setPassword(password); return druidDataSource; } }
二、通过加载外部资源,通过属性对象注入(一)
-
1、首先准备一个properties文件,我这里取名为jdbc.properties
jdbc.driverClassName = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/yun6 jdbc.username=root jdbc.password=root
-
2、准备一个属性类:
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "jdbc") @Data public class JdbcProperties { private String driverClassName; private String url; private String username; private String password; }
-
3、准备一个配置类
import com.alibaba.druid.pool.DruidDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource("classpath:jdbc.properties") @EnableConfigurationProperties(JdbcProperties.class) public class JdbcConfig { @Bean public DruidDataSource dataSource(JdbcProperties properties){ DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(properties.getDriverClassName()); druidDataSource.setUrl(properties.getUrl()); druidDataSource.setName(properties.getUserName()); druidDataSource.setPassword(properties.getPassword()); return druidDataSource; } }
三、通过加载外部资源,通过属性对象注入(二)
-
1、首先准备一个properties文件,我这里取名为jdbc.properties
jdbc.driverClassName = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/yun6 jdbc.username=root jdbc.password=root
-
2、准备一个属性类:
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "jdbc") @Component @Data public class JdbcProperties { private String driverClassName; private String url; private String username; private String password; }
-
3、准备一个配置类
import com.alibaba.druid.pool.DruidDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource("classpath:jdbc.properties") @EnableConfigurationProperties(JdbcProperties.class) public class JdbcConfig { @Autowired private JdbcProperties properties; @Bean public DruidDataSource dataSource(){ DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(properties.getDriverClassName()); druidDataSource.setUrl(properties.getUrl()); druidDataSource.setName(properties.getUserName()); druidDataSource.setPassword(properties.getPassword()); return druidDataSource; } }
四、通过加载外部资源,通过属性对象注入(三)
-
1、首先准备一个properties文件,我这里取名为jdbc.properties
jdbc.driverClassName = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/yun6 jdbc.username=root jdbc.password=root
-
2、准备一个属性类:
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "jdbc") @Component @Data public class JdbcProperties { private String driverClassName; private String url; private String username; private String password; }
-
3、准备一个配置类
import com.alibaba.druid.pool.DruidDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource("classpath:jdbc.properties") @EnableConfigurationProperties(JdbcProperties.class) public class JdbcConfig { private JdbcProperties properties; public JdbcConfig(JdbcProperties prop){ this.properties = prop; } @Bean public DruidDataSource dataSource(){ DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(properties.getDriverClassName()); druidDataSource.setUrl(properties.getUrl()); druidDataSource.setName(properties.getUserName()); druidDataSource.setPassword(properties.getPassword()); return druidDataSource; } }
五、通过加载properties,通过DruidDataSource对象注入
-
1、首先准备一个application.properties文件
jdbc.driverClassName = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/yun6 jdbc.username=root jdbc.password=root
-
3、准备一个配置类
import com.alibaba.druid.pool.DruidDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class JdbcConfig { @Bean @ConfigurationProperties(prefix = "jdbc") public DruidDataSource dataSource(){ return new DruidDataSource(); } }
拦截器
定义拦截器
import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Author: Steven.yu
* CreateDate: 2019/11/25
* Description: 定义interceptor
*/
@Slf4j
public class MyIntercepter implements HandlerInterceptor {
/**
* preHandle方法,在请求到达时执行,前置拦截,true为放行,false为拦截
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.debug("preHandle method is running ...");
return true;
}
/**
* postHandle方法,在业务逻辑代码执行后执行
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
log.debug("postHandle method is running ...");
}
/**
* afterCompletion方法,最终执行的方法
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
log.debug("afterCompletion method is running ...");
}
}
注册并使用拦截器
import com.itcast.intercepter.MyIntercepter;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* Author: Steven.yu
* CreateDate: 2019/11/25
* Description: 实现WebMvcConfigurer接口,完成拦截器的注册
*/
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
/**
* 将刚刚定义的拦截器进行添加,并设置拦截路径,这里拦截的是所有请求
*/
registry.addInterceptor(new MyIntercepter()).addPathPatterns("/**");
}
}
通过通用mapper实现简单功能
-
1、表结构
CREATE TABLE `tb_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(30) DEFAULT NULL, `password` varchar(18) DEFAULT NULL, `name` varchar(20) DEFAULT NULL, `age` int(11) DEFAULT NULL, `sex` int(11) DEFAULT NULL, `birthday` datetime DEFAULT NULL, `created` datetime DEFAULT NULL, `updated` datetime DEFAULT NULL, `note` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
-
2、 yaml文件变化
logging: level: com.itcast : debug # 设置日志级别,需要map格式 org.springframework: debug spring: # 设置连接池的四大参数 datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/yun6 username: root password: root
-
3、主启动器的变化
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import tk.mybatis.spring.annotation.MapperScan; @SpringBootApplication @MapperScan("com.itcast.mapper") //注册mapper所在的包,我这里的包是com.itcast.mapper public class SpringDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringDemoApplication.class , args); } }
-
4、pojo类:
import lombok.Data; import tk.mybatis.mapper.annotation.KeySql; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Transient; import java.io.Serializable; import java.util.Date; /** * Author: Steven.yu * CreateDate: 2019/11/25 * Description: 用户类,通用mapper会自动开启驼峰命名 */ @Table(name = "tb_user")//匹配的表名称 @Data public class User implements Serializable { @Id//是否为主键 @KeySql(useGeneratedKeys = true)//是否自增长 private Integer id; private String userName; private String password; private String name; private Integer age; private Integer sex; private Date birthday; private Date created; private Date updated; @Transient//瞬时 private String note; }
-
5、mapper接口:
import com.itcast.pojo.User; import tk.mybatis.mapper.common.Mapper; /** * Author: Steven.yu * CreateDate: 2019/11/25 * Description: 通过mapper,User类接口,使用通用mapper会完成单表的增删改查 */ public interface UserMapper extends Mapper<User> {}