SpringBoot如何通過yml方式整合Mybatis
本來打算寫個使用Sharding-JDBC的例程,但是在搭建Mybatis的過程中,一波三折,因為好久沒搭建項目了,另外加上換了電腦。所以很破折,在這里記錄一下Spring Boot整合Mybatis吧??赡芎芎唵危俏议L時間沒用忘記了,我這里備忘一下吧。
一、項目目錄結(jié)構(gòu)
注意這里Application文件的位置,它是與controller、entity、mapper、service等包處于并列的關(guān)系。

二、數(shù)據(jù)庫文件
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for t_user_00 -- ---------------------------- DROP TABLE IF EXISTS `t_user_00`; CREATE TABLE `t_user_00` ( `id` int(0) NOT NULL AUTO_INCREMENT, `user_id` int(0) NOT NULL, `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `age` int(0) NOT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;
三、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gougou</groupId>
<artifactId>shardingjdbc-shardingtable-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>shardingjdbc-shardingtable-demo</name>
<description>shardingjdbc-shardingtable-demo</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<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>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
四、application.yml
# 數(shù)據(jù)源 spring: application: name: shardingjdbc-shardingtable-demo datasource: url: jdbc:mysql://localhost:3306/sharding_0?serverTimezone=UTC username: root password: root driver-class-name: com.mysql.jdbc.Driver dbcp2: min-idle: 5 # 數(shù)據(jù)庫連接池的最小維持連接數(shù) initial-size: 5 # 初始化連接數(shù) max-total: 5 # 最大連接數(shù) max-wait-millis: 150 # 等待連接獲取的最大超時時間 # mybatis配置 mybatis: mapper-locations: classpath:mapper/*.xml # mapper映射文件位置 type-aliases-package: com.gouggou.shardingtable.entity # 實體類所在的位置 configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #用于控制臺打印sql語句
五、啟動類Application
說明:
1、@MapperScan的:掃描mapper接口的位置
2、@ComponentScan:如果Application文件的位置不是與controller、entity、mapper、service等包處于并列的關(guān)系。就要用此注解,否則可以不用;
@MapperScan("com.gouggou.shardingtable.mapper")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
六、controller
@RequestMapping("student")
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("save")
public String save() {
User user = new User();
user.setUserId(new Random().nextInt( 1000 ) + 1);
user.setName("張三"+user.getUserId());
user.setAge(new Random().nextInt( 80 ) + 1);
userService.insert(user);
return user.getName()+"創(chuàng)建成功!";
}
}
七、service
public interface UserService {
Integer insert(User u);
List<User> findAll();
List<User> findByUserIds(List<Integer> userIds);
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public Integer insert(User u) {
return userMapper.insert(u);
}
@Override
public List<User> findAll() {
return userMapper.findAll();
}
@Override
public List<User> findByUserIds(List<Integer> userIds) {
return userMapper.findByUserIds(userIds);
}
}
八、entity
@Data
public class User implements Serializable {
private static final long serialVersionUID = -5514139686858156155L;
private Integer id;
private Integer userId;
private String name;
private Integer age;
}
九、Mapper
@Repository
public interface UserMapper {
Integer insert(User u);
List<User> findAll();
List<User> findByUserIds(List<Integer> userIds);
}
<?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.gouggou.shardingtable.mapper.UserMapper" >
<resultMap id="resultMap" type="com.gouggou.shardingtable.entity.User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="user_id" property="userId" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
</resultMap>
<insert id="insert">
insert into t_user_00 (user_id,name,age) values (#{userId},#{name},#{age})
</insert>
<select id="findAll" resultMap="resultMap">
select <include refid="columnsName"/> from t_user_00
</select>
<select id="findByUserIds" resultMap="resultMap">
select <include refid="columnsName"/> from t_user_00 where user_id in (
<foreach collection="list" item="item" separator=",">
#{item}
</foreach>
)
</select>
<sql id="columnsName">
id,user_id,name,age
</sql>
</mapper>
十、遇到的問題
2、maven倉庫中產(chǎn)生后綴是LastUpdated的文件
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 解決Spring boot整合mybatis,xml資源文件放置及路徑配置問題
- SpringBoot整合mybatis-generator-maven-plugin的方法
- springboot整合mybatis-plus 實現(xiàn)分頁查詢功能
- Spring Boot整合mybatis使用注解實現(xiàn)動態(tài)Sql、參數(shù)傳遞等常用操作(實現(xiàn)方法)
- springboot2.3 整合mybatis-plus 高級功能(圖文詳解)
- 解決SpringBoot整合Mybatis掃描不到Mapper的問題
- SpringBoot整合MybatisSQL過濾@Intercepts的實現(xiàn)
- Spring整合Mybatis的全過程
相關(guān)文章
SpringBoot整合Redisson實現(xiàn)分布式鎖
本文主要介紹了SpringBoot整合Redisson實現(xiàn)分布式鎖,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
詳解servlet的url-pattern匹配規(guī)則
本篇文章主要介紹了=servlet的url-pattern匹配規(guī)則,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
Spring?IoC容器Bean作用域的singleton與prototype使用配置
這篇文章主要為大家介紹了Spring?IoC容器Bean作用域的singleton與prototype使用配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
Java Spring Boot消息服務(wù)萬字詳解分析
在實際項目開發(fā)中,有時需要與其他系統(tǒng)進行集成完成相關(guān)業(yè)務(wù)功能,這種情況最原始做法是程序內(nèi)部相互調(diào)用,除此之外,還可以用消息服務(wù)中間件進行業(yè)務(wù)處理,用消息服務(wù)中間件處理業(yè)務(wù)能夠提升系統(tǒng)的異步通信和擴展解耦能力。Spring Boot對消息服務(wù)管理提供了非常好的支持2021-10-10
Spring?Boot的幾種統(tǒng)一處理方式梳理小結(jié)
這篇文章主要為大家介紹了Spring?Boot的幾種統(tǒng)一處理方式梳理小結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05
WebSocket 中使用 @Autowired 注入對應(yīng)為null的解決方案
SpringBoot集成WebSocket時,會遇到service對象為null的情況,原因是Spring默認為單例模式與WebSocket的多對象模式相沖突,當客戶端與服務(wù)器端建立連接時,會創(chuàng)建新的WebSocket對象,本文給大家介紹WebSocket 中使用 @Autowired 注入對應(yīng)為null的問題,感興趣的朋友一起看看吧2024-10-10

