MyBatis使用

Administrator
Administrator
发布于 2024-03-09 / 19 阅读
0
0

MyBatis使用

MyBatis使用

1、导入mybatis所需jar。Maven项目可以直接进行相关包引入。

<dependencies>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.8</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>

2、导报完成后编写mybatis-config.xml配置文件

注意:表中字段以下划线分割时,需要打开驼峰转换,mapUnderscoreToCamelCase=true

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <typeAliases>
        <package name="com.lwl.entity"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=UTC&amp;rewriteBatchedStatements=true&amp;serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/lwl/dao/UserDao.xml"/>
    </mappers>
</configuration>

3、根据数据表创建对应实体

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String userName; //驼峰
    private String address;
    private String iphone;
    private int age;
}

4、编写sql,创建UserDao.xml

<?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.lwl.dao.UserDao">

    <select id="findAll" resultType="User">
        select
        <include refid="all_col"></include>
        from user
    </select>
    <sql id="all_col">
        id,user_name,address,iphone,age
    </sql>
</mapper>

5、编写测试类

package com.lwl.test;

import com.lwl.dao.UserDao;
import com.lwl.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class AppTest {
    @Test
    public void test() {
        try {
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
            SqlSession sqlSession = sqlSessionFactory.openSession();
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            List<User> all = mapper.findAll();
            all.forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

测试结果:


评论