Mybatis常用配置文件o_0

本文档详细介绍了MyBatis的核心配置文件mybatis-config.xml,包括数据库配置、设置、类型别名、环境和Mapper的配置。同时,展示了db.properties数据库配置文件,MybatisUtils工具类的实现,以及UserMapper接口和XML配置文件的使用。此外,还提及了Maven配置和结果集映射的UserMap.xml,提供了解决数据库字段与Java实体类属性不一致的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mybatis-config.xml

mybatis核心配置文件

<?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核心配置文件-->
<configuration>
    <!--引入外部配置文件-->
    <properties resource="db.properties"/>

    <settings>
        <!--标准的日志工厂实现-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <!--<setting name="logImpl" value="log4j"/>-->
        <!--下划线命名到驼峰命名 经典转换 自动-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>


    <!--给实体类pojo起别名, 为了不写长的全类名-->
    <typeAliases>
        <!--为了可以在Mapper.xml可以省全类名 -->
        <!--<typeAlias type="com.xxxx.pojo.User" alias="User"/>-->
        <!--开启包扫描别名, 类名首字母小写(大写也可以)(user / User)就可以了省去全类名.
            想DIY就在实体类上注解起别名@Alias("")-->
        <package name="com.xxxx.pojo"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"></property>
                <property name="url" value="${url}"></property>
                <property name="username" value="${username}"></property>
                <property name="password" value="${password}"></property>
            </dataSource>
        </environment>
    </environments>

    <!--每一个Mapper.xml都需要在mybatis核心配置文件中注册-->
    <mappers>
        <mapper resource="com/xxxx/dao/UserMapper.xml"/>
        <!--通过同一个包内的接口类 而且接口和配置文件必须同名 要求很高 同包同名-->
        <!--<mapper class="com.UserMapper"/>-->
        <!--使用包扫描 和上面的class要求一样, 接口和配置文件必须在同一个包下, 而且同名 同包同名-->
        <!--为了简单, 就要接受框架的约束, 相应的会失去一些自由 呵-->
        <!--<package name="com.xxxx.dao"/>-->
    </mappers>

</configuration>

db.properties

数据库配置文件
以下mysql版本5 版本8要改driver和url

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&;useUnicode=true&;characterEncoding=UTF-8
username=root
password=123456

com.xxxx.utils.MybatisUtils

mybatis 工具类 生成sqlSession

package com.xxxx.utils;

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 java.io.IOException;
import java.io.InputStream;

//sqlSessionFactory --> sqlSession
public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;

    static{
        try {
            //使用mybatis第一步:获取sqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        }catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    //获取sqlSession实例
    public static SqlSession getSqlSession(){
        return  sqlSessionFactory.openSession();

    }
}

maven配置

父工程的配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--父工程-->
    <groupId>com.xxxx</groupId>
    <artifactId>Mybatis-Study</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>mybatis-01</module>
        <module>mybatis-02</module>
        <module>Mybatis-03</module>
    </modules>

    <dependencies>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--配合log4j.propertis生成日志-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!--注解自动生成getset方法 构造方法 toString方法等-->
        <!--在实体类上加@Data @AllArgsConstructor @NoArgsConstructor -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
        <!--junit-->
    </dependencies>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>


    <build>
        <!--在build中配置resource,来防止我们资源导出失败的问题-->
        <!--这样也可以把所有的xml文件,打包到相应位置。  -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.tld</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.tld</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
</project>

com.xxxx.dao.UserMapper

接口方法类-对应表的操作

package com.xxxx.dao;

import com.xxxx.pojo.User;

import java.util.List;
import java.util.Map;

//ps:表的接口方法
public interface UserMapper {
    //查询所有用户(记录)
    List<User> getUserList();
    //查询一个用户(记录)
    User getUserById(int id);
    //万能的Map
    int getUserById2(Map<String, Object> map);
    //模糊查询
    List<User> getUserLike(String value);

    //插入一个用户(记录)
    int addUser(User user);
    //万能的Map
    int addUser2(Map<String, Object> map);
    //修改用户(记录)
    int updateUser(User user);
    //删除用户(记录)
    int deleteUser(int id);



}

com/xxxx/dao/UserMapper.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">

<!--表的接口方法实例化-->
<!--namesapce=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.UserMapper">
    <!--select查询语句-->
    <select id="getUserList" resultType="user">
        select * from mybatis.user
    </select>
    <!--用map传参好处在于 它可以把多个参数打包在一起传进来, 通过#{key}获得对应的数值 map中可以随意定制key名字-->
    <!--因为sql好像最多只能传一个参数!!! 传一个对象, 一个基本数据, 或者打包后的map-->
    <!--上面不够准确, 注解好像可以解决多个参数问题???-->
    <select id="getUserById2" parameterType="map" resultType="com.User">
        select * from mybatis.user where id = #{id} and name = #{name};
    </select>
    <select id="getUserById" parameterType="int" resultType="map">
        select * from mybatis.user where id = #{id}
    </select>
    <select id="getUserLike" resultType="com.User">
        select * from mybatis.user where name like #{value};
    </select>


    <!--insert插入语句-->
    <!--注意:传入的是一个对象,而对象的属性,可以直接取出来,超级方便-->
    <insert id="addUser" parameterType="com.User" >
        insert into mybatis.user (id, name,pwd) values(#{id}, #{name}, #{pwd})


    </insert>
    <!--传递map里的key-->
    <insert id="addUser2" parameterType="map" >
        insert into mybatis.user (id, name,pwd) values(#{userId}, #{userName}, #{password})
    </insert>

    <update id="updateUser" parameterType="com.User" >
        update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id}
    </update>

    <delete id="deleteUser" parameterType="int">
        delete from mybatis.user where id = #{id}
    </delete>
</mapper>

带结果集映射的UserMap.xml

处理数据库表字段名与java实体类属性不一致

<?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">

<!--表的接口方法实例化-->
<!--namesapce=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.xxxx.dao.UserMapper">

    <!--结果集映射 将User映射 把数据库表字段和java的实体类属性映射打包-->
    <resultMap id="UserMap" type="User">
        <!--<result column="id" property="id"/>-->
        <!--<result column="name" property="name"/>-->
        <result column="pwd" property="password"/>
    </resultMap>
    <!--select查询语句-->
    <select id="getUserList" resultMap="UserMap">
        select * from mybatis.user
    </select>

</mapper>

工程结构

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值