mybatis 简单使用示例(单独使用):

Posted it馅儿包子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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}。





















































































以上是关于mybatis 简单使用示例(单独使用):的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis系列接口式编程

在android中使用带有片段的roboguice的任何简单示例?

mybatis 单独使用的配置文件

MyBatis结合Spring使用源码分析

处理屏幕旋转上的片段重复(带有示例代码)

使用 ViewModel 和 LiveData 递增变量的简单片段示例 - 变量始终为空