Java程序设计:spring boot(6)——SpringBoot 整合 Mybatis

1 SpringBoot 整合 Mybatis

1.1 环境整合配置

Idea 下创建 Maven 普通⼯程 springboot_mybatis:

pom.xml 添加核⼼依赖:

<properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <maven.compiler.source>1.8</maven.compiler.source>
 <maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>2.2.2.RELEASE</version>
</parent>
<dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <!-- mybatis 集成 -->
 <dependency>
 <groupId>org.mybatis.spring.boot</groupId>
 <artifactId>mybatis-spring-boot-starter</artifactId>
 <version>2.1.1</version>
 </dependency>
 <!-- springboot 分⻚插件 -->
 <dependency>
 <groupId>com.github.pagehelper</groupId>
 <artifactId>pagehelper-spring-boot-starter</artifactId>
 <version>1.2.13</version>
 </dependency>
 <!-- mysql 驱动 -->
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 </dependency>
 <!-- c3p0 数据源 -->
 <dependency>
 <groupId>com.mchange</groupId>
 <artifactId>c3p0</artifactId>
 <version>0.9.5.5</version>
 </dependency>
</dependencies>
<build>
 <plugins>
 <plugin>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 </plugin>
 </plugins>
</build> 

application.yml 整合配置:

server:
 # 设置项⽬启动的端⼝号
 port: 8080
 # 设置项⽬的访问路径(上下⽂路径)
 servlet:
 context-path: /springboot_mybatis
## 数据源配置
spring:
 datasource:
 type: com.mchange.v2.c3p0.ComboPooledDataSource
 driver-class-name: com.mysql.cj.jdbc.Driver
 url: jdbc:mysql://127.0.0.1:3306/springboot_mybatis?
useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
 username: root
 password: root
## mybatis 配置
mybatis:
 mapper-locations: classpath:/mappers/*.xml
 type-aliases-package: com.xxxx.springboot.po
 configuration:
 ## 下划线转驼峰配置
 map-underscore-to-camel-case: true
## pageHelper
pagehelper:
 helper-dialect: mysql
## 显示dao 执⾏sql语句
logging:
 level:
 com:
 xxxx:
 springboot:
 dao: debug

1.2 源代码添加

JavaBean 对象定义:

package com.xxxx.springboot.po;
public class User {
 private Integer id;
 private String userName;
 private String userPwd;
 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 String getUserPwd() {
 return userPwd;
 }
 public void setUserPwd(String userPwd) {
 this.userPwd = userPwd;
 }
}

Dao层接⼝⽅法定义:

       com.xxxx.springboot.dao 包下创建 UserMapper.java 接⼝声明查询⽅法

package com.xxxx.springboot.dao;
import com.xxxx.springboot.po.User;
public interface UserMapper {
 // 根据⽤户名查询⽤户记录
 User queryUserByUserName(String userName);
}

SQL映射⽂件添加:

       resources/mappers ⽬录下添加 UserMapper.xml 配置,查询 statetment

<?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.xxxx.springboot.dao.UserMapper">
 <select id="queryUserByUserName" parameterType="string"
resultType="com.xxxx.springboot.po.User">
 select
 id,user_name,user_pwd
 from tb_user
 where user_name=#{userName}
 </select>
</mapper>

添加 service 、controller 对应代码:

UserService.java

@Service
public class UserService {
 
 @Autowired
 private UserMapper userMapper;
 public User queryUserByUserName(String userName){
 return userMapper.queryUserByUserName(userName);
 }
}

UserController.java

@RestController
public class UserController {
 @Resource
 private UserService userService;
 @GetMapping("user/{userName}")
 public User queryUserByUserName(@PathVariable String userName){
 return userService.queryUserByUserName(userName);
 }
}

添加应⽤启动⼊⼝:

@SpringBootApplication
@MapperScan("com.xxxx.springboot.dao")
public class Starter {
 public static void main(String[] args) {
 SpringApplication.run(Starter.class);
 }
}

1.3 启动测试

运⾏ Starter main⽅法,启动应⽤浏览器测试查询:

后端⽇志打印效果 :

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

茜茜西西CeCe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值