zoukankan      html  css  js  c++  java
  • mybatis 简单使用示例(单独使用):

    mybatis的单独使用简单示例:

    步骤1:

    新建xml文件。

    示例:

    <?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="com.dao.UserMapper">

    <select id="getUserOne" parameterType="String" resultType="Integer">
    select code2 from a where code1=#{code1,javaType=String,jdbcType=INTEGER,typeHandler=com.typeHandler.TestTypeHandler}
    limit 1
    </select>

    </mapper>

    此处注意两点:

    一:<mapper namespace="com.dao.UserMapper"> 的namespace须与相对应的dao类名一致,比如dao类的全路径为:
    com.dao.UserMapper,则此处namespace="com.dao.UserMapper".
    二:方法的id,parameterType,result等参数须与dao类中的某一个方法相一致。

    步骤2:
    dao类。
    示例:
    public interface UserMapper{

    Integer getUserOne(@Param("code1") String string);
    }
    此处的@Param("code1")为注解方法,定义传入此处的参数的名称为code1,以便后续操作。

    步骤3:
    配置文件mybatis-config.xml。
    示例:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration
    PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>

    <settings>
    <setting name="cacheEnabled" value="false"/>
    <!--lazyLoadingEnabled: lazy loading开关,默认为true。
    全局性设置懒加载。如果设为‘false’,则所有相关联的都会被初始化加载-->
    <setting name="lazyLoadingEnabled" value="true"/>
    <!--aggressiveLazyLoading 默认true:当访问任何一个属性都会加载所有的其他lazy load属性,
    即使你根本没有调用哪个lazy load属性,说白了就是aggressiveLazyLoading=true,则lazy load等于没用,
    所以要使用lazy load还是将其设为false -->
    <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

    <environments default="development">
    <environment id="development">
    <transactionManager type="JDBC"/>
    <dataSource type="POOLED">
    <property name="username" value="root"/>
    <property name="password" value="123456"/>
    <property name="url" value="jdbc:mysql://localhost:33333/local_law_hz"/>
    <property name="driver" value="com.mysql.jdbc.Driver"/>
    </dataSource>
    </environment>
    </environments>

    <mappers>
    <mapper resource="mybatis/mapper/UserMapper.xml"/>
    </mappers>

    </configuration>
    此处注意<mappers>的配置。

    步骤4:
    测试类:
    public class MybatisTest {

    public static void main(String [] args) throws IOException {
    // 使用类加载器,加载mybatis的配置文件
    // InputStream inputStream=MybatisTest.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
    InputStream inputStream= Resources.getResourceAsStream("mybatis-config.xml");

    // 构件sqlSession工厂
    SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession sqlSession=sqlSessionFactory.openSession();
    // System.out.println(sqlSession);
    UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
    Integer result=userMapper.getUserOne("1");
    System.out.println(result);

    }
    }
    此处注意两点:
    1:加载mybatis的配置文件,构件sqlSession:
    InputStream inputStream= Resources.getResourceAsStream("mybatis-config.xml");
    SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession sqlSession=sqlSessionFactory.openSession();
    2.利用xml文件,dao类,mybatis-config.xml配置文件(mappers中加载xml文件)产生mapper类:
    UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
    Integer result=userMapper.getUserOne("1");
    输出结果:
    4
    以上,未mybatis的简单应用。
     
    补充:xml中如下:
    select code2 from a where code1=#{code1,javaType=String,jdbcType=INTEGER,typeHandler=com.typeHandler.TestTypeHandler}
    是学习typeHandler时使用的,可简单看做
    select code2 from a where code1=#{code1}。
  • 相关阅读:
    laravel框架——保存用户登陆信息(session)
    laravel框架——增删改查
    laravel框架——表单验证
    laravel框架——上传、下载文件
    Forms & HTML 组件
    phantomJS+Python 隐形浏览器
    Python idle中lxml 解析HTML时中文乱码解决
    python 根据字符串语句进行操作再造函数(evec和eval方法)
    python通过LXML库读取xml命名空间
    Python通过lxml库遍历xml通过xpath查询(标签,属性名称,属性值,标签对属性)
  • 原文地址:https://www.cnblogs.com/zqsky/p/6016128.html
Copyright © 2011-2022 走看看