当前位置: 首页 > news >正文

Day40(10)-F:\硕士阶段\Java\课程代码\后端\web-ai-code\web-ai-project01\springboot-mybatis-quickstart

Mybatis

image-20251116133907065

image-20251116134300146

image-20251116140306307

image-20251116140314402

image-20251116141225205

image-20251116165543669

image-20251116165640078

image-20251116165732487

public class HikariDataSource extends HikariConfig implements DataSource, Closeable {private static final Logger LOGGER = LoggerFactory.getLogger(HikariDataSource.class);private final AtomicBoolean isShutdown = new AtomicBoolean();private final HikariPool fastPathPool;private volatile HikariPool pool;public HikariDataSource() {this.fastPathPool = null;}public HikariDataSource(HikariConfig configuration) {configuration.validate();configuration.copyStateTo(this);LOGGER.info("{} - Starting...", configuration.getPoolName());this.pool = this.fastPathPool = new HikariPool(this);LOGGER.info("{} - Start completed.", configuration.getPoolName());this.seal();}public Connection getConnection() throws SQLException {if (this.isClosed()) {throw new SQLException("HikariDataSource " + this + " has been closed.");} else if (this.fastPathPool != null) {return this.fastPathPool.getConnection();} else {HikariPool result = this.pool;if (result == null) {synchronized(this) {result = this.pool;if (result == null) {this.validate();LOGGER.info("{} - Starting...", this.getPoolName());try {this.pool = result = new HikariPool(this);this.seal();} catch (HikariPool.PoolInitializationException pie) {if (pie.getCause() instanceof SQLException) {throw (SQLException)pie.getCause();}throw pie;}LOGGER.info("{} - Start completed.", this.getPoolName());}}}return result.getConnection();}}

image-20251116165959351

切换连接池

image-20251116170627703

com.alibabadruid-spring-boot-starter1.2.19

image-20251116171642794

image-20251116172442226

image-20251116172806023

image-20251116192459448

image-20251116193149271

image-20251116193909853

package com.itheima.mapper;import com.itheima.pojo.User;
import org.apache.ibatis.annotations.*;import java.util.List;@Mapper//这个注解是说明这个接口是mybatis中的持久层接口
//应用程序在运行时,会自动的为该接口创建一个实现类对象(代理对象),并自动将该实现类对象放置到IOC容器中,成为bean对象
public interface UserMapper {/*** 查询所有用户*/@Select("select id, username, password, name, age from user")public List<User> findAll();/*** 根据id删除用户* @param id* @return*/@Delete("delete from user where id =#{id}")public Integer deleteById(Integer id);/*** 新增用户* @param user*/@Insert("insert into user(username,password,name,age) values (#{username},#{password},#{name},#{age})")public void insert(User user);/*** 更新*/@Update("update user set username = #{username}, password = #{password},name = #{name}, age = #{age} where #{id} = 1")public void update(User user);/*** 根据用户名和密码查询信息* @param username* @param password* @return*/@Select("select * from user where username = #{username} and password = #{password}")public User findByUsernameAndPassword(@Param("username")String username,@Param("password")String password);
}
package com.itheima;import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTest//springboot中单元测试的注解-当前测试类中的测试方法运行时,会启动springboot的项目-IOC容器就创建好了
class SpringbootMybatisQuickstartApplicationTests {@Autowiredprivate UserMapper userMapper;@Testpublic void testFindAll(){List<User> userList = userMapper.findAll();userList.forEach(System.out::println);}@Testpublic void testDeleteById(){Integer i = userMapper.deleteById(4);System.out.println("执行完毕后影响的记录数:"+i);}@Testpublic void testInsert(){User user = new User(null,"gaoyuanyuan","666888","高圆圆",18);userMapper.insert(user);}@Testpublic void testUpdate(){User user = new User(1,"zhouyu","666888","周瑜",20);userMapper.update(user);}@Testpublic void testFindByUsernameAndPassword(){User user = userMapper.findByUsernameAndPassword("zhouyu", "666888");System.out.println(user);}
}

xml映射配置文件

image-20251116194251294

创建多级目录用/,创建多级包用.

Mybatis中文网

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.example.BlogMapper"><select id="selectBlog" resultType="Blog">select * from Blog where id = #{id}</select>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.UserMapper">
<!--    <select id="selectBlog" resultType="Blog">-->
<!--        select * from Blog where id = #{id}-->
<!--    </select>-->
<!--    resultType:查询返回的单条记录所封装的类型--><select id="findAll" resultType="com.itheima.pojo.User">select id, username, password, name, age from user</select>
</mapper>

image-20251116195809010

image-20251116195831618

image-20251116200649388

spring.application.name=springboot-mybatis-quickstart#配置数据库的连接信息
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/web01
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=1234
#配置mybaris的配置输出
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl#指定XML映射配置文件的位置
mybatis.mapper-locations=classpath:mapper/*.xml
http://www.gsyq.cn/news/51462.html

相关文章:

  • 还能回到原先吗 绞尽脑汁翻阅文献 这名为爱的实验 被等号连接
  • 2025年11月手动旗杆厂家口碑推荐榜单及选购指南
  • 2025年四川电动旗杆制造厂排行榜TOP5权威发布
  • debian sysctl: cannot open /etc/sysctl.conf: 没有那个文件或目录
  • mysql函数大全及举例 - 详解
  • P14507 缺零分治 mexdnc题解
  • Swagger开启账号验证访问
  • 标准解读——GB/T 46353—2025《信息技术 大数据 资料资产价值评估》国家标准
  • 代码背后的故事:docker容器名生成算法
  • 20232428 2025-2026-1 《网络与系统攻防技术》实验五实验报告
  • HarmonyOS 5 鸿蒙Context上下文机制与资源管理详解 - 教程
  • 11.16组会
  • MFC + OpenCV 图像预览显示不全中断问题解除:GDI行填充详解
  • 飞牛os打开本机usb摄像头
  • 12306售票系统分析与实战
  • Java StringTokenizer 类 Scanner 类详解
  • Java 断言(Assert) 简介
  • 实用指南:IntelliJ IDEA 2023中为 Spring Boot 项目添加注释模板
  • 量化存储墙(三):GEMM EMA 下限解析解以及硬件静态资源分配设计
  • c# 获取当前时间
  • YOLOv3 深度解析:网络架构、核心改进与目标检测实践 - 指南
  • ai学习机是不是智商税?到底有没有用?2025年学习机推荐指南
  • docker命令提示插件
  • C语言和C++有什么区别
  • Snipe-IT支持Oauth2登录
  • 绝对值的性质
  • 智能硬件利用小聆AI自定义MCP应用开发操作讲解
  • Linux - sudo -i
  • 科学计算复习
  • 2025年11月石笼网厂家最新推荐,聚焦资质、案例、售后的五家企业深度解读!