当前位置: 首页 > 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.jsqmd.com/news/42089/

相关文章:

  • 还能回到原先吗 绞尽脑汁翻阅文献 这名为爱的实验 被等号连接
  • irm steam.work|iex 风险分析
  • 2025年11月手动旗杆厂家口碑推荐榜单及选购指南
  • 2025年四川电动旗杆制造厂排行榜TOP5权威发布
  • Pandas --DataFrame基本操作
  • 2025年11月全国旗杆厂家综合实力排行榜TOP5权威发布
  • debian sysctl: cannot open /etc/sysctl.conf: 没有那个文件或目录
  • 完整教程:(Linux) WSL 通过 VSCode 连接不执行 profile 问题(登录Shell问题)
  • 入侵防护技术深度解析:最新漏洞与威胁态势
  • mysql函数大全及举例 - 详解
  • 20232427 2025-2026-1 《网络与系统攻防技术》实验五实验报告
  • P14507 缺零分治 mexdnc题解
  • python多进程通信 —— 两进程通信 —— Pipe与Queue的通信性能对比
  • 解决Elctron打包成功,IPC无法注册问题。
  • Swagger开启账号验证访问
  • 标准解读——GB/T 46353—2025《信息技术 大数据 资料资产价值评估》国家标准
  • noip7
  • 代码背后的故事:docker容器名生成算法
  • 在Windows系统置顶窗口不被Win+D快捷键影响
  • HTTP请求走私漏洞介绍 - 实践
  • 20232428 2025-2026-1 《网络与系统攻防技术》实验五实验报告
  • xml.etree.ElementTree 完全支持嵌套查找子元素,且有多种简洁实用的方式。
  • 深入解析:Spring MVC 拦截器interceptor
  • HarmonyOS 5 鸿蒙Context上下文机制与资源管理详解 - 教程
  • 《重生之我成为世界顶级黑客》第八章:未来野望
  • 打开工作空间时,但未在 DTD/架构中声明
  • 开源软件的崛起:技术共享与协作创新的新时代 - 详解
  • 20232418 2025-2026-1 《网络与系统攻防技术》实验五实验报告
  • Claude Code教程:从零构建AutoPost GPT自动内容生成系统
  • MFC + OpenCV 图像预览显示不全中断问题解除:GDI行填充详解