Mybatis常用代码总结:

配置文件:

log4j.properties

#将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码
log4j.rootLogger=DEBUG,console,file

#控制台输出的相关设置
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n

#文件输出的相关设置
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/zhaox.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n

#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

db.properties

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
username=用户名
password=密码

mybatis核心配置文件

  1. 引入properties文件
  2. 设置一些参数
  3. 别名优化
  4. 环境配置
    1. 事务管理
    <transactionManager type="JDBC"/>
          <dataSource type="POOLED">
              <!--properties文件里的东西-->
              <property name="driver" value="{driver}"/>
              <property name="url" value="{url}"/>
              <property name="username" value="{username}"/>
              <property name="password" value="{password}"/>
          </dataSource>
    
  5. 映射器配置

<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration核心配置文件-->
<configuration>

    <!--引入properties文件-->
    <properties resource="db.properties"/>

    <!--配置日志-->
    <settings>
        <!--使用默认日志输出或log4j输出-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <!--<setting name="logImpl" value="LOG4J"/>-->
    </settings>
    <!--别名优化-->
    <typeAliases>
        <package name="com.xxx.pojo"/>
    </typeAliases>
    <!--环境配置-->
    <environments default="development">
        <environment id="development">
            <!--事务管理-->
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <!--properties文件里的东西-->
                <property name="driver" value="{driver}"/>
                <property name="url" value="{url}"/>
                <property name="username" value="{username}"/>
                <property name="password" value="{password}"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <!--映射器配置-->
        <mapper resource="com/zhaox/Mapper/xxx.xml"/>
    </mappers>
</configuration>

POM文件

防止资源绑定出问题,让放在java文件夹中的xml文件也可以用

<!--在build中配置resources,来防止我们资源导出失败的问题-->
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

MapperXml文件

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhaox.Mapper.StudentMapper">

</mapper>
  1. 注意顶部文件有两处要改动为mapper
  2. mapper 内要加namespace,用·分割

测试类常用代码

SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);

//中间是增删改查代码

sqlSession.commit();
sqlSession.close();

工具类

public class MybatisUtils {

    private static SqlSessionFactory sqlSessionFactory;
    static{
        String resource = "mybatis-config.xml";//数据库配置文件
        InputStream inputStream = null;//通过配置得到一个输入流
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//通过工厂构造器得到工厂
    }


    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}



醉后不知天在水,满船清梦压星河