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.alibaba druid-spring-boot-starter 1.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 {

    @Autowired
   private UserMapper userMapper;
    @Test
    public void testFindAll(){
        List<User> userList = userMapper.findAll();
        userList.forEach(System.out::println);
    }

    @Test
    public void testDeleteById(){
        Integer i = userMapper.deleteById(4);
        System.out.println("执行完毕后影响的记录数:"+i);
    }

    @Test
    public void testInsert(){
        User user = new User(null,"gaoyuanyuan","666888","高圆圆",18);
        userMapper.insert(user);
    }

    @Test
    public void testUpdate(){
        User user = new User(1,"zhouyu","666888","周瑜",20);
        userMapper.update(user);
    }

    @Test
    public void testFindByUsernameAndPassword(){
        User user = userMapper.findByUsernameAndPassword("zhouyu", "666888");
        System.out.println(user);
    }
}

xml映射配置文件

image-20251116194251294

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

Mybatis中文网

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//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 mapper
        PUBLIC "-//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
posted @ 2025-11-16 20:25  David大胃  阅读(2)  评论(0)    收藏  举报