Mybatis基础环境配置模板

1.mybatis环境基础配置概述

1.创建一个maven工程

2.在pom.xml中导入mybatis相关的依赖,以及操作数据库相关的文件配置依赖

3.在resource下创建mybatis的配置文件,一般名字为mybatis-config.xml

4.创建与数据库对应的实体类

5.创建mapper接口

6.在resource下创建与mapper接口相对应的sql映射文件

2.基础环境详细教程

2.1 创建maven工程

此处省略,创建成功后截图如下

Mybatis基础环境配置模板_第1张图片

2.2 导入相关的依赖文件(简单版本版本)

<dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatisartifactId>
    <version>3.5.7version>
dependency>

<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <version>8.0.25version>
dependency>

<dependency>
    <groupId>log4jgroupId>
    <artifactId>log4jartifactId>
    <version>1.2.17version>
dependency>

<dependency>
    <groupId>org.junit.jupitergroupId>
    <artifactId>junit-jupiterartifactId>
    <version>RELEASEversion>
    <scope>compilescope>
dependency>

2.3 在resource下配置mybatis配置文件MyBatisConfig.xml(简单版本版本)


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    
    <properties resource="jdbc.properties"/>

    <settings>
        <setting name="logImpl" value="LOG4J"/>
    settings>
    
    <typeAliases>
        <package name="com.tz.entity"/>
    typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            dataSource>
        environment>
    environments>
    <mappers>
        
        <package name="com.tz.mapper"/>
    mappers>
configuration>

2.4 创建与数据库对应的实体类

字段名和数据库名字保持一致,如果不一致可以在MyBatisConfig.xml配置文件中设置,或者是配置开启驼峰命名

2.5 创建实体类对应的mapper接口

mapper接口作用就是写增删改查的抽象方法

2.6 在resource下创建与mapper接口相对应的sql映射文件



DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="StudentMapper">
    
    <select id="selectAll" resultType="com.iflytek.bean.Student">
        SELECT * FROM student
    select>
mapper>

2.7 连接工具类

com.iflytek 包下创建 utils 包,并在该包下创建名为 SqlSessionFactoryUtils 工具类

public class SqlSessionFactoryUtils {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        //静态代码块会随着类的加载而自动执行,且只执行一次
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getSqlSessionFactory(){
        return sqlSessionFactory;
    }
}

3.必须配置的配置文件总结

Mybatis基础环境配置模板_第2张图片

3.1 导入依赖文件

这里的配置文件给出了详细的依赖。

<dependencies>

    <dependency>
      <groupId>log4jgroupId>
      <artifactId>log4jartifactId>
      <version>1.2.12version>
    dependency>

    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>3.5.6version>
    dependency>

    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>8.0.31version>
    dependency>

    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.11version>

    dependency>

    
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>3.1.0version>
      <scope>providedscope>
    dependency>
    
    <dependency>
      <groupId>javax.servlet.jspgroupId>
      <artifactId>jsp-apiartifactId>
      <version>2.2version>
      <scope>providedscope>
    dependency>
    
    <dependency>
      <groupId>jstlgroupId>
      <artifactId>jstlartifactId>
      <version>1.2version>
    dependency>

    <dependency>
      <groupId>taglibsgroupId>
      <artifactId>standardartifactId>
      <version>1.1.2version>
    dependency>
  dependencies>

3.2 在resource下的文件配置

3.2.1 mybatis-config核心配置文件,所有其它配置文件都需要在这里面配置加载


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    
    <properties resource="jdbc.properties"/>

    <settings>
        <setting name="logImpl" value="LOG4J"/>

        <setting name="mapUnderscoreToCamelCase" value="true"/>
    settings>
    
    <typeAliases>
        <package name="com.tz.entity"/>
    typeAliases>

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

configuration>

3.2.2 在resource配置mapper映射文件


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.tz.mapper.BrandMapper">


    <select id="selectAll" resultType="com.tz.entity.Brand">
        select * from tb_brand;
    select>
mapper>

注意:

namespace名字一定要和mapper接口保持一致

id一定要和mapper接口的方法保持一致,为后面的方向代理做准备

3.2.3 log4j.properties配置

文件名字必须为log4j.properties

log4j.rootLogger=DEBUG,console

########################控制台输出的相关设置
log4j.appender.console = org.apache.log4j.ConsoleAppender
#在控制台输出
log4j.appender.console.Target = System.out
#在DEBUG级别输出
log4j.appender.console.Threshold = DEBUG
log4j.appender.console.layout = org.apache.log4j.PatternLayout
#日志格式
log4j.appender.console.layout.ConversionPattern=[%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

3.2.4 jdbc.properties

文件名字必须为jdbc.properties

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql:///db1
username=root
password=123456

3.4 配置mybatis连接工具类

工具类类名为:SqlSessionFactoryUtils

public class SqlSessionFactoryUtils {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        //静态代码块会随着类的加载而自动执行,且只执行一次
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getSqlSessionFactory(){
        return sqlSessionFactory;
    }
}

你可能感兴趣的:(编程固定模板配置,mybatis,java,mysql)