1. SpringBoot整合連接池
1.1 在pom.xml中引人jdbc連接池依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
1.2 在application.properties配置數據庫連接信息
# 數據源連接信息
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
# hikari連接池
spring.datasource.hikari.idle-timeout=60000
spring.datasource.hikari.maximum-pool-size=30
spring.datasource.hikari.minimum-idle=10
2 SpringBoot整合mybatis
2.1 概述
2.2 在pom.xml引入mybatis依賴
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
2.3 表結構與數據
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`birthday` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `user` VALUES ('1', '張三', '13', '2013-03-03');
INSERT INTO `user` VALUES ('2', '李四', '14', '2014-04-04');
INSERT INTO `user` VALUES ('3', '王五', '15', '2015-05-05');
2.4 實體類
package com.domain;
import java.util.Date;
public class User {
private Integer id;
private String username;
private Integer age;
private Date birthday;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {return birthday;}
public void setBirthday(Date birthday) {this.birthday = birthday; }
@Override
public String toString() {
return "User{" + "id=" + id + ", username='" + username + '\'' + ", age=" + age + ", birthday=" + birthday + '}';
}
}
2.5 業務接口與實現類
package com.service;
import com.domain.User;
import java.util.List;
public interface IUserService {
List<User> findAll();
}
package com.service.impl;
import com.service.IUserService;
import com.domain.User;
import com.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.findAll();
}
}
2.6 控制器類
package com.controller;
import com.domain.User;
import com.service.IUserService;
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
@RequestMapping("user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("findAll")
public List<User> findAll(){
System.out.println("findAll");
List<User> userList=userService.findAll();
System.out.println(userList);
return userList;
}
}
2.7 XML方式
2.7.1 Mapper接口
package com.mapper;
import com.domain.User;
import java.util.List;
public interface UserMapper {
List<User> findAll();
}
2.7.2 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.mapper.UserMapper">
<select id="findAll" resultType="com.domain.User">
select * from user;
</select>
</mapper>
2.7.3 application.properties配置
# mybatis 實體類型別名掃描,配合映射文件XXXMapper.xml使用
# 如果用註解方式,該設置可以省略
mybatis.type-aliases-package=com.domain
# mapper.xml映射文件位置,如果接口與xml在同一個包中(包括java中的包與resource中的包同包),該設置可以省略
# 如果用註解方式,該設置可以省略
mybatis.mapper-locations=classpath:com/mapper/*.xml
2.7.4 SpringBoot啓動類添加掃描接口的註解
@SpringBootApplication
@MapperScan("com.mapper")
public class SpringBootStarter {
public static void main(String[] args) {
SpringApplication.run(SpringBootStarter.class);
}
}
2.8 註解方式
- 2.7.2與2.7.3步驟直接省略掉,不用做
- 2.7.4代碼不變,依然要添加@MapperScan註解
- Mapper接口中@Mapper註解只對當前接口有效
- SpringBoot啓動類的@MapperScan對指定包下所有接口都有效
package com.mapper;
import com.domain.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface UserMapper {
@Select("select * from user")
List<User> findAll();
}
@SpringBootApplication
@MapperScan("com.mapper")
public class SpringBootStarter {
public static void main(String[] args) {
SpringApplication.run(SpringBootStarter.class);
}
}
3. SpringBoot集成tkMybatis
3.1 在pom.xml引入依賴
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
3.2 Mapper接口繼承通用的Mapper接口
package com.mapper;
import com.domain.User;
import tk.mybatis.mapper.common.Mapper;
public interface UserMapper extends Mapper<User> {
}
3.3 SpringBoot啓動類修改成tk包的@MapperScan
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.mapper")
public class SpringBootStarter {
public static void main(String[] args) {
SpringApplication.run(SpringBootStarter.class);
}
}
3.4 tk的Mapper的方法
- 這裏不詳細說明