Maven Spring Boot + Mybatis
程序员文章站
2022-03-02 15:29:54
...
添加依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
application.properties
############### 数据源 ###############
# druid基本属性
spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/tdog?useUnicode=true&characterEncoding=utf8
spring.datasource.druid.username=root
spring.datasource.druid.password=root
# 初始化连接数/最小/最大
spring.datasource.druid.initial-size=1
spring.datasource.druid.min-idle=1
spring.datasource.druid.max-active=20
# 获取连接等待超时时间
spring.datasource.druid.max-wait=60000
############### Mybatis配置 ###############
# 获取自增主键值
mybatis.configuration.use-generated-keys=true
# 列别名替换列标签
mybatis.configuration.use-column-label=true
# 开启驼峰命名转换:Table{create_time} -> Entity{createTime}
mybatis.configuration.map-underscore-to-camel-case=true
# mapper位置
mybatis.mapper-locations=classpath:mapper/*.xml
# 实体类路径,在Mapper中使用时:org.baozi.entity.XXX 可以替换成 XXX
mybatis.type-aliases-package=org.baozi.entity
关联Dao和Mapper
方式一:在Dao接口上添加@Mapper:
import org.apache.ibatis.annotations.Mapper;
import org.baozi.entity.LocalAuth;
@Mapper
public interface LocalAuthDao {
/**
* 登录
* @param localAuth
* @return
*/
LocalAuth login(LocalAuth localAuth);
}
方式二:在Spring boot启动类启动类加@MapperScan("Dao的包路径")
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("org.baozi.dao")
public class TdogApplication {
public static void main(String[] args) {
SpringApplication.run(TdogApplication.class, args);
}
}
Mapper文件框架
<?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="dao接口">
</mapper>
转载于:https://www.jianshu.com/p/7486c38b9762
推荐阅读
-
spring boot整合Shiro实现单点登录的示例代码
-
spring boot实现验证码功能
-
spring boot 与kafka集成的示例代码
-
spring boot Logging的配置以及使用详解
-
Spring Boot启动及退出加载项的方法
-
spring boot利用docker构建gradle项目的实现步骤
-
Spring Boot+Mybatis+Druid+PageHelper实现多数据源并分页的方法
-
Spring Boot整合ElasticSearch实现多版本兼容的方法详解
-
详解手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)
-
Spring Boot实战之模板引擎