springboot整合MyBatis(注解方式)
springboot整合MyBatis(注解方式)
在pom.xml文件中导入MyBatis所需依赖:
<dependencies>
<!-- jdbc的starter,如果使用mybatis-spring-boot-starter就不需要这个starter,因为mybatis的starter自带jdbc的starter -->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-jdbc</artifactId>-->
<!-- </dependency>-->
<!-- MyBatis的starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!--引入mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- druid连接池依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
<!-- log4j日志依赖 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
在application.yaml文件中配置数据源和MyBatis:
#数据源的配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
创建DataSourceConfig配置类:
package cn.tx.datasource;
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;
public class DataSourceConfig {
//配置第三方组件(Druid连接池的数据源配置)
public DruidDataSource dataSource(){
return new DruidDataSource();
}
}
创建User实体类(实体类的属性要与数据表中字段名相同,数据库表的内容就不贴出来了):
package cn.tx.datasource;
public class User {
private int id;
private String name;
private String phone;
private String password;
private String email;
private String imageSrc;
private String manager;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getImageSrc() {
return imageSrc;
}
public void setImageSrc(String imageSrc) {
this.imageSrc = imageSrc;
}
public String getManager() {
return manager;
}
public void setManager(String manager) {
this.manager = manager;
}
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", phone='" + phone + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", imageSrc='" + imageSrc + '\'' +
", manager='" + manager + '\'' +
'}';
}
}
创建UserMapper.java接口,并添加@Mapper注解,使用@Select(“select * from user”)注解方式写查询语句:
package cn.tx.datasource;
import cn.tx.datasource.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface UserMapper {
public List<User> findUser();
}
创建Controller类:
package cn.tx.datasource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
public class HelloController {
private UserMapper userMapper;
private List<User> findUser(){
return userMapper.findUser();
}
}
创建主启动类MyApplication:
package cn.tx.datasource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class,args);
}
}
- 到此为止springboot整合MyBatis的注解方式完成,运行主启动类的main函数,访问http://localhost:8080/findUser可以看到成功查询数据。
评论