文章目录
MyBatis作用开发MyBatis程序的思路(crud):Mapper动态代理时的crud
MyBatis作用
简化JDBC操作,实现数据的持久化。
开发MyBatis程序的思路(crud):
(1) 配置MyBatis:通过config.xml文件配置XXXMapper的映射路径和数据库的连接。
<environments
default="development">
<environment id
="development">
<!-- 事务的提交方式
-->
<transactionManager type
="JDBC" />
<!-- 数据库连接池
-->
<dataSource type
="POOLED">
<property name
="driver" value
="${driver}" />
<property name
="url" value
="${url}" />
<property name
="username" value
="${username}" />
<property name
="password" value
="${password}" />
</dataSource
>
</environment
>
<mappers>
<mapper resource
="com/mapper/empMapper.xml" />
<mapper resource
="com/mapper/companyMapper.xml"/>
<mapper resource
="com/mapper/empCardMapper.xml"/>
</mappers
>
(2)编写XXXMapper.xml映射文件。
编写XXXMapper.java接口(动态注入时)
<?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.mapper.EmpMapper">
<cache type
="org.mybatis.caches.ehcache.EhcacheCache"></cache
>
<sql id
="queryArray">
<where>
<if test
="array != null and array.length>0">
<foreach collection
="array" open
=" and empno in (" close
=")"
item
="empNo" separator
=",">
#
{empNo
}
</foreach
>
</if>
</where
>
</sql
>
<select id
="seleteEmp" resultType
="emp" parameterType
="int" useCache
="false">
select
* from emp where empno
= #
{empno
}
</select
>
<select id
="seleteEmp2" resultType
="emp" parameterType
="int" flushCache
="false">
select
* from emp where empno
= #
{empno
}
</select
>
<delete id
="deleteEmpByempNo" parameterType
="int">
delete from emp where empno
= #
{empno
}
</delete
>
</mapper
>
(动态注入时)
public interface EmpMapper {
List
<Emp> queryAllStudent();
Emp
seleteEmp(int empno
);
Emp
seleteEmp2(int empno
);
}
(3)编写测试类,通过sqlSessionFactory生成sqlSession。然后
通过sqlSession的selectOne("需要查询的SQL的namespace.id","SQL的参数值")等方法查询。
public static void seleteEmp2() throws IOException
{
String statement
="com.pojo.empMapper.seleteEmp";
String resource
= "config.xml";
InputStream inputStream
= Resources
.getResourceAsStream(resource
);
if (inputStream
!= null
) {
SqlSessionFactory sqlSessionFactory
= new SqlSessionFactoryBuilder().build(inputStream
);
SqlSession sqlSession
= sqlSessionFactory
.openSession();
Emp emp
=sqlSession
.selectOne(statement
,2);
sqlSession
.close();
SqlSession sqlSession2
= sqlSessionFactory
.openSession();
EmpMapper empMapper2
= sqlSession2
.getMapper(EmpMapper
.class);
Emp emp2
= empMapper2
.seleteEmp2(2);
System
.out
.println("qqq");
System
.out
.println(emp
);
System
.out
.println(emp2
);
sqlSession2
.close();
} else
System
.out
.println("222");
}
public static void main(String
[] args
) throws IOException
{
seleteEmp2();
}
MyBatis约定,输入参数parameterType和输出参数resultType在形式上都只能有一个 1.如果输入参数是简单类型,是可以使用任何占位符的,#{xxxx} 如果是对象类型,则必须是对象的属性名#{属性名} 2.输出参数 返回值是一个对象,那么无论是返回一个还是多个都是在写成 resultType=“com.wang.entity.Emp”(可以用别名) 需要注意的是,事务方式是jdbc ,需要用commit()手动提交。
Mapper动态代理时的crud
原则:约定优于配置 动态代理约定的目标: 省掉statement 即根据约定 可以直接定位出sql语句。 接口。xxxMapper.java 需要遵循: * 1.方法名和xxMapper.xml 的sql标签一致。 * 2.方法的 输入参数 和mapper.xml文件中标签的 parameterType类型一致 (如果mapper.xml的标签中没有 parameterType,则说明方法没有输入参数) * 3.方法的返回值 和mapper.xml文件中标签的 resultType类型一致 (无论查询结果是一个 还是多个(student、List),在mapper.xml标签中的resultType中只写 一个(Student);如果没有resultType,则说明方法的返回值为void)。
匹配的过程:(约定的过程)
根据 接口名 找到 mapper.xml文件(根据的是namespace=接口全类名)根据 接口的方法名 找到 mapper.xml文件中的SQL标签 (方法名=SQL标签Id值) 以上2点可以保证: 当我们调用接口中的方法时, 程序能自动定位到 某一个Mapper.xml文件中的sqL标签。 习惯:SQL映射文件(mapper.xml) 和 接口放在同一个包中 (注意修改conf.xml中加载mapper.xml文件的路径)(com.wang.mapper) 以上,可以通过接口的方法->SQL语句 执行:
StudentMapper studentMapper
= session
.getMapper(StudentMapper
.class) ;
studentMapper
.方法
();
通过session对象获取接口(session.getMapper(接口.class);),再调用该接口中的方法,程序会自动执行该方法对应的SQL。