• 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">
    <!--根节点,XMl只能有一个-->
    <configuration>
        <!--一个environments有N个environment-->
        <typeAliases>
            <package name="cn.bdqn.entity"></package>
        </typeAliases>
        <environments default="development">
    
            <environment id="development">
                <!--
               transactionManager:JDBC保证事务的
               update
               delete
               事务分类:JDBC:编程式事务
                           xxx.beginTransaction()
    
                           tx.commit()
                           tx.rollback()
                         配置式事务
                         JDBC|MANAGED
                         区别
    
               -->
                <transactionManager type="JDBC"></transactionManager>
                <!--
                   POOLED:MyBatis内置的连接池
                   c3p0连接池
                           POOLED 、UNPOOLED  、JNDI
               -->
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql:///blog"/>
                    <property name="username" value="root"/>
                    <property name="password" value="1234"/>
                </dataSource>
    
            </environment>
        </environments>
    
    <mappers>
    <mapper resource="cn/bdqn/dao/DeptMappr.xml"></mapper>
            </mappers>
            </configuration>

    二.小配置

    <?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">
    <!--namespace:
         命名空间:区分不同空间下的同名SQLID
         A: findlAll
         B:  findAll
        -->
    <mapper namespace="cn.bdqn.dao.IDeptDao">
        <!--SQL标签
          id:唯一锁定到SQL标识
          paramenterType:SQL语句的入参  可以省略
          resultType:
          增删除操作:不能 写
          查询:单个实体的类型
        -->
        <select id="getAll"  resultType="Dept">
      /*SQL文:SQL语句*/
        SELECT * FROM  bloginfo
        </select>
        <select id="getid"  resultType="Dept">
            /*SQL文:SQL语句*/
            SELECT * FROM  bloginfo WHERE blogId=#{blogId}
        </select>
    </mapper>

    三.jar包

       <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.2.2</version>
            </dependency>
    
    
            <!--mysql  jar包-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.38</version>
            </dependency>

    四.实体类

         public class StudentInfo {

         private Integer stuId;

         private String stuName;

         private Integer  stuAge;

         private Date stuDate;

    }

    五.更新POM.xml文件中build节点

     <build>

        <resources>

          <resource>

            <directory>src/main/java</directory>

            <includes>

              <include>**/*.xml</include>

            </includes>

          </resource>

        </resources>

      </build>

    六.测试类 

    InputStream is=Resources.getResourcesAsStream(path);

    SessionFactory factory=new SessionFactoryBuilder().build(is);

    SqlSession session=factory.openSession();

    <?xml version="1.0" encoding="UTF-8" ?>
    <!--文档类型,头文件-->
    <!DOCTYPEconfiguration
    PUBLIC"-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <!--根节点,XMl只能有一个-->
    <configuration>
    <!--一个environments有N个environment-->
    <typeAliases>
    <package name="cn.bdqn.entity"></package>
    </typeAliases>
    <environments default="development">

    <environment id="development">
    <!--
    transactionManager:JDBC保证事务的
    update
    delete
    事务分类:JDBC:编程式事务
    xxx.beginTransaction()

    tx.commit()
    tx.rollback()
    配置式事务
    JDBC|MANAGED
    区别

    -->
    <transactionManager type="JDBC"></transactionManager>
    <!--
    POOLED:MyBatis内置的连接池
    c3p0连接池
    POOLED 、UNPOOLED 、JNDI
    -->
    <dataSource type="POOLED">
    <property name="driver" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql:///blog"/>
    <property name="username" value="root"/>
    <property name="password" value="1234"/>
    </dataSource>

    </environment>
    </environments>

    <mappers>
    <mapper resource="cn/bdqn/dao/DeptMappr.xml"></mapper>
    </mappers>
    </configuration>
  • 相关阅读:
    商务通服务器版LR_Data目录下相关配置文件
    Python入门神图
    你不知道的JavaScript-2.词法作用域
    你不知道的JavaScript-1.作用域是什么
    linux服务器对外打包处理
    C# Form 关闭按钮灰化
    Spread常用属性
    Spread 常用属性
    C#打开关闭窗体事件顺序
    sqlserver如何使用日期计算
  • 原文地址:https://www.cnblogs.com/qjt970518--/p/7144255.html
Copyright © 2020-2023  润新知