这篇文章主要介绍SpringBoot通过yml方式整合Mybatis的方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创新互联公司是少有的成都网站建设、网站制作、营销型企业网站、成都微信小程序、手机APP,开发、制作、设计、买友情链接、推广优化一站式服务网络公司,于2013年开始,坚持透明化,价格低,无套路经营理念。让网页惊喜每一位访客多年来深受用户好评
一、项目目录结构
注意这里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
<?xml version="1.0" encoding="UTF-8"?>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
四、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); ListfindAll(); List findByUserIds(List userIds); }
@Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public Integer insert(User u) { return userMapper.insert(u); } @Override public ListfindAll() { 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); ListfindAll(); List findByUserIds(List userIds); }
<?xml version="1.0" encoding="UTF-8" ?>insert into t_user_00 (user_id,name,age) values (#{userId},#{name},#{age}) id,user_id,name,age
十、遇到的问题
1、idea 右键无java class选项
2、maven仓库中产生后缀是LastUpdated的文件
以上是SpringBoot通过yml方式整合Mybatis的方法的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!