SpringBoot如何通过yml方式整合Mybatis

网友投稿 261 2022-12-10


SpringBoot如何通过yml方式整合Mybatis

本来打算写个使用Sharding-JDBC的例程,但是在搭建Mybatis的过程中,一波三折,因为好久没搭建项目了,另外加上换了电脑。所以很破折,在这里记录一下Spring Boot整合Mybatis吧。可能很简单,但是我长时间没用忘记了,我这里备忘一下吧。

一、项目目录结构

注意这里Application文件的位置,它是与controller、entity、mapper、service等包处于并列的关系。

二、数据库文件

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

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.13.RELEASE

com.gougou

shardingjdbc-shardingtable-demo

0.0.1-SNAPSHOT

shardingjdbc-shardingtable-demo

shardingjdbc-shardingtable-demo

UTF-8

1.8

org.springframework.boot

spring-boot-starter-web

org.projectlombok

lombok

true

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

mysql

mysql-connector-java

org.springframework.boot

spring-boot-maven-plugin

&lhttp://t;/plugins>

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.13.RELEASE

com.gougou

shardingjdbc-shardingtable-demo

0.0.1-SNAPSHOT

shardingjdbc-shardingtable-demo

shardingjdbc-shardingtable-demo

UTF-8

1.8

org.springframework.boot

spring-boot-starter-web

org.projectlombok

lombok

true

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

mysql

mysql-connector-java

org.springframework.boot

spring-boot-maven-plugin

&lhttp://t;/plugins>

四、application.yml

# 数据源

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 # 数据库连接池的最小维持连接数

initial-size: 5 # 初始化连接数

max-total: 5 # 最大连接数

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等包处于并列的关系。就要用此注解,否则可以不用;

@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()+"创建成功!";

}

}

七、service

public interface UserService {

Integer insert(User u);

List findAll();

List findByUserIds(List userIds);

}

@Service

public class UserServiceImpl implements UserService {

@Autowired

private UserMapptKuiFELvger userMapper;

@Override

public Integer insert(User u) {

return userMapper.insert(u);

}

@Override

public List findAll() {

return userMapper.findAll();

}

@Override

public List findByUserIds(List 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 findAll();

List findByUserIds(List userIds);

}

insert into t_user_00 (user_id,name,age)tKuiFELvg values (#{userId},#{name},#{age})

select from t_user_00

select from t_user_00 where user_id in (

#{item}

)

id,user_id,name,age

十、遇到的问题

1、idea 右键无java class选项

2、maven仓库中产生后缀是LastUpdated的文件


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java解除文件占用即Dom4j操作后实现xml关流
下一篇:Java Management Extensions管理扩展原理解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~