玄子Share - Mybatis 项目模板使用指南

玄子Share - Mybatis 项目模板使用指南

项目结构图

玄子Share - Mybatis 项目模板使用指南_第1张图片

mybatis-config.xml 配置模板设置

玄子Share - Mybatis 项目模板使用指南_第2张图片

参数


DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="database.properties"/>
    <settings>
        
        
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    settings>
    <typeAliases>
        
        
        <package name="${PACKAGE_NAME}.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="${PACKAGE_NAME}.dao"/>
    mappers>
configuration>
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        

需求

  1. ${PACKAGE_NAME}.entity ${PACKAGE_NAME}.dao 需要用到项目路径,所以需在项目内创建完成后再剪切到resources文件夹
  2. ${@password}需要将此处的@符号删除掉,使用Ctrl + F查找替换@

database.properties 数据库配置模板设置

玄子Share - Mybatis 项目模板使用指南_第3张图片

参数

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/myschool?useUnicode=TRUE&characterEncoding=UTF-8&serverTimezone=GMT-8
username=root
password=root

需求

  1. 将连接数据库的myschool改为自己的数据库

MybatisMapper.java 工具类模板设置

玄子Share - Mybatis 项目模板使用指南_第4张图片

参数

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end

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;

public class MybatisMapper {
    private static final SqlSessionFactory sqlSessionFactory;

    static {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static SqlSession getSqlSession() {
        return sqlSessionFactory.openSession(true);
        // true 为自动提交事务
    }
}

需求

  1. 注意mybatis-config.xml配置文件名称是否一致
  2. openSession(true);已开启自动提交事务

EntityMapper.xml XML 映射模板设置

玄子Share - Mybatis 项目模板使用指南_第5张图片

参数

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${PACKAGE_NAME}.${NAME}">

</mapper>

需求

  1. 注意文件命名规范

运行 Main

public static void main(String[] args) {
    SqlSession sqlSession = MybatisMapper.getSqlSession();
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    List<Student> studentList = mapper.selectStudent();
    studentList.forEach(System.out::println);
}

玄子Share - Mybatis 项目模板使用指南 8.4

你可能感兴趣的:(SSM,mybatis,spring,java)