zoukankan      html  css  js  c++  java
  • 星涛:mybatis极速入门!

    安装

    pom.xml部分(maven导入依赖)

     <build>
            <!--设置版本-->
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>14</source>
                        <target>14</target>
                    </configuration>
                </plugin>
            </plugins>
          <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>
        <dependencies>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.2</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.48</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
        </dependencies>
    

    mybatis-config.xml(核心配置文件)

    <?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="logImpl" value="STDOUT_LOGGING"/>
        </settings>
        <!-- 数据库环境 -->
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://localhost:3306/apiserve?useSSL=true&amp;characterEncoding=UTF-8"/>
                    <property name="username" value="root"/>
                    <property name="password" value="root"/>
                </dataSource>
            </environment>
        </environments>
        <!-- 映射文件 -->
        <mappers>
            <mapper resource="UserMapper.xml" />
        </mappers>
    
    </configuration>
    

    工具类

    package botao.util;
    
    
    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;
    
    /**
     * @author cbt28
     */
    public class MybatisUtils {
        private static SqlSessionFactory sqlSessionFactory;
        static {
            try {
                String resources="mybatis.xml";
                InputStream io = Resources.getResourceAsStream(resources);
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(io);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public static SqlSession getSqlSession(){
            return sqlSessionFactory.openSession();
        }
    }
    

    映射文件

    <?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="botao.dao.UserMapper">
        <select id="findById" resultType="botao.pojo.User" parameterType="int">
            select * from  user where id=#{id}
        </select>
    </mapper>
    
  • 相关阅读:
    bootstrap treevie只展开一个节点,关闭其他节点
    Java后端校验-使用hibernate-validator校验JavaBean
    js生成Excel文件
    Databus&canal对比
    KVM安装配置笔记
    机器学习之数学基础一导数
    机器学习-线性回归
    机器学习之数学基础一统计
    Leader与Boss,技术leader与管理者
    php递归获取无限分类菜单
  • 原文地址:https://www.cnblogs.com/botaoJava/p/13453153.html
Copyright © 2011-2022 走看看