-
Mybatis訪問數據庫
-
註解方式
-
創建項目
-
勾選mysql Driver; JDBC API;Mybatis
-
-
配置數據源
-
spring:
-
datasource:
-
username: root
-
password: yahang.521
-
driver-class-name: com.mysql.cj.jdbc.Driver
-
-
# Hikari 連接池配置
-
hikari:
-
minimum-idle: 5 # 最小空閒連接數量
-
idle-timeout: 180000 # 空閒連接存活最大時間,默認600000(10分鐘)
-
maximum-pool-size: 10 # 連接池最大連接數,默認是10
-
auto-commit: true # 此屬性控制從池返回的連接的默認自動提交行爲,默認值:true
-
pool-name: HikariPool # 連接池名稱
-
max-lifetime: 1800000 # 此屬性控制池中連接的最長生命週期,值0表示無限生命週期,默認1800000即30分鐘
-
connection-timeout: 30000 # 數據庫連接超時時間,默認30秒,即30000
-
connection-test-query: SELECT 1
-
-
創建操作不同表對應的Mapper接口
-
@Mapper
-
public interface StudentMapper {
-
-
@Select("select * from student where id=#{id}")
-
Student queryStudentById(Integer id);
-
-
@Delete("delete from student where id = #{id}")
-
int deleteStudentById(Integer id);
-
-
}
-
-
編寫業務代碼
-
@Controller
-
public class StController {
-
-
@Autowired
-
private StudentMapper studentMapper;
-
-
@ResponseBody
-
@RequestMapping("/getStudentById")
-
public Student getStudentById(Integer id) {
-
Student student = studentMapper.queryStudentById(id);
-
return student;
-
}
-
}
-
-
其他
-
批量掃描包下的mapper類
-
@MapperScan(value = "com.njupt.mapper")
-
@SpringBootApplication
-
public class MybatisApplication {
-
public static void main(String[] args) {
-
SpringApplication.run(MybatisApplication.class, args);
-
}
-
}
-
-
-
配置文件方式
-
創建項目
-
勾選mysql Driver; JDBC API;Mybatis
-
-
配置數據源
-
spring:
-
datasource:
-
username: root
-
password: yahang.521
-
driver-class-name: com.mysql.cj.jdbc.Driver
-
-
# Hikari 連接池配置
-
hikari:
-
minimum-idle: 5 # 最小空閒連接數量
-
idle-timeout: 180000 # 空閒連接存活最大時間,默認600000(10分鐘)
-
maximum-pool-size: 10 # 連接池最大連接數,默認是10
-
auto-commit: true # 此屬性控制從池返回的連接的默認自動提交行爲,默認值:true
-
pool-name: HikariPool # 連接池名稱
-
max-lifetime: 1800000 # 此屬性控制池中連接的最長生命週期,值0表示無限生命週期,默認1800000即30分鐘
-
connection-timeout: 30000 # 數據庫連接超時時間,默認30秒,即30000
-
connection-test-query: SELECT 1
-
-
在resources目錄下創建兩個目錄
-
mybatis官方文檔: https://mybatis.org/mybatis-3/zh/index.html
-
resources/mybatis/config:mybatis全局配置文件目錄
-
新建mybatis-config.xml文件: 該文件無需配置環境,也無需引用mapper文件,直接配置就可以
-
<?xml version="1.0" encoding="UTF-8" ?>
-
<!DOCTYPE configuration
-
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
-
<configuration>
-
<settings>
-
<setting name="mapUnderscoreToCamelCase" value="true"/>
-
</settings>
-
</configuration>
-
-
-
resources/mybatis/mapper:mybatis sql映射文件目錄
-
新建StudentMapper.xml
-
<?xml version="1.0" encoding="UTF-8" ?>
-
<!DOCTYPE mapper
-
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
-
<mapper namespace="com.njupt.mapper.StudentMapper"> <!-- 接口全類名 -->
-
<select id="getStudentById" resultType="com.njupt.entities.Student">
-
select * from student where id=#{id}
-
</select>
-
</mapper>
-
-
-
-
讓SpringBoot查找到這兩個xml文件,需要在application.yml中配置以下兩項
-
mybatis:
-
config-location: classpath:mybatis/config/mybatis-config.xml
-
mapper-locations:
-
- classpath:mybatis/mapper/*.xml
-
-
創建操作不同表對應的Mapper接口
-
public interface StudentMapper {
-
public Student getStudentById(Integer id);
-
}
-
-
批量掃描包下的mapper類
-
@SpringBootApplication
-
@MapperScan(value = "com.njupt.mapper")
-
public class MybatisApplication {
-
public static void main(String[] args) {
-
SpringApplication.run(MybatisApplication.class, args);
-
}
-
}
-
-
編寫業務代碼
-
@Controller
-
public class StudentController {
-
-
@Autowired
-
private StudentMapper studentMapper;
-
private Logger logger = LoggerFactory.getLogger(this.getClass());
-
-
@RequestMapping("/getStudentById/{id}")
-
@ResponseBody
-
public Student getStudentById(@PathVariable("id") Integer id) {
-
logger.info("enter getStudentById");
-
return studentMapper.getStudentById(id);
-
}
-
}
-
-
測試
-
-
Springboot整合MyBatis進行數據庫交互
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.