【SpringBoot】整合Mybatis

发布时间:2025-01-07 19:52

配合服装和场合调整妆容,整体造型统一。 #生活技巧# #化妆美容技巧# #化妆步骤详解#

获取配置文件的内容并显示

第一种方法

yml文件

#对象的配置 person: name: zhangsan age: 18 addr: beijing 12345 回显

package com.itheima.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @ConfigurationProperties(prefix = "person")//自动去找Person开头的匹配 public class Quick3Controller { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } private String addr; @RequestMapping("/quick3") @ResponseBody public String quick3(){ //获得配置文件的信息 System.out.println(name); return "name:"+name+",addr="+addr; } }

123456789101112131415161718192021222324252627282930313233343536373839404142

结果

在这里插入图片描述

第二种方法

.java

@Value("${projectStatusBegin}") private int projectStatusBegin; 12

.yml

projectStatusBegin:1 1

整合Mybatis

添加起步依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.1.1</version></dependency>添加数据库驱动坐标<!--MySQL连接驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency> 1234567891011

添加数据库连接信息

#数据库连接信息spring.datasource.driver.driverClassName=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://127.0.0.1:3306/e3mall?useUnicode=true&characterEncoding=utf8spring.datasource.username=e3mallspring.datasource.password=123456 12345

创建User表

-- ---------------------------- -- Table structure for `user` -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) DEFAULT NULL, `password` varchar(50) DEFAULT NULL, `name` varchar(50) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;-- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES ('1', 'zhangsan', '123', '张三'); INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');

12345678910111213141516

创建实体Bean

package com.itheima.domain; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class User { public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } // 主键 @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; // 用户名 private String username; // 密码 private String password; // 姓名 private String name; @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", name='" + name + '\'' + '}'; } }

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263

编写Mapper

<?xmlversion="1.0"encoding="utf-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.itheima.mapper.UserMapper"><selectid="queryUserList"resultType="user">select*fromuser</select></mapper> 1234567

在application.properties中添加mybatis配置,配置Mapper映射文件

#spring集成Mybatis环境#pojo别名扫描包mybatis.type-aliases-package=com.itheima.domain#加载Mybatis映射文件mybatis.mapper-locations=classpath:mapper/*Mapper.xml 12345

报错The server time zone value ' й ׼ʱ ' is unrecognized or represents more than one time zone. 解决方案:修改jdbc:mysql://127.0.0.1:3306/e3mall?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8 12

编写测试Controller

@ControllerpublicclassMybatisController{@AutowiredprivateUserMapperuserMapper;@RequestMapping("/query")@ResponseBodypublicList<User>queryUserList(){List<User>users=userMapper.queryUserList();returnusers;}}

12345678910111213141516

运行结果
在这里插入图片描述

网址:【SpringBoot】整合Mybatis https://www.yuejiaxmz.com/news/view/665335

相关内容

Springboot+ssm(Spring+SpringMVC+MyBatis)旧物置换网站
Mybatis
二手家电交易系统(SpringBoot,SSM,MySQL)
【原创】springboot+mysql宠物领养和论坛系统设计与实现
【原创】java+springboot+mysql日程管理系统设计与实现
基于springboot的疫情社区生活服务系统
SpringBoot项目中MybatisPlus的使用
基于SpringBoot的个人时间规划系统
基于SpringBoot实现二手交易商城
springboot个人运动健康管理71719[独有源码]了解毕业设计的关键考虑因素

随便看看