mybatis单独使用

resultMap:适合使用返回值是自定义实体类的情况

resultType:适合使用返回值得数据类型是非自定义的,即jdk的提供的类型

resultMap : 

映射实体类的数据类型

resultMap的唯一标识

column: 库表的字段名

property: 实体类里的属性名




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}。
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值